index.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**
  2. Performs a deep comparison of the two values including support for circular dependencies, prototype, and enumerable properties.
  3. @param obj - The value being compared.
  4. @param ref - The reference value used for comparison.
  5. @return true when the two values are equal, otherwise false.
  6. */
  7. export function deepEqual(obj: any, ref: any, options?: deepEqual.Options): boolean;
  8. declare namespace deepEqual {
  9. interface Options {
  10. /**
  11. Allow partial match.
  12. @default false
  13. */
  14. readonly part?: boolean;
  15. /**
  16. Compare the objects' prototypes.
  17. @default true
  18. */
  19. readonly prototype?: boolean;
  20. /**
  21. Compare symbol properties.
  22. @default false
  23. */
  24. readonly symbols?: boolean;
  25. }
  26. }
  27. /**
  28. Clone any value, object, or array.
  29. @param obj - The value being cloned.
  30. @param options - The second number to add.
  31. @returns A deep clone of `obj`.
  32. */
  33. export function clone<T>(obj: T, options?: clone.Options): T;
  34. declare namespace clone {
  35. interface Options {
  36. /**
  37. Clone the object's prototype.
  38. @default true
  39. */
  40. readonly prototype?: boolean;
  41. /**
  42. Include symbol properties.
  43. @default false
  44. */
  45. readonly symbols?: boolean;
  46. }
  47. }
  48. /**
  49. Merge all the properties of source into target.
  50. @param target - The object being modified.
  51. @param source - The object used to copy properties from.
  52. @param isNullOverride - When true, null value from `source` overrides existing value in `target`. Defaults to true.
  53. @param isMergeArrays - When true, array value from `source` is merged with the existing value in `target`. Defaults to true.
  54. @returns The `target` object.
  55. */
  56. export function merge<T1 extends object, T2 extends object>(target: T1, source: T2, isNullOverride?: boolean, isMergeArrays?: boolean): T1 & T2;
  57. /**
  58. Apply options to a copy of the defaults.
  59. @param defaults - An object with the default values to use of `options` does not contain the same keys.
  60. @param options - The options used to override the `defaults`.
  61. @param isNullOverride - When true, null value from `options` overrides existing value in `defaults`. Defaults to false.
  62. @returns A copy of `defaults` with `options` keys overriding any conflicts.
  63. */
  64. export function applyToDefaults<T extends object>(defaults: Partial<T>, options: Partial<T> | boolean | null, isNullOverride?: boolean): Partial<T>;
  65. /**
  66. Clone an object or array with specific keys shallowly cloned.
  67. @param obj - The object being cloned.
  68. @param keys - An array of string keys indicating which `obj` properties to shallow copy instead of deep clone. Use dot-notation to indicate nested keys (e.g. "a.b").
  69. @returns A deep clone of `obj` with the requested `keys` shallowly cloned.
  70. */
  71. export function cloneWithShallow<T>(obj: T, keys: string[], options?: clone.Options): T;
  72. /**
  73. Apply options to a copy of the defaults with specific keys shallowly cloned.
  74. @param defaults - An object with the default values to use of `options` does not contain the same keys.
  75. @param options - The options used to override the `defaults`.
  76. @param keys - An array of string keys indicating which `options` properties to shallow copy instead of deep clone. Use dot-notation to indicate nested keys (e.g. "a.b").
  77. @returns A copy of `defaults` with `options` keys overriding any conflicts.
  78. */
  79. export function applyToDefaultsWithShallow<T extends object>(defaults: Partial<T>, options: Partial<T> | boolean | null, keys: string[]): Partial<T>;
  80. /**
  81. Find the common unique items in two arrays.
  82. @param array1 - The first array to compare.
  83. @param array2 - The second array to compare.
  84. @param justFirst - If true, return the first overlapping value. Defaults to false.
  85. @return - An array of the common items. If `justFirst` is true, returns the first common item.
  86. */
  87. export function intersect<T1, T2>(array1: intersect.Array<T1>, array2: intersect.Array<T2>, justFirst?: false): Array<T1 | T2>;
  88. export function intersect<T1, T2>(array1: intersect.Array<T1>, array2: intersect.Array<T2>, justFirst: true): T1 | T2;
  89. declare namespace intersect {
  90. type Array<T> = ArrayLike<T> | Set<T> | null;
  91. }
  92. /**
  93. Checks if the reference value contains the provided values.
  94. @param ref - The reference string, array, or object.
  95. @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.
  96. @return true if the value contains the provided values, otherwise false.
  97. */
  98. export function contain(ref: string, values: string | string[], options?: contain.Options): boolean;
  99. export function contain(ref: any[], values: any, options?: contain.Options): boolean;
  100. export function contain(ref: object, values: string | string[] | object, options?: contain.Options): boolean;
  101. declare namespace contain {
  102. interface Options {
  103. /**
  104. Perform a deep comparison.
  105. @default false
  106. */
  107. readonly deep?: boolean;
  108. /**
  109. Allow only one occurrence of each value.
  110. @default false
  111. */
  112. readonly once?: boolean;
  113. /**
  114. Allow only values explicitly listed.
  115. @default false
  116. */
  117. readonly only?: boolean;
  118. /**
  119. Allow partial match.
  120. @default false
  121. */
  122. readonly part?: boolean;
  123. /**
  124. Include symbol properties.
  125. @default false
  126. */
  127. readonly symbols?: boolean;
  128. }
  129. }
  130. /**
  131. Flatten an array with sub arrays
  132. @param array - an array of items or other arrays to flatten.
  133. @param target - if provided, an array to shallow copy the flattened `array` items to
  134. @return a flat array of the provided values (appended to `target` is provided).
  135. */
  136. export function flatten<T>(array: ArrayLike<T | ReadonlyArray<T>>, target?: ArrayLike<T | ReadonlyArray<T>>): T[];
  137. /**
  138. Convert an object key chain string to reference.
  139. @param obj - the object from which to look up the value.
  140. @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 negative indices on an array.
  141. @return The value referenced by the chain if found, otherwise undefined. If chain is null, undefined, or false, the object itself will be returned.
  142. */
  143. export function reach(obj: object | null, chain: string | (string | number)[] | false | null | undefined, options?: reach.Options): any;
  144. declare namespace reach {
  145. interface Options {
  146. /**
  147. String to split chain path on. Defaults to '.'.
  148. @default false
  149. */
  150. readonly separator?: string;
  151. /**
  152. Value to return if the path or value is not present. No default value.
  153. @default false
  154. */
  155. readonly default?: any;
  156. /**
  157. If true, will throw an error on missing member in the chain. Default to false.
  158. @default false
  159. */
  160. readonly strict?: boolean;
  161. /**
  162. If true, allows traversing functions for properties. false will throw an error if a function is part of the chain.
  163. @default false
  164. */
  165. readonly functions?: boolean;
  166. }
  167. }
  168. /**
  169. Replace string parameters (using format "{path.to.key}") with their corresponding object key values using `Hoek.reach()`.
  170. @param obj - the object from which to look up the value.
  171. @param template - the string containing {} enclosed key paths to be replaced.
  172. @return The template string with the {} enclosed keys replaced with looked-up values.
  173. */
  174. export function reachTemplate(obj: object | null, template: string, options?: reach.Options): string;
  175. /**
  176. Throw an error if condition is falsey.
  177. @param condition - If `condition` is not truthy, an exception is thrown.
  178. @param error - The error thrown if the condition fails.
  179. @return Does not return a value but throws if the `condition` is falsey.
  180. */
  181. export function assert(condition: any, error: Error): void;
  182. /**
  183. Throw an error if condition is falsey.
  184. @param condition - If `condition` is not truthy, an exception is thrown.
  185. @param args - Any number of values, concatenated together (space separated) to create the error message.
  186. @return Does not return a value but throws if the `condition` is falsey.
  187. */
  188. export function assert(condition: any, ...args: any): void;
  189. /**
  190. A benchmarking timer, using the internal node clock for maximum accuracy.
  191. */
  192. export class Bench {
  193. constructor();
  194. /** The starting timestamp expressed in the number of milliseconds since the epoch. */
  195. ts: number;
  196. /** The time in milliseconds since the object was created. */
  197. elapsed(): number;
  198. /** Reset the `ts` value to now. */
  199. reset(): void;
  200. /** The current time in milliseconds since the epoch. */
  201. static now(): number;
  202. }
  203. /**
  204. Escape string for Regex construction by prefixing all reserved characters with a backslash.
  205. @param string - The string to be escaped.
  206. @return The escaped string.
  207. */
  208. export function escapeRegex(string: string): string;
  209. /**
  210. Escape string for usage as an attribute value in HTTP headers.
  211. @param attribute - The string to be escaped.
  212. @return The escaped string. Will throw on invalid characters that are not supported to be escaped.
  213. */
  214. export function escapeHeaderAttribute(attribute: string): string;
  215. /**
  216. Escape string for usage in HTML.
  217. @param string - The string to be escaped.
  218. @return The escaped string.
  219. */
  220. export function escapeHtml(string: string): string;
  221. /**
  222. Escape string for usage in JSON.
  223. @param string - The string to be escaped.
  224. @return The escaped string.
  225. */
  226. export function escapeJson(string: string): string;
  227. /**
  228. Wraps a function to ensure it can only execute once.
  229. @param method - The function to be wrapped.
  230. @return The wrapped function.
  231. */
  232. export function once<T extends Function>(method: T): T;
  233. /**
  234. A reusable no-op function.
  235. */
  236. export function ignore(...ignore: any): void;
  237. /**
  238. Generate a random file name within a given path and optional extension.
  239. @param path - The file path within to generate a temporary file.
  240. @param extension - File extension.
  241. @return The generarted filename.
  242. */
  243. export function uniqueFilename(path: string, extension?: string): string;
  244. /**
  245. Converts a JavaScript value to a JavaScript Object Notation (JSON) string with protection against thrown errors.
  246. @param value A JavaScript value, usually an object or array, to be converted.
  247. @param replacer The JSON.stringify() `replacer` argument.
  248. @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
  249. @return The JSON string. If the operation fails, an error string value is returned (no exception thrown).
  250. */
  251. export function stringify(value: any, replacer?: any, space?: string | number): string;
  252. /**
  253. Returns a Promise that resolves after the requested timeout.
  254. @param timeout - The number of milliseconds to wait before resolving the Promise.
  255. @return A Promise.
  256. */
  257. export function wait(timeout?: number): Promise<void>;
  258. /**
  259. Returns a Promise that never resolves.
  260. */
  261. export function block(): Promise<void>;