@radio-garden/ditojs-utils 2.85.0-0.5067ad799
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/package.json +43 -0
- package/src/__snapshots__/index.test.js.snap +88 -0
- package/src/array/flatten.js +14 -0
- package/src/array/flattten.test.js +29 -0
- package/src/array/index.js +2 -0
- package/src/array/shuffle.js +11 -0
- package/src/array/shuffle.test.js +25 -0
- package/src/base/base.js +118 -0
- package/src/base/base.test.js +590 -0
- package/src/base/index.js +1 -0
- package/src/class/index.js +1 -0
- package/src/class/mixin.js +29 -0
- package/src/class/mixin.test.js +70 -0
- package/src/dataPath/getEntriesAtDataPath.js +67 -0
- package/src/dataPath/getEntriesAtDataPath.test.js +204 -0
- package/src/dataPath/getValueAtDataPath.js +45 -0
- package/src/dataPath/getValueAtDataPath.test.js +140 -0
- package/src/dataPath/index.js +6 -0
- package/src/dataPath/normalizeDataPath.js +27 -0
- package/src/dataPath/normalizeDataPath.test.js +36 -0
- package/src/dataPath/parseDataPath.js +16 -0
- package/src/dataPath/parseDataPath.test.js +67 -0
- package/src/dataPath/setDataPathEntries.js +8 -0
- package/src/dataPath/setDataPathEntries.test.js +36 -0
- package/src/dataPath/setValueAtDataPath.js +13 -0
- package/src/dataPath/setValueAtDataPath.test.js +34 -0
- package/src/function/deprecate.js +9 -0
- package/src/function/index.js +4 -0
- package/src/function/toAsync.js +9 -0
- package/src/function/toAsync.test.js +31 -0
- package/src/function/toCallback.js +11 -0
- package/src/function/toCallback.test.js +29 -0
- package/src/function/toPromiseCallback.js +9 -0
- package/src/function/toPromiseCallback.test.js +24 -0
- package/src/html/escapeHtml.js +11 -0
- package/src/html/escapeHtml.test.js +19 -0
- package/src/html/index.js +2 -0
- package/src/html/stripHtml.js +23 -0
- package/src/html/stripHtml.test.js +37 -0
- package/src/index.js +10 -0
- package/src/index.test.js +7 -0
- package/src/object/asCallback.js +9 -0
- package/src/object/clone.js +75 -0
- package/src/object/clone.test.js +131 -0
- package/src/object/equals.js +36 -0
- package/src/object/equals.test.js +269 -0
- package/src/object/groupBy.js +15 -0
- package/src/object/groupBy.test.js +70 -0
- package/src/object/index.js +8 -0
- package/src/object/mapKeys.js +7 -0
- package/src/object/mapKeys.test.js +16 -0
- package/src/object/mapValues.js +9 -0
- package/src/object/mapValues.test.js +38 -0
- package/src/object/mergeDeeply.js +47 -0
- package/src/object/mergeDeeply.test.js +152 -0
- package/src/object/pick.js +11 -0
- package/src/object/pick.test.js +23 -0
- package/src/object/pickBy.js +11 -0
- package/src/object/pickBy.test.js +48 -0
- package/src/promise/index.js +2 -0
- package/src/promise/mapConcurrently.js +33 -0
- package/src/promise/mapSequentially.js +9 -0
- package/src/string/camelize.js +14 -0
- package/src/string/camelize.test.js +37 -0
- package/src/string/capitalize.js +7 -0
- package/src/string/capitalize.test.js +33 -0
- package/src/string/decamelize.js +27 -0
- package/src/string/decamelize.test.js +83 -0
- package/src/string/deindent.js +69 -0
- package/src/string/deindent.test.js +181 -0
- package/src/string/escapeRegexp.js +3 -0
- package/src/string/format.js +109 -0
- package/src/string/format.test.js +196 -0
- package/src/string/formatDate.js +13 -0
- package/src/string/formatDate.test.js +28 -0
- package/src/string/getCommonPrefix.js +35 -0
- package/src/string/getCommonPrefix.test.js +23 -0
- package/src/string/index.js +15 -0
- package/src/string/isAbsoluteUrl.js +7 -0
- package/src/string/isAbsoluteUrl.test.js +15 -0
- package/src/string/isCreditCard.js +21 -0
- package/src/string/isCreditCard.test.js +50 -0
- package/src/string/isDomain.js +9 -0
- package/src/string/isDomain.test.js +15 -0
- package/src/string/isEmail.js +6 -0
- package/src/string/isEmail.test.js +37 -0
- package/src/string/isHostname.js +8 -0
- package/src/string/isHostname.test.js +12 -0
- package/src/string/isUrl.js +23 -0
- package/src/string/isUrl.test.js +1595 -0
- package/src/string/labelize.js +17 -0
- package/src/string/labelize.test.js +39 -0
- package/src/timer/debounce.js +34 -0
- package/src/timer/debounce.test.js +101 -0
- package/src/timer/debounceAsync.js +60 -0
- package/src/timer/debounceAsync.test.js +143 -0
- package/src/timer/index.js +2 -0
- package/types/index.d.ts +939 -0
- package/types/tests/base.test-d.ts +172 -0
- package/types/tests/datapath.test-d.ts +75 -0
- package/types/tests/function.test-d.ts +137 -0
- package/types/tests/object.test-d.ts +190 -0
- package/types/tests/promise.test-d.ts +66 -0
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,939 @@
|
|
|
1
|
+
// Type definitions for Dito.js utils
|
|
2
|
+
// Project: <https://github.com/ditojs/dito/>
|
|
3
|
+
|
|
4
|
+
/* ---------------------------------- base ---------------------------------- */
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Re-export of `Array.isArray` as a type guard.
|
|
8
|
+
*/
|
|
9
|
+
export const isArray: (arg: any) => arg is any[]
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Determines whether both supplied values are the same value, using the
|
|
13
|
+
* SameValue algorithm.
|
|
14
|
+
* @see {@link http://ecma-international.org/ecma-262/5.1/#sec-9.12 The SameValue Algorithm}
|
|
15
|
+
*/
|
|
16
|
+
export function is(value1: any, value2: any): boolean
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Determines whether the supplied value is a plain object.
|
|
20
|
+
*/
|
|
21
|
+
export function isPlainObject(arg: any): arg is Record<string, unknown>
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Determines whether the supplied value is a plain `Array` instance.
|
|
25
|
+
*/
|
|
26
|
+
export function isPlainArray(arg: any): arg is any[]
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Determines whether the supplied value is an object.
|
|
30
|
+
*/
|
|
31
|
+
export function isObject(arg: any): arg is Record<string, unknown>
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Determines whether the supplied value is a function.
|
|
35
|
+
*/
|
|
36
|
+
export function isFunction(arg: any): arg is (...args: any[]) => any
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Determines whether the supplied value is a number.
|
|
40
|
+
*/
|
|
41
|
+
export function isNumber(arg: any): arg is number
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Determines whether the supplied value is a string.
|
|
45
|
+
*/
|
|
46
|
+
export function isString(arg: any): arg is string | String
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Determines whether the supplied value is a boolean.
|
|
50
|
+
*/
|
|
51
|
+
export function isBoolean(arg: any): arg is boolean
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Determines whether the supplied value is an ES module object.
|
|
55
|
+
*/
|
|
56
|
+
export function isModule(arg: any): boolean
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Determines whether the supplied value is a Date object.
|
|
60
|
+
*/
|
|
61
|
+
export function isDate(arg: any): arg is Date
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Determines whether the supplied value is a regular expression (RegExp).
|
|
65
|
+
*/
|
|
66
|
+
export function isRegExp(arg: any): arg is RegExp
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Determines whether the supplied value is a Promise.
|
|
70
|
+
*/
|
|
71
|
+
export function isPromise(arg: any): arg is Promise<any>
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Determines whether the supplied value is an integer.
|
|
75
|
+
*/
|
|
76
|
+
export function isInteger(arg: any): arg is number
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Determines whether the supplied value is an async function.
|
|
80
|
+
*/
|
|
81
|
+
export function isAsync(
|
|
82
|
+
arg: any
|
|
83
|
+
): arg is (...args: any[]) => Promise<any>
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Determines whether the supplied value is array like, i.e. it has a length
|
|
87
|
+
• property between `0` and `Number.MAX_SAFE_INTEGER` and is not a function.
|
|
88
|
+
*/
|
|
89
|
+
export function isArrayLike(arg: any): arg is ArrayLike<any>
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Determines whether the supplied value can be considered empty,
|
|
93
|
+
* i.e. undefined, null, an empty string, or an object without properties.
|
|
94
|
+
*/
|
|
95
|
+
export function isEmpty(arg: any): boolean
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Returns the supplied value as an object. The most frequent use case is to
|
|
99
|
+
* add properties to a primitive. As primitives are immutable, they need to be
|
|
100
|
+
* converted to an object in order to do so.
|
|
101
|
+
*
|
|
102
|
+
* @see {@link https://2ality.com/2011/04/javascript-converting-any-value-to.html JavaScript: converting any value to an object}
|
|
103
|
+
*/
|
|
104
|
+
export function asObject<T>(
|
|
105
|
+
arg: T
|
|
106
|
+
): T extends null | undefined ? T : T & Object
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Returns the supplied value as an array.
|
|
110
|
+
*
|
|
111
|
+
* When the supplied value is an array, the original array is returned. When
|
|
112
|
+
* the supplied value is undefined, an empty array is returned. Otherwise an
|
|
113
|
+
* array is returned containing the supplied value as its only element.
|
|
114
|
+
*/
|
|
115
|
+
export function asArray<T>(
|
|
116
|
+
arg: T
|
|
117
|
+
): T extends any[] ? T : Exclude<T, undefined>[]
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Returns the supplied value as a function.
|
|
121
|
+
*
|
|
122
|
+
* When the supplied value is a function, the original function is returned.
|
|
123
|
+
* Otherwise a function is returned that returns the value when called.
|
|
124
|
+
*/
|
|
125
|
+
export function asFunction<T>(
|
|
126
|
+
arg: T
|
|
127
|
+
): T extends Function ? T : () => T
|
|
128
|
+
|
|
129
|
+
/* --------------------------------- object --------------------------------- */
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Clones the supplied value.
|
|
133
|
+
*/
|
|
134
|
+
export function clone<T>(value: T, options?: {
|
|
135
|
+
/**
|
|
136
|
+
* Whether to clone the value shallowly.
|
|
137
|
+
*
|
|
138
|
+
* @default false
|
|
139
|
+
*/
|
|
140
|
+
shallow?: boolean
|
|
141
|
+
/**
|
|
142
|
+
* Whether to clone all object properties, or only those that are defined
|
|
143
|
+
* directly on that object, and are not inherited from the object's prototype.
|
|
144
|
+
*
|
|
145
|
+
* @default true
|
|
146
|
+
*/
|
|
147
|
+
enumerable?: boolean
|
|
148
|
+
/**
|
|
149
|
+
* @default `true` if `options.enumerable` is true, `false` otherwise
|
|
150
|
+
*/
|
|
151
|
+
descriptors?: boolean
|
|
152
|
+
transferables?: any[]
|
|
153
|
+
/**
|
|
154
|
+
* Optional callback to process the cloned value.
|
|
155
|
+
*/
|
|
156
|
+
processValue?: (value: T) => T | undefined | void
|
|
157
|
+
}): T
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Determines whether the supplied values can be considered equal through
|
|
161
|
+
* recursive comparison of object properties and array elements. Non-objects
|
|
162
|
+
* and non-arrays are compared using the SameValue algorithm.
|
|
163
|
+
*/
|
|
164
|
+
export function equals(arg1: any, arg2: any): boolean
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Groups items in a collection by a key returned from the callback function,
|
|
168
|
+
* or by a property name when a string is provided.
|
|
169
|
+
*/
|
|
170
|
+
export function groupBy<T, K extends keyof T>(
|
|
171
|
+
collection: T[] | Record<string, T>,
|
|
172
|
+
callback: K
|
|
173
|
+
): Record<string, T[]>
|
|
174
|
+
export function groupBy<T, K extends string | number | symbol>(
|
|
175
|
+
collection: T[] | Record<string, T>,
|
|
176
|
+
callback: (item: T) => K
|
|
177
|
+
): Record<K, T[]>
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Recursively merges multiple source objects into the target object. For
|
|
181
|
+
* arrays, merges objects at the same indices and concatenates
|
|
182
|
+
* non-mergeable values. Does not override root-level values with
|
|
183
|
+
* nullish values.
|
|
184
|
+
*/
|
|
185
|
+
export function mergeDeeply<ArgA, ArgB>(a: ArgA, b: ArgB): ArgA & ArgB
|
|
186
|
+
export function mergeDeeply<ArgA, ArgB, ArgC>(
|
|
187
|
+
a: ArgA,
|
|
188
|
+
b: ArgB,
|
|
189
|
+
c: ArgC
|
|
190
|
+
): ArgA & ArgB & ArgC
|
|
191
|
+
export function mergeDeeply<ArgA, ArgB, ArgC, ArgD>(
|
|
192
|
+
a: ArgA,
|
|
193
|
+
b: ArgB,
|
|
194
|
+
c: ArgC,
|
|
195
|
+
d: ArgD
|
|
196
|
+
): ArgA & ArgB & ArgC & ArgD
|
|
197
|
+
export function mergeDeeply<ArgA, ArgB, ArgC, ArgD, ArgE>(
|
|
198
|
+
a: ArgA,
|
|
199
|
+
b: ArgB,
|
|
200
|
+
c: ArgC,
|
|
201
|
+
d: ArgD,
|
|
202
|
+
e: ArgE
|
|
203
|
+
): ArgA & ArgB & ArgC & ArgD & ArgE
|
|
204
|
+
export function mergeDeeply(...args: any[]): any
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Recursively assigns values from source objects into the target object.
|
|
208
|
+
* Similar to `mergeDeeply` but without array concatenation — overwrites
|
|
209
|
+
* array values at the same indices instead. Does not override
|
|
210
|
+
* root-level values with nullish values.
|
|
211
|
+
*/
|
|
212
|
+
export function assignDeeply<ArgA, ArgB>(a: ArgA, b: ArgB): ArgA & ArgB
|
|
213
|
+
export function assignDeeply<ArgA, ArgB, ArgC>(
|
|
214
|
+
a: ArgA,
|
|
215
|
+
b: ArgB,
|
|
216
|
+
c: ArgC
|
|
217
|
+
): ArgA & ArgB & ArgC
|
|
218
|
+
export function assignDeeply<ArgA, ArgB, ArgC, ArgD>(
|
|
219
|
+
a: ArgA,
|
|
220
|
+
b: ArgB,
|
|
221
|
+
c: ArgC,
|
|
222
|
+
d: ArgD
|
|
223
|
+
): ArgA & ArgB & ArgC & ArgD
|
|
224
|
+
export function assignDeeply<ArgA, ArgB, ArgC, ArgD, ArgE>(
|
|
225
|
+
a: ArgA,
|
|
226
|
+
b: ArgB,
|
|
227
|
+
c: ArgC,
|
|
228
|
+
d: ArgD,
|
|
229
|
+
e: ArgE
|
|
230
|
+
): ArgA & ArgB & ArgC & ArgD & ArgE
|
|
231
|
+
export function assignDeeply(...args: any[]): any
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Returns the first argument which is not undefined.
|
|
235
|
+
*/
|
|
236
|
+
export function pick<ArgA, ArgB>(a: ArgA, b: ArgB): ArgA | ArgB
|
|
237
|
+
export function pick<ArgA, ArgB, ArgC>(
|
|
238
|
+
a: ArgA,
|
|
239
|
+
b: ArgB,
|
|
240
|
+
c: ArgC
|
|
241
|
+
): ArgA | ArgB | ArgC
|
|
242
|
+
export function pick<ArgA, ArgB, ArgC, ArgD>(
|
|
243
|
+
a: ArgA,
|
|
244
|
+
b: ArgB,
|
|
245
|
+
c: ArgC,
|
|
246
|
+
d: ArgD
|
|
247
|
+
): ArgA | ArgB | ArgC | ArgD
|
|
248
|
+
export function pick<ArgA, ArgB, ArgC, ArgD, ArgE>(
|
|
249
|
+
a: ArgA,
|
|
250
|
+
b: ArgB,
|
|
251
|
+
c: ArgC,
|
|
252
|
+
d: ArgD,
|
|
253
|
+
e: ArgE
|
|
254
|
+
): ArgA | ArgB | ArgC | ArgD | ArgE
|
|
255
|
+
export function pick(...args: any[]): any
|
|
256
|
+
/**
|
|
257
|
+
* Creates an object composed of the object properties predicate returns
|
|
258
|
+
* truthy for. When a string is provided, filters by the truthiness of that
|
|
259
|
+
* property on each value.
|
|
260
|
+
* @param object The source object.
|
|
261
|
+
* @param callback Callback invoked with three arguments: (value, key, item),
|
|
262
|
+
* or a property name string to check for truthiness.
|
|
263
|
+
*/
|
|
264
|
+
export function pickBy<T extends Dictionary<any>, K extends keyof T[keyof T]>(
|
|
265
|
+
object: T,
|
|
266
|
+
callback: K
|
|
267
|
+
): Partial<T>
|
|
268
|
+
export function pickBy<T extends Dictionary<any>>(
|
|
269
|
+
object: T,
|
|
270
|
+
callback: (value: T[keyof T], key: keyof T, object: T) => any
|
|
271
|
+
): Partial<T>
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Transforms object keys by applying a callback function to each
|
|
275
|
+
* key-value pair. Returns a new object with the transformed keys and
|
|
276
|
+
* original values.
|
|
277
|
+
*/
|
|
278
|
+
export function mapKeys<T extends Dictionary<any>, K extends keyof any>(
|
|
279
|
+
object: T,
|
|
280
|
+
callback: (key: keyof T, value: T[keyof T], object: T) => K
|
|
281
|
+
): Record<K, T[keyof T]>
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Maps the values of an object using a callback function, or extracts a
|
|
285
|
+
* property when a string is provided.
|
|
286
|
+
*/
|
|
287
|
+
export function mapValues<
|
|
288
|
+
T extends Dictionary<any>,
|
|
289
|
+
K extends keyof T[keyof T]
|
|
290
|
+
>(
|
|
291
|
+
object: T,
|
|
292
|
+
callback: K
|
|
293
|
+
): Record<keyof T, T[keyof T][K]>
|
|
294
|
+
export function mapValues<T extends Dictionary<any>, K>(
|
|
295
|
+
object: T,
|
|
296
|
+
callback: (value: T[keyof T], key: keyof T, object: T) => K
|
|
297
|
+
): Record<keyof T, K>
|
|
298
|
+
|
|
299
|
+
/* -------------------------------- promise -------------------------------- */
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Maps an async callback over an array with controlled concurrency.
|
|
303
|
+
* Executes all promises in parallel by default, or limits concurrent
|
|
304
|
+
* execution when the `concurrency` option is set.
|
|
305
|
+
*
|
|
306
|
+
* @param input An array or a promise that resolves to an array.
|
|
307
|
+
* @param callback Async function called for each element.
|
|
308
|
+
* @param options.concurrency Max concurrent promises (0 = unlimited).
|
|
309
|
+
*/
|
|
310
|
+
export function mapConcurrently<T, R>(
|
|
311
|
+
input: Promise<T[]> | T[],
|
|
312
|
+
callback: (value: T, index: number, array: T[]) => Promise<R> | R,
|
|
313
|
+
options?: {
|
|
314
|
+
concurrency?: number
|
|
315
|
+
}
|
|
316
|
+
): Promise<R[]>
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Maps an async callback over an array sequentially, waiting for each
|
|
320
|
+
* promise to resolve before processing the next element.
|
|
321
|
+
*
|
|
322
|
+
* @param input An array or a promise that resolves to an array.
|
|
323
|
+
* @param callback Async function called for each element.
|
|
324
|
+
*/
|
|
325
|
+
export function mapSequentially<T, R>(
|
|
326
|
+
input: Promise<T[]> | T[],
|
|
327
|
+
callback: (value: T, index: number) => Promise<R> | R
|
|
328
|
+
): Promise<R[]>
|
|
329
|
+
|
|
330
|
+
/* --------------------------------- string --------------------------------- */
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Converts a string separated by spaces, dashes and underscores to camel-case
|
|
334
|
+
* (`'camelCase'`) or optionally pascal-case (`'CamelCase'`).
|
|
335
|
+
*/
|
|
336
|
+
export function camelize(str: string, pascalCase?: boolean): string
|
|
337
|
+
/**
|
|
338
|
+
* Capitalizes words in a string.
|
|
339
|
+
*/
|
|
340
|
+
export function capitalize(str: string): string
|
|
341
|
+
/**
|
|
342
|
+
* Convert a string from camel-case to a string separated by spaces or a
|
|
343
|
+
* supplied separator string.
|
|
344
|
+
*
|
|
345
|
+
* @param str The string to decamelize.
|
|
346
|
+
* @param {string} [sep=' '] - The string to separate the decamelized words
|
|
347
|
+
* with.
|
|
348
|
+
*/
|
|
349
|
+
export function decamelize(str: string, sep?: string): string
|
|
350
|
+
/**
|
|
351
|
+
* Converts a camelized string to a string separated by dashes.
|
|
352
|
+
*/
|
|
353
|
+
export function hyphenate(str: string): string
|
|
354
|
+
/**
|
|
355
|
+
* Converts a camelized string to a string separated by underscores.
|
|
356
|
+
*/
|
|
357
|
+
export function underscore(str: string): string
|
|
358
|
+
/**
|
|
359
|
+
* ES6 string tag that strips indentation from multi-line strings.
|
|
360
|
+
*
|
|
361
|
+
* @example
|
|
362
|
+
* const str = deindent`Such a long string that you need to break it
|
|
363
|
+
* over multiple lines. Such a long string that
|
|
364
|
+
* you need to break it over multiple lines.`
|
|
365
|
+
* console.log(str)
|
|
366
|
+
*/
|
|
367
|
+
export function deindent(
|
|
368
|
+
strings: OrArrayOf<string>,
|
|
369
|
+
...values: Array<any>
|
|
370
|
+
): string
|
|
371
|
+
/**
|
|
372
|
+
* Escapes special characters in a string for use in a regular expression.
|
|
373
|
+
*/
|
|
374
|
+
export function escapeRegexp(string: string): string
|
|
375
|
+
/**
|
|
376
|
+
* Returns the longest prefix string that is common to the supplied strings.
|
|
377
|
+
*/
|
|
378
|
+
export function getCommonPrefix(...strings: Array<string>): string
|
|
379
|
+
/**
|
|
380
|
+
* Returns the offset of the longest prefix string that is common to the
|
|
381
|
+
* supplied strings.
|
|
382
|
+
*/
|
|
383
|
+
export function getCommonOffset(...strings: Array<string>): number
|
|
384
|
+
/**
|
|
385
|
+
* Determines whether the supplied string is an absolute url.
|
|
386
|
+
*
|
|
387
|
+
* A URL is considered absolute if it begins with "<scheme>://" or "//"
|
|
388
|
+
* (protocol-relative URL). RFC 3986 defines scheme name as a sequence of
|
|
389
|
+
* characters beginning with a letter and followed by any combination of
|
|
390
|
+
* letters, digits, plus, period, or hyphen.
|
|
391
|
+
*/
|
|
392
|
+
export function isAbsoluteUrl(str: string): boolean
|
|
393
|
+
/**
|
|
394
|
+
* Determines whether the supplied string is a valid creditcard number.
|
|
395
|
+
*/
|
|
396
|
+
export function isCreditCard(str: string): boolean
|
|
397
|
+
/**
|
|
398
|
+
* Determines whether the supplied string is a valid domain name.
|
|
399
|
+
* Supports internationalized domain names with punycode.
|
|
400
|
+
*/
|
|
401
|
+
export function isDomain(str: string): boolean
|
|
402
|
+
/**
|
|
403
|
+
* Determines whether the supplied string is a valid email address.
|
|
404
|
+
*/
|
|
405
|
+
export function isEmail(str: string): boolean
|
|
406
|
+
/**
|
|
407
|
+
* Determines whether the string is a valid hostname.
|
|
408
|
+
*/
|
|
409
|
+
export function isHostname(str: string): boolean
|
|
410
|
+
/**
|
|
411
|
+
* Determines whether the string is a valid url.
|
|
412
|
+
*/
|
|
413
|
+
export function isUrl(str: string): boolean
|
|
414
|
+
/**
|
|
415
|
+
* Expands hyphenated, underscored and camel-cased strings to title case.
|
|
416
|
+
*/
|
|
417
|
+
export function labelize(str: string | undefined): string
|
|
418
|
+
|
|
419
|
+
/* ---------------------------------- array --------------------------------- */
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Recursively flattens a nested array.
|
|
423
|
+
*
|
|
424
|
+
* @param array The array to recursively flatten.
|
|
425
|
+
* @param [maxDepth] The maximum recursion depth.
|
|
426
|
+
* @return Returns the new flattened array.
|
|
427
|
+
*/
|
|
428
|
+
export function flatten<T>(array: RecursiveArray<T>, maxDepth?: number): T[]
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Shuffle an array using the Fisher-Yates (aka Knuth) Shuffle.
|
|
432
|
+
*
|
|
433
|
+
* @param array The array to shuffle.
|
|
434
|
+
* @returns Returns the newly shuffled array.
|
|
435
|
+
*/
|
|
436
|
+
export function shuffle<T>(array: T[]): T[]
|
|
437
|
+
|
|
438
|
+
/* ---------------------------------- date ---------------------------------- */
|
|
439
|
+
|
|
440
|
+
export interface TimeFormat {
|
|
441
|
+
/**
|
|
442
|
+
* @defaultValue `'2-digit'`
|
|
443
|
+
*/
|
|
444
|
+
hour?: boolean | 'numeric' | '2-digit'
|
|
445
|
+
/**
|
|
446
|
+
* @defaultValue `'2-digit'`
|
|
447
|
+
*/
|
|
448
|
+
minute?: boolean | 'numeric' | '2-digit'
|
|
449
|
+
/**
|
|
450
|
+
* @defaultValue `'2-digit'`
|
|
451
|
+
*/
|
|
452
|
+
second?: boolean | 'numeric' | '2-digit'
|
|
453
|
+
|
|
454
|
+
format?: (
|
|
455
|
+
value: string,
|
|
456
|
+
type: Intl.DateTimeFormatPartTypes,
|
|
457
|
+
options: Omit<TimeFormat, 'format'>
|
|
458
|
+
) => string | undefined
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
export interface DateFormat {
|
|
462
|
+
/**
|
|
463
|
+
* @defaultValue `'numeric'`
|
|
464
|
+
*/
|
|
465
|
+
day?: boolean | 'numeric' | '2-digit'
|
|
466
|
+
/**
|
|
467
|
+
* @defaultValue `'long'`
|
|
468
|
+
*/
|
|
469
|
+
month?: boolean | 'numeric' | '2-digit' | 'long' | 'short' | 'narrow'
|
|
470
|
+
/**
|
|
471
|
+
* @defaultValue `'numeric'`
|
|
472
|
+
*/
|
|
473
|
+
year?: boolean | 'numeric' | '2-digit'
|
|
474
|
+
/**
|
|
475
|
+
* Format date in a custom way.
|
|
476
|
+
*/
|
|
477
|
+
format?: (
|
|
478
|
+
value: string,
|
|
479
|
+
type: Intl.DateTimeFormatPartTypes,
|
|
480
|
+
options: Omit<DateFormat, 'format'>
|
|
481
|
+
) => string | undefined
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
export interface NumberFormat extends Intl.NumberFormatOptions {
|
|
485
|
+
format?: (
|
|
486
|
+
value: string,
|
|
487
|
+
type: Intl.NumberFormatPartTypes,
|
|
488
|
+
options: Intl.NumberFormatOptions
|
|
489
|
+
) => string | undefined
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Default format options for number, date, and time formatting.
|
|
494
|
+
*/
|
|
495
|
+
export const defaultFormats: {
|
|
496
|
+
number: NumberFormat
|
|
497
|
+
date: DateFormat
|
|
498
|
+
time: TimeFormat
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Formats the value as a string.
|
|
503
|
+
*/
|
|
504
|
+
export function format(
|
|
505
|
+
value: Date | string | number | null | undefined,
|
|
506
|
+
options?: {
|
|
507
|
+
/**
|
|
508
|
+
* @default 'en-US'
|
|
509
|
+
*/
|
|
510
|
+
locale?: string
|
|
511
|
+
date?: boolean | DateFormat
|
|
512
|
+
time?: boolean | TimeFormat
|
|
513
|
+
number?: boolean | NumberFormat
|
|
514
|
+
defaults?: {
|
|
515
|
+
number?: NumberFormat
|
|
516
|
+
date?: DateFormat
|
|
517
|
+
time?: TimeFormat
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
): string | null | undefined
|
|
521
|
+
/**
|
|
522
|
+
* Formats a date value as a string. If the value is not a Date,
|
|
523
|
+
* attempts to convert it to a Date first.
|
|
524
|
+
*/
|
|
525
|
+
export function formatDate(
|
|
526
|
+
value: any,
|
|
527
|
+
options?: {
|
|
528
|
+
/**
|
|
529
|
+
* @default 'en-US'
|
|
530
|
+
*/
|
|
531
|
+
locale?: string
|
|
532
|
+
/**
|
|
533
|
+
* @default true
|
|
534
|
+
*/
|
|
535
|
+
date?: boolean | DateFormat
|
|
536
|
+
/**
|
|
537
|
+
* @default true
|
|
538
|
+
*/
|
|
539
|
+
time?: boolean | TimeFormat
|
|
540
|
+
}
|
|
541
|
+
): string | null | undefined
|
|
542
|
+
/* -------------------------------- function -------------------------------- */
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Logs a deprecation warning message to the console. Each unique message
|
|
546
|
+
* is only logged once.
|
|
547
|
+
*
|
|
548
|
+
* @param message The deprecation message to log.
|
|
549
|
+
*/
|
|
550
|
+
export function deprecate(message: string): void
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* Creates a debounced function that delays invoking func until after wait
|
|
554
|
+
* milliseconds have elapsed since the last time the debounced function was
|
|
555
|
+
* invoked. The debounced function comes with a cancel method to cancel
|
|
556
|
+
* delayed invocations. Provide an options object to indicate that func
|
|
557
|
+
* should be invoked on the leading edge of the wait timeout. Subsequent
|
|
558
|
+
* calls to the debounced function return the result of the last func
|
|
559
|
+
* invocation.
|
|
560
|
+
*
|
|
561
|
+
* @param func The function to debounce.
|
|
562
|
+
* @param options The options object or the number of milliseconds to delay.
|
|
563
|
+
* @param options.delay The number of milliseconds to delay.
|
|
564
|
+
* @param options.immediate Specify invoking on the leading edge of the timeout.
|
|
565
|
+
* @return Returns the new debounced function.
|
|
566
|
+
*/
|
|
567
|
+
export function debounce<T extends (...args: any[]) => any>(
|
|
568
|
+
func: T,
|
|
569
|
+
options:
|
|
570
|
+
| number
|
|
571
|
+
| {
|
|
572
|
+
delay: number
|
|
573
|
+
immediate?: boolean
|
|
574
|
+
}
|
|
575
|
+
): T & { cancel: () => boolean }
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Creates a debounced function that delays invoking func until after wait
|
|
579
|
+
* milliseconds have elapsed since the last time the debounced function was
|
|
580
|
+
* invoked.
|
|
581
|
+
*
|
|
582
|
+
* Each time the debounced function is called, it returns a promise
|
|
583
|
+
* which will resolve to the value that the debounced async function resolves
|
|
584
|
+
* to. If the async function throws an error, the last waiting promise is
|
|
585
|
+
* rejected and all others are resolved with undefined.
|
|
586
|
+
*
|
|
587
|
+
* The debounced function comes with a cancel method to cancel
|
|
588
|
+
* delayed invocations. Provide an options object to indicate that func
|
|
589
|
+
* should be invoked on the leading edge of the wait timeout.
|
|
590
|
+
*
|
|
591
|
+
* @param func The function to debounce.
|
|
592
|
+
* @param options The options object or the number of milliseconds to delay.
|
|
593
|
+
* @param options.delay The number of milliseconds to delay.
|
|
594
|
+
* @param options.immediate Specify invoking on the leading edge of the timeout.
|
|
595
|
+
* @return Returns the new debounced function.
|
|
596
|
+
*/
|
|
597
|
+
export function debounceAsync<T extends (...args: any[]) => Promise<any>>(
|
|
598
|
+
func: T,
|
|
599
|
+
options:
|
|
600
|
+
| number
|
|
601
|
+
| {
|
|
602
|
+
delay: number
|
|
603
|
+
immediate?: boolean
|
|
604
|
+
}
|
|
605
|
+
): T & { cancel: () => boolean }
|
|
606
|
+
|
|
607
|
+
// Adjusted version of @types/node's promisify (which includes support for
|
|
608
|
+
// overloaded node function types), excluding the custom promisified function
|
|
609
|
+
// functionality, which toAsync does not support:
|
|
610
|
+
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/fa723dee727e433add7e700420523f0f90e87a01/types/node/util.d.ts#L86
|
|
611
|
+
interface CustomPromisifyLegacy<TCustom extends Function> extends Function {
|
|
612
|
+
__promisify__: TCustom
|
|
613
|
+
}
|
|
614
|
+
type CustomToAsync<TCustom extends Function> = CustomPromisifyLegacy<TCustom>
|
|
615
|
+
/**
|
|
616
|
+
* Takes a function following the common error-first callback style, i.e.
|
|
617
|
+
* taking an (err, value) => ... callback as the last argument, and returns a
|
|
618
|
+
* version that returns promises.
|
|
619
|
+
*
|
|
620
|
+
* @param fn function following the error-first callback style
|
|
621
|
+
*/
|
|
622
|
+
export function toAsync<TCustom extends Function>(
|
|
623
|
+
fn: CustomToAsync<TCustom>
|
|
624
|
+
): TCustom
|
|
625
|
+
export function toAsync<R>(
|
|
626
|
+
fn: (callback: (err: any, result: R) => void) => void
|
|
627
|
+
): () => Promise<R>
|
|
628
|
+
export function toAsync(
|
|
629
|
+
fn: (callback: (err?: any) => void) => void
|
|
630
|
+
): () => Promise<void>
|
|
631
|
+
export function toAsync<T1, R>(
|
|
632
|
+
fn: (arg1: T1, callback: (err: any, result: R) => void) => void
|
|
633
|
+
): (arg1: T1) => Promise<R>
|
|
634
|
+
export function toAsync<T1>(
|
|
635
|
+
fn: (arg1: T1, callback: (err?: any) => void) => void
|
|
636
|
+
): (arg1: T1) => Promise<void>
|
|
637
|
+
export function toAsync<T1, T2, R>(
|
|
638
|
+
fn: (arg1: T1, arg2: T2, callback: (err: any, result: R) => void) => void
|
|
639
|
+
): (arg1: T1, arg2: T2) => Promise<R>
|
|
640
|
+
export function toAsync<T1, T2>(
|
|
641
|
+
fn: (arg1: T1, arg2: T2, callback: (err?: any) => void) => void
|
|
642
|
+
): (arg1: T1, arg2: T2) => Promise<void>
|
|
643
|
+
export function toAsync<T1, T2, T3, R>(
|
|
644
|
+
fn: (
|
|
645
|
+
arg1: T1,
|
|
646
|
+
arg2: T2,
|
|
647
|
+
arg3: T3,
|
|
648
|
+
callback: (err: any, result: R) => void
|
|
649
|
+
) => void
|
|
650
|
+
): (arg1: T1, arg2: T2, arg3: T3) => Promise<R>
|
|
651
|
+
export function toAsync<T1, T2, T3>(
|
|
652
|
+
fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err?: any) => void) => void
|
|
653
|
+
): (arg1: T1, arg2: T2, arg3: T3) => Promise<void>
|
|
654
|
+
export function toAsync<T1, T2, T3, T4, R>(
|
|
655
|
+
fn: (
|
|
656
|
+
arg1: T1,
|
|
657
|
+
arg2: T2,
|
|
658
|
+
arg3: T3,
|
|
659
|
+
arg4: T4,
|
|
660
|
+
callback: (err: any, result: R) => void
|
|
661
|
+
) => void
|
|
662
|
+
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<R>
|
|
663
|
+
export function toAsync<T1, T2, T3, T4>(
|
|
664
|
+
fn: (
|
|
665
|
+
arg1: T1,
|
|
666
|
+
arg2: T2,
|
|
667
|
+
arg3: T3,
|
|
668
|
+
arg4: T4,
|
|
669
|
+
callback: (err?: any) => void
|
|
670
|
+
) => void
|
|
671
|
+
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<void>
|
|
672
|
+
export function toAsync<T1, T2, T3, T4, T5, R>(
|
|
673
|
+
fn: (
|
|
674
|
+
arg1: T1,
|
|
675
|
+
arg2: T2,
|
|
676
|
+
arg3: T3,
|
|
677
|
+
arg4: T4,
|
|
678
|
+
arg5: T5,
|
|
679
|
+
callback: (err: any, result: R) => void
|
|
680
|
+
) => void
|
|
681
|
+
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise<R>
|
|
682
|
+
export function toAsync<T1, T2, T3, T4, T5>(
|
|
683
|
+
fn: (
|
|
684
|
+
arg1: T1,
|
|
685
|
+
arg2: T2,
|
|
686
|
+
arg3: T3,
|
|
687
|
+
arg4: T4,
|
|
688
|
+
arg5: T5,
|
|
689
|
+
callback: (err?: any) => void
|
|
690
|
+
) => void
|
|
691
|
+
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise<void>
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Takes an async function (or a function that returns a Promise) and returns
|
|
695
|
+
* a function following the error-first callback style, i.e. taking an
|
|
696
|
+
* (err, value) => ... callback as the last argument.
|
|
697
|
+
*/
|
|
698
|
+
export function toCallback(
|
|
699
|
+
fn: () => Promise<void>
|
|
700
|
+
): (callback: (err: any) => void) => void
|
|
701
|
+
export function toCallback<R>(
|
|
702
|
+
fn: () => Promise<R>
|
|
703
|
+
): (callback: (err: any, result: R) => void) => void
|
|
704
|
+
export function toCallback<T1>(
|
|
705
|
+
fn: (arg1: T1) => Promise<void>
|
|
706
|
+
): (arg1: T1, callback: (err: any) => void) => void
|
|
707
|
+
export function toCallback<T1, R>(
|
|
708
|
+
fn: (arg1: T1) => Promise<R>
|
|
709
|
+
): (arg1: T1, callback: (err: any, result: R) => void) => void
|
|
710
|
+
export function toCallback<T1, T2>(
|
|
711
|
+
fn: (arg1: T1, arg2: T2) => Promise<void>
|
|
712
|
+
): (arg1: T1, arg2: T2, callback: (err: any) => void) => void
|
|
713
|
+
export function toCallback<T1, T2, R>(
|
|
714
|
+
fn: (arg1: T1, arg2: T2) => Promise<R>
|
|
715
|
+
): (arg1: T1, arg2: T2, callback: (err: any, result: R) => void) => void
|
|
716
|
+
export function toCallback<T1, T2, T3>(
|
|
717
|
+
fn: (arg1: T1, arg2: T2, arg3: T3) => Promise<void>
|
|
718
|
+
): (arg1: T1, arg2: T2, arg3: T3, callback: (err: any) => void) => void
|
|
719
|
+
export function toCallback<T1, T2, T3, R>(
|
|
720
|
+
fn: (arg1: T1, arg2: T2, arg3: T3) => Promise<R>
|
|
721
|
+
): (
|
|
722
|
+
arg1: T1,
|
|
723
|
+
arg2: T2,
|
|
724
|
+
arg3: T3,
|
|
725
|
+
callback: (err: any, result: R) => void
|
|
726
|
+
) => void
|
|
727
|
+
export function toCallback<T1, T2, T3, T4>(
|
|
728
|
+
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<void>
|
|
729
|
+
): (
|
|
730
|
+
arg1: T1,
|
|
731
|
+
arg2: T2,
|
|
732
|
+
arg3: T3,
|
|
733
|
+
arg4: T4,
|
|
734
|
+
callback: (err: any) => void
|
|
735
|
+
) => void
|
|
736
|
+
export function toCallback<T1, T2, T3, T4, R>(
|
|
737
|
+
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<R>
|
|
738
|
+
): (
|
|
739
|
+
arg1: T1,
|
|
740
|
+
arg2: T2,
|
|
741
|
+
arg3: T3,
|
|
742
|
+
arg4: T4,
|
|
743
|
+
callback: (err: any, result: R) => void
|
|
744
|
+
) => void
|
|
745
|
+
export function toCallback<T1, T2, T3, T4, T5>(
|
|
746
|
+
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise<void>
|
|
747
|
+
): (
|
|
748
|
+
arg1: T1,
|
|
749
|
+
arg2: T2,
|
|
750
|
+
arg3: T3,
|
|
751
|
+
arg4: T4,
|
|
752
|
+
arg5: T5,
|
|
753
|
+
callback: (err: any) => void
|
|
754
|
+
) => void
|
|
755
|
+
export function toCallback<T1, T2, T3, T4, T5, R>(
|
|
756
|
+
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise<R>
|
|
757
|
+
): (
|
|
758
|
+
arg1: T1,
|
|
759
|
+
arg2: T2,
|
|
760
|
+
arg3: T3,
|
|
761
|
+
arg4: T4,
|
|
762
|
+
arg5: T5,
|
|
763
|
+
callback: (err: any, result: R) => void
|
|
764
|
+
) => void
|
|
765
|
+
export function toCallback<T1, T2, T3, T4, T5, T6>(
|
|
766
|
+
fn: (
|
|
767
|
+
arg1: T1,
|
|
768
|
+
arg2: T2,
|
|
769
|
+
arg3: T3,
|
|
770
|
+
arg4: T4,
|
|
771
|
+
arg5: T5,
|
|
772
|
+
arg6: T6
|
|
773
|
+
) => Promise<void>
|
|
774
|
+
): (
|
|
775
|
+
arg1: T1,
|
|
776
|
+
arg2: T2,
|
|
777
|
+
arg3: T3,
|
|
778
|
+
arg4: T4,
|
|
779
|
+
arg5: T5,
|
|
780
|
+
arg6: T6,
|
|
781
|
+
callback: (err: any) => void
|
|
782
|
+
) => void
|
|
783
|
+
export function toCallback<T1, T2, T3, T4, T5, T6, R>(
|
|
784
|
+
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => Promise<R>
|
|
785
|
+
): (
|
|
786
|
+
arg1: T1,
|
|
787
|
+
arg2: T2,
|
|
788
|
+
arg3: T3,
|
|
789
|
+
arg4: T4,
|
|
790
|
+
arg5: T5,
|
|
791
|
+
arg6: T6,
|
|
792
|
+
callback: (err: any, result: R) => void
|
|
793
|
+
) => void
|
|
794
|
+
|
|
795
|
+
/**
|
|
796
|
+
* Creates a Node.js-style error-first callback that resolves or rejects
|
|
797
|
+
* a promise. If the error argument is truthy, rejects the promise;
|
|
798
|
+
* otherwise resolves with the result.
|
|
799
|
+
*
|
|
800
|
+
* @param resolve Promise resolve function.
|
|
801
|
+
* @param reject Promise reject function.
|
|
802
|
+
* @returns Callback with signature `(err, res) => void`.
|
|
803
|
+
*/
|
|
804
|
+
export function toPromiseCallback<T, R>(
|
|
805
|
+
resolve: (value: T) => void,
|
|
806
|
+
reject: (reason: R) => void
|
|
807
|
+
): (err: R, res: T) => void
|
|
808
|
+
|
|
809
|
+
/* -------------------------------- dataPath -------------------------------- */
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* Gets entries at a data path, supporting wildcards (* and **).
|
|
813
|
+
* Wildcard * matches direct children, ** matches recursively.
|
|
814
|
+
*
|
|
815
|
+
* @param obj The object to query.
|
|
816
|
+
* @param path The data path (supports wildcards).
|
|
817
|
+
* @param handleError Optional error handler called when path is invalid.
|
|
818
|
+
* @returns Object with normalized paths as keys and values at those paths.
|
|
819
|
+
* @see {@link parseDataPath} for supported path formats.
|
|
820
|
+
*/
|
|
821
|
+
export function getEntriesAtDataPath(
|
|
822
|
+
obj: any,
|
|
823
|
+
path: OrArrayOf<string>,
|
|
824
|
+
handleError?: (obj: any, part: string, index: number) => any
|
|
825
|
+
): Record<string, any>
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* Retrieves a value from a nested object or array at a given path.
|
|
829
|
+
* Supports property access notation (`obj.arr[0].prop`), JSON pointers
|
|
830
|
+
* (`/obj/arr/0/prop`), and wildcard matching (`*` for shallow, `**`
|
|
831
|
+
* for deep recursive).
|
|
832
|
+
*
|
|
833
|
+
* @param obj The object or array to retrieve from.
|
|
834
|
+
* @param path The data path (multiple formats supported).
|
|
835
|
+
* @param handleError Optional error handler called when the path is
|
|
836
|
+
* invalid, with `(obj, part, index)`.
|
|
837
|
+
* @see {@link parseDataPath} for supported path formats.
|
|
838
|
+
*/
|
|
839
|
+
export function getValueAtDataPath(
|
|
840
|
+
obj: any,
|
|
841
|
+
path: OrArrayOf<string>,
|
|
842
|
+
handleError?: (obj: any, part: string, index: number) => any
|
|
843
|
+
): any
|
|
844
|
+
|
|
845
|
+
/**
|
|
846
|
+
* Normalizes a data path to a standard format using forward slashes.
|
|
847
|
+
* Converts property access notation, JSON pointers, and relative paths
|
|
848
|
+
* to normalized relative path format. Resolves relative tokens (`..`
|
|
849
|
+
* and `.`).
|
|
850
|
+
* @see {@link parseDataPath} for supported path formats.
|
|
851
|
+
*/
|
|
852
|
+
export function normalizeDataPath(path: OrArrayOf<string>): string
|
|
853
|
+
|
|
854
|
+
/**
|
|
855
|
+
* Parses a data path string or array into an array of path segments.
|
|
856
|
+
* Supports property access notation (`obj.arr[0].prop`), JSON pointers
|
|
857
|
+
* (`/obj/arr/0/prop`), and relative paths. Always returns a new array
|
|
858
|
+
* to prevent mutation.
|
|
859
|
+
*/
|
|
860
|
+
export function parseDataPath(path: OrArrayOf<string>): string[]
|
|
861
|
+
|
|
862
|
+
/**
|
|
863
|
+
* Sets multiple values at data paths from an entries object.
|
|
864
|
+
*
|
|
865
|
+
* @param obj The object to modify.
|
|
866
|
+
* @param entries Object with data paths as keys and values to set.
|
|
867
|
+
* @returns The modified object.
|
|
868
|
+
* @see {@link parseDataPath} for supported path formats.
|
|
869
|
+
*/
|
|
870
|
+
export function setDataPathEntries<O>(
|
|
871
|
+
obj: O,
|
|
872
|
+
entries: Record<string, any>
|
|
873
|
+
): O
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* Sets a value in a nested object or array at a given path. Parses the
|
|
877
|
+
* path, navigates to the parent location, and assigns the value.
|
|
878
|
+
* Mutates the original object.
|
|
879
|
+
*
|
|
880
|
+
* @param obj The object or array to set the value on.
|
|
881
|
+
* @param path The data path to the target location.
|
|
882
|
+
* @param value The value to set.
|
|
883
|
+
* @returns The modified object.
|
|
884
|
+
* @see {@link parseDataPath} for supported path formats.
|
|
885
|
+
*/
|
|
886
|
+
export function setValueAtDataPath<O>(
|
|
887
|
+
obj: O,
|
|
888
|
+
path: OrArrayOf<string>,
|
|
889
|
+
value: any
|
|
890
|
+
): O
|
|
891
|
+
|
|
892
|
+
/* --------------------------------- class ---------------------------------- */
|
|
893
|
+
|
|
894
|
+
/**
|
|
895
|
+
* Creates a mixin decorator that applies a mixin function to a class.
|
|
896
|
+
* Prevents duplicate application of the same mixin in the inheritance chain.
|
|
897
|
+
*
|
|
898
|
+
* @param mixinFunction Function that takes a class and returns a mixed class.
|
|
899
|
+
* @returns Decorator function that applies the mixin to a target class.
|
|
900
|
+
*/
|
|
901
|
+
export function mixin<T extends new (...args: any[]) => any>(
|
|
902
|
+
mixinFunction: (targetClass: T) => T
|
|
903
|
+
): (targetClass: T) => T
|
|
904
|
+
|
|
905
|
+
/* ---------------------------------- html ---------------------------------- */
|
|
906
|
+
|
|
907
|
+
/**
|
|
908
|
+
* Escapes double quotes, ampersands, and angle brackets (`"&<>`).
|
|
909
|
+
|
|
910
|
+
* @param html The html to escape.
|
|
911
|
+
* @returns The newly escaped html.
|
|
912
|
+
*/
|
|
913
|
+
export function escapeHtml(html: string | null | undefined): string
|
|
914
|
+
|
|
915
|
+
/**
|
|
916
|
+
* Strips HTML tags from the string.
|
|
917
|
+
* @param html The string to strip.
|
|
918
|
+
* @returns The newly stripped string.
|
|
919
|
+
*/
|
|
920
|
+
export function stripHtml(html: string | null | undefined): string
|
|
921
|
+
|
|
922
|
+
/**
|
|
923
|
+
* Strips HTML tags from the string.
|
|
924
|
+
* @param html The string to strip.
|
|
925
|
+
* @returns The newly stripped string.
|
|
926
|
+
* @deprecated Use stripHtml() instead
|
|
927
|
+
*/
|
|
928
|
+
export function stripTags(html: string | null | undefined): string
|
|
929
|
+
|
|
930
|
+
/* -------------------------- typescript utilities -------------------------- */
|
|
931
|
+
export interface ArrayLike<T> {
|
|
932
|
+
readonly length: number
|
|
933
|
+
readonly [n: number]: T
|
|
934
|
+
}
|
|
935
|
+
export interface Dictionary<T> {
|
|
936
|
+
[index: string]: T
|
|
937
|
+
}
|
|
938
|
+
export type RecursiveArray<T> = Array<T | RecursiveArray<T>>
|
|
939
|
+
type OrArrayOf<T> = T | T[]
|