UNPKG

5.17 kBTypeScriptView Raw
1export interface ContainOptions {
2 /** Perform a deep comparison of the values? */
3 deep?: boolean | undefined;
4 /** Allow only one occurrence of each value? */
5 once?: boolean | undefined;
6 /** Don't allow values not explicitly listed? */
7 only?: boolean | undefined;
8 /** Allow partial match of the values? */
9 part?: boolean | undefined;
10}
11
12export interface ReachOptions {
13 /** String to split chain path on. Defaults to ".". */
14 separator?: string | undefined;
15 /** Value to return if the path or value is not present. Default is undefined. */
16 default?: any;
17 /** Throw an error on missing member? Default is false. */
18 strict?: boolean | undefined;
19 /** Allow traversing functions for properties? */
20 functions?: boolean | undefined;
21}
22
23// Object
24
25/**
26 * Clone an object or an array.
27 */
28export function clone<T>(obj: T): T;
29
30/**
31 * Clone an object or array.
32 */
33export function cloneWithShallow(obj: any, keys: string[]): any;
34
35/**
36 * Merge all the properties of source into target.
37 */
38export function merge<T1, T2>(target: T1, source: T2, isNullOverride?: boolean, isMergeArrays?: boolean): T1 & T2;
39
40/**
41 * Apply options to a copy of the defaults.
42 */
43export function applyToDefaults<T1, T2>(defaults: T1, options: T2, isNullOverride?: boolean): T1 & T2;
44
45/**
46 * Apply options to a copy of the defaults.
47 */
48export function applyToDefaultsWithShallow<T1, T2>(defaults: T1, options: T2, keys?: string[]): T1 & T2;
49
50/**
51 * Perform a deep comparison of the two values.
52 */
53export function deepEqual<T>(b: T, a: T, options?: any): T;
54
55/**
56 * Remove duplicate items from Array.
57 */
58export function unique<T>(array: T[], key?: string): T[];
59
60/**
61 * Convert an Array into an Object.
62 */
63export function mapToObject(array: any[], key?: string): any;
64
65/**
66 * Find the common unique items in two arrays.
67 */
68export function intersect(array1: any[], array2: any[]): any;
69
70/**
71 * Test if the reference value contains the provided values.
72 */
73export function contain(ref: any, values: any, options?: ContainOptions): boolean;
74
75/**
76 * Flatten an array.
77 */
78export function flatten(array: any[], target?: any[]): any[];
79
80/**
81 * Convert an object key chain string to reference.
82 */
83export function reach(obj: any, chain: any, options?: ReachOptions): any;
84
85/**
86 * Replace string parameters ({name}) with their corresponding object key values.
87 */
88export function reachTemplate(obj: any, template: string, options?: ReachOptions): any;
89
90/**
91 * Transform an existing object into a new one based on the supplied obj and transform map.
92 */
93export function transform(obj: any, transform: any, options?: ReachOptions): any;
94
95/**
96 * Perform a shallow copy by copying the references of all the top level children.
97 */
98export function shallow(obj: any): any;
99
100/**
101 * Convert an object to string. Any errors are caught and reported back in the form of the returned string.
102 */
103export function stringify(obj: any): string;
104
105// Timer
106
107/**
108 * A Timer object.
109 */
110export class Timer {
111 /** The number of milliseconds elapsed since the epoch. */
112 ts: number;
113 /** The time (ms) elapsed since the timer was created. */
114 elapsed(): number;
115}
116
117// Bench
118
119/**
120 * Same as Timer, except ts stores the internal node clock.
121 */
122export class Bench {
123 /** The number of milliseconds on the node clock elapsed since the epoch. */
124 ts: number;
125 /** The time (ms) elapsed since the timer was created. */
126 elapsed(): number;
127}
128
129// Binary Encoding/Decoding
130
131/**
132 * Encode value of string or buffer type in Base64 or URL encoding.
133 */
134export function base64urlEncode(value: string): string;
135
136/**
137 * Decode string into Base64 or URL encoding.
138 */
139export function base64urlDecode(value: string): string;
140
141// Escaping Characters
142
143/**
144 * Escape html characters.
145 */
146export function escapeHtml(htmlString: string): string;
147
148/**
149 * Escape attribute value for use in HTTP header.
150 */
151export function escapeHeaderAttribute(attribute: string): string;
152
153/**
154 * Escape string for Regex construction.
155 */
156export function escapeRegex(regexString: string): string;
157
158// Errors
159
160/**
161 * Print message or throw error if condition fails.
162 */
163// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
164export function assert(condition: boolean, message: string | Error): void | Error;
165
166/**
167 * Throw if process.env.NODE_ENV === 'test'. Else display most recent stack and exit process.
168 */
169export function abort(message: string | Error): void;
170
171/**
172 * Display the trace stack.
173 */
174export function displayStack(slice?: any): string[];
175
176/**
177 * Return a trace stack array.
178 */
179export function callStack(slice?: any): any[];
180
181// Function
182
183/**
184 * Wrap fn in process.nextTick.
185 */
186export function nextTick(fn: () => void): () => void;
187
188/**
189 * Make sure fn is only run once.
190 */
191export function once(fn: () => void): () => void;
192
193/**
194 * A simple no-op function.
195 */
196export function ignore(): void;
197
198// Miscellaneous
199
200/**
201 * path to prepend to a randomly generated file name.
202 */
203export function uniqueFilename(path: string, extension?: string): string;
204
205/**
206 * Check value to see if it is an integer.
207 */
208export function isInteger(value: any): boolean;