index.d.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /// <reference types="node" />
  2. /**
  3. * Performs a deep comparison of the two values including support for circular dependencies, prototype, and enumerable properties.
  4. *
  5. * @param obj - The value being compared.
  6. * @param ref - The reference value used for comparison.
  7. *
  8. * @return true when the two values are equal, otherwise false.
  9. */
  10. export function deepEqual(obj: any, ref: any, options?: deepEqual.Options): boolean;
  11. export namespace deepEqual {
  12. interface Options {
  13. /**
  14. * Compare functions with difference references by comparing their internal code and properties.
  15. *
  16. * @default false
  17. */
  18. readonly deepFunction?: boolean;
  19. /**
  20. * Allow partial match.
  21. *
  22. * @default false
  23. */
  24. readonly part?: boolean;
  25. /**
  26. * Compare the objects' prototypes.
  27. *
  28. * @default true
  29. */
  30. readonly prototype?: boolean;
  31. /**
  32. * List of object keys to ignore different values of.
  33. *
  34. * @default null
  35. */
  36. readonly skip?: (string | symbol)[];
  37. /**
  38. * Compare symbol properties.
  39. *
  40. * @default true
  41. */
  42. readonly symbols?: boolean;
  43. }
  44. }
  45. /**
  46. * Clone any value, object, or array.
  47. *
  48. * @param obj - The value being cloned.
  49. * @param options - Optional settings.
  50. *
  51. * @returns A deep clone of `obj`.
  52. */
  53. export function clone<T>(obj: T, options?: clone.Options): T;
  54. export namespace clone {
  55. interface Options {
  56. /**
  57. * Clone the object's prototype.
  58. *
  59. * @default true
  60. */
  61. readonly prototype?: boolean;
  62. /**
  63. * Include symbol properties.
  64. *
  65. * @default true
  66. */
  67. readonly symbols?: boolean;
  68. /**
  69. * Shallow clone the specified keys.
  70. *
  71. * @default undefined
  72. */
  73. readonly shallow?: string[] | string[][] | boolean;
  74. }
  75. }
  76. /**
  77. * Merge all the properties of source into target.
  78. *
  79. * @param target - The object being modified.
  80. * @param source - The object used to copy properties from.
  81. * @param options - Optional settings.
  82. *
  83. * @returns The `target` object.
  84. */
  85. export function merge<T1 extends object, T2 extends object>(target: T1, source: T2, options?: merge.Options): T1 & T2;
  86. export namespace merge {
  87. interface Options {
  88. /**
  89. * When true, null value from `source` overrides existing value in `target`.
  90. *
  91. * @default true
  92. */
  93. readonly nullOverride?: boolean;
  94. /**
  95. * When true, array value from `source` is merged with the existing value in `target`.
  96. *
  97. * @default false
  98. */
  99. readonly mergeArrays?: boolean;
  100. /**
  101. * Compare symbol properties.
  102. *
  103. * @default true
  104. */
  105. readonly symbols?: boolean;
  106. }
  107. }
  108. /**
  109. * Apply source to a copy of the defaults.
  110. *
  111. * @param defaults - An object with the default values to use of `options` does not contain the same keys.
  112. * @param source - The source used to override the `defaults`.
  113. * @param options - Optional settings.
  114. *
  115. * @returns A copy of `defaults` with `source` keys overriding any conflicts.
  116. */
  117. export function applyToDefaults<T extends object>(defaults: Partial<T>, source: Partial<T> | boolean | null, options?: applyToDefaults.Options): Partial<T>;
  118. export namespace applyToDefaults {
  119. interface Options {
  120. /**
  121. * When true, null value from `source` overrides existing value in `target`.
  122. *
  123. * @default true
  124. */
  125. readonly nullOverride?: boolean;
  126. /**
  127. * Shallow clone the specified keys.
  128. *
  129. * @default undefined
  130. */
  131. readonly shallow?: string[] | string[][];
  132. }
  133. }
  134. /**
  135. * Find the common unique items in two arrays.
  136. *
  137. * @param array1 - The first array to compare.
  138. * @param array2 - The second array to compare.
  139. * @param options - Optional settings.
  140. *
  141. * @return - An array of the common items. If `justFirst` is true, returns the first common item.
  142. */
  143. export function intersect<T1, T2>(array1: intersect.Array<T1>, array2: intersect.Array<T2>, options?: intersect.Options): Array<T1 | T2>;
  144. export function intersect<T1, T2>(array1: intersect.Array<T1>, array2: intersect.Array<T2>, options?: intersect.Options): T1 | T2;
  145. export namespace intersect {
  146. type Array<T> = ArrayLike<T> | Set<T> | null;
  147. interface Options {
  148. /**
  149. * When true, return the first overlapping value.
  150. *
  151. * @default false
  152. */
  153. readonly first?: boolean;
  154. }
  155. }
  156. /**
  157. * Checks if the reference value contains the provided values.
  158. *
  159. * @param ref - The reference string, array, or object.
  160. * @param values - A single or array of values to find within `ref`. If `ref` is an object, `values` can be a key name, an array of key names, or an object with key-value pairs to compare.
  161. *
  162. * @return true if the value contains the provided values, otherwise false.
  163. */
  164. export function contain(ref: string, values: string | string[], options?: contain.Options): boolean;
  165. export function contain(ref: any[], values: any, options?: contain.Options): boolean;
  166. export function contain(ref: object, values: string | string[] | object, options?: Omit<contain.Options, 'once'>): boolean;
  167. export namespace contain {
  168. interface Options {
  169. /**
  170. * Perform a deep comparison.
  171. *
  172. * @default false
  173. */
  174. readonly deep?: boolean;
  175. /**
  176. * Allow only one occurrence of each value.
  177. *
  178. * @default false
  179. */
  180. readonly once?: boolean;
  181. /**
  182. * Allow only values explicitly listed.
  183. *
  184. * @default false
  185. */
  186. readonly only?: boolean;
  187. /**
  188. * Allow partial match.
  189. *
  190. * @default false
  191. */
  192. readonly part?: boolean;
  193. /**
  194. * Include symbol properties.
  195. *
  196. * @default true
  197. */
  198. readonly symbols?: boolean;
  199. }
  200. }
  201. /**
  202. * Flatten an array with sub arrays
  203. *
  204. * @param array - an array of items or other arrays to flatten.
  205. * @param target - if provided, an array to shallow copy the flattened `array` items to
  206. *
  207. * @return a flat array of the provided values (appended to `target` is provided).
  208. */
  209. export function flatten<T>(array: ArrayLike<T | ReadonlyArray<T>>, target?: ArrayLike<T | ReadonlyArray<T>>): T[];
  210. /**
  211. * Convert an object key chain string to reference.
  212. *
  213. * @param obj - the object from which to look up the value.
  214. * @param chain - the string path of the requested value. The chain string is split into key names using `options.separator`, or an array containing each individual key name. A chain including negative numbers will work like a negative index on an array.
  215. *
  216. * @return The value referenced by the chain if found, otherwise undefined. If chain is null, undefined, or false, the object itself will be returned.
  217. */
  218. export function reach(obj: object | null, chain: string | (string | number)[] | false | null | undefined, options?: reach.Options): any;
  219. export namespace reach {
  220. interface Options {
  221. /**
  222. * String to split chain path on. Defaults to '.'.
  223. *
  224. * @default false
  225. */
  226. readonly separator?: string;
  227. /**
  228. * Value to return if the path or value is not present. No default value.
  229. *
  230. * @default false
  231. */
  232. readonly default?: any;
  233. /**
  234. * If true, will throw an error on missing member in the chain. Default to false.
  235. *
  236. * @default false
  237. */
  238. readonly strict?: boolean;
  239. /**
  240. * If true, allows traversing functions for properties. false will throw an error if a function is part of the chain.
  241. *
  242. * @default true
  243. */
  244. readonly functions?: boolean;
  245. /**
  246. * If true, allows traversing Set and Map objects for properties. false will return undefined regardless of the Set or Map passed.
  247. *
  248. * @default false
  249. */
  250. readonly iterables?: boolean;
  251. }
  252. }
  253. /**
  254. * Replace string parameters (using format "{path.to.key}") with their corresponding object key values using `Hoek.reach()`.
  255. *
  256. * @param obj - the object from which to look up the value.
  257. * @param template - the string containing {} enclosed key paths to be replaced.
  258. *
  259. * @return The template string with the {} enclosed keys replaced with looked-up values.
  260. */
  261. export function reachTemplate(obj: object | null, template: string, options?: reach.Options): string;
  262. /**
  263. * Throw an error if condition is falsy.
  264. *
  265. * @param condition - If `condition` is not truthy, an exception is thrown.
  266. * @param error - The error thrown if the condition fails.
  267. *
  268. * @return Does not return a value but throws if the `condition` is falsy.
  269. */
  270. export function assert(condition: any, error: Error): void;
  271. /**
  272. * Throw an error if condition is falsy.
  273. *
  274. * @param condition - If `condition` is not truthy, an exception is thrown.
  275. * @param args - Any number of values, concatenated together (space separated) to create the error message.
  276. *
  277. * @return Does not return a value but throws if the `condition` is falsy.
  278. */
  279. export function assert(condition: any, ...args: any): void;
  280. /**
  281. * A benchmarking timer, using the internal node clock for maximum accuracy.
  282. */
  283. export class Bench {
  284. constructor();
  285. /** The starting timestamp expressed in the number of milliseconds since the epoch. */
  286. ts: number;
  287. /** The time in milliseconds since the object was created. */
  288. elapsed(): number;
  289. /** Reset the `ts` value to now. */
  290. reset(): void;
  291. /** The current time in milliseconds since the epoch. */
  292. static now(): number;
  293. }
  294. /**
  295. * Escape string for Regex construction by prefixing all reserved characters with a backslash.
  296. *
  297. * @param string - The string to be escaped.
  298. *
  299. * @return The escaped string.
  300. */
  301. export function escapeRegex(string: string): string;
  302. /**
  303. * Escape string for usage as an attribute value in HTTP headers.
  304. *
  305. * @param attribute - The string to be escaped.
  306. *
  307. * @return The escaped string. Will throw on invalid characters that are not supported to be escaped.
  308. */
  309. export function escapeHeaderAttribute(attribute: string): string;
  310. /**
  311. * Escape string for usage in HTML.
  312. *
  313. * @param string - The string to be escaped.
  314. *
  315. * @return The escaped string.
  316. */
  317. export function escapeHtml(string: string): string;
  318. /**
  319. * Escape string for usage in JSON.
  320. *
  321. * @param string - The string to be escaped.
  322. *
  323. * @return The escaped string.
  324. */
  325. export function escapeJson(string: string): string;
  326. /**
  327. * Wraps a function to ensure it can only execute once.
  328. *
  329. * @param method - The function to be wrapped.
  330. *
  331. * @return The wrapped function.
  332. */
  333. export function once<T extends Function>(method: T): T;
  334. /**
  335. * A reusable no-op function.
  336. */
  337. export function ignore(...ignore: any): void;
  338. /**
  339. * Converts a JavaScript value to a JavaScript Object Notation (JSON) string with protection against thrown errors.
  340. *
  341. * @param value A JavaScript value, usually an object or array, to be converted.
  342. * @param replacer The JSON.stringify() `replacer` argument.
  343. * @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
  344. *
  345. * @return The JSON string. If the operation fails, an error string value is returned (no exception thrown).
  346. */
  347. export function stringify(value: any, replacer?: any, space?: string | number): string;
  348. /**
  349. * Returns a Promise that resolves after the requested timeout.
  350. *
  351. * @param timeout - The number of milliseconds to wait before resolving the Promise.
  352. *
  353. * @return A Promise.
  354. */
  355. export function wait(timeout?: number): Promise<void>;
  356. /**
  357. * Returns a Promise that never resolves.
  358. */
  359. export function block(): Promise<void>;
  360. /**
  361. * Determines if an object is a promise.
  362. *
  363. * @param promise - the object tested.
  364. *
  365. * @returns true if the object is a promise, otherwise false.
  366. */
  367. export function isPromise(promise: any): boolean;
  368. export namespace ts {
  369. /**
  370. * Defines a type that can must be one of T or U but not both.
  371. */
  372. type XOR<T, U> = (T | U) extends object ? (internals.Without<T, U> & U) | (internals.Without<U, T> & T) : T | U;
  373. }
  374. declare namespace internals {
  375. type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
  376. }