@superutils/core 1.2.0 → 1.2.1
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 +18 -7
- package/dist/index.d.ts +1176 -935
- package/dist/index.js +11 -9
- package/package.json +2 -1
package/dist/index.d.ts
CHANGED
|
@@ -4,14 +4,16 @@
|
|
|
4
4
|
type AsyncFn<TOut = unknown, TArgs extends unknown[] = []> = (...args: TArgs) => Promise<Awaited<TOut>>;
|
|
5
5
|
/**
|
|
6
6
|
* Create a tuple of specific type with given length
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* #### Create a new tuple
|
|
9
10
|
* ```typescript
|
|
10
11
|
* type CreatedTuple = CreateTuple<number, 3>
|
|
11
12
|
* // Result: [number, number, number]
|
|
12
13
|
* ```
|
|
13
14
|
*
|
|
14
|
-
* @example
|
|
15
|
+
* @example
|
|
16
|
+
* #### Create a new tuple by extending an existing tuple
|
|
15
17
|
* ```typescript
|
|
16
18
|
* type ExtendedTuple = CreateTuple<string, 6, CreatedTuple>
|
|
17
19
|
* // Result: [number, number, number, string, string, string]
|
|
@@ -30,8 +32,8 @@ type CurriedArgs<TArgs extends unknown[], TArgsIsFinite extends boolean, TFunc e
|
|
|
30
32
|
], TArity>;
|
|
31
33
|
/**
|
|
32
34
|
* Drop the first item from an array/tuple and keep the rest
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
35
37
|
* ```typescript
|
|
36
38
|
* type MyTuple = [first: string, second: number, third: boolean]
|
|
37
39
|
* type MyTupleWOFirst = DropFirst<MyTuple> // result: [second: number, third: boolean]
|
|
@@ -40,8 +42,8 @@ type CurriedArgs<TArgs extends unknown[], TArgsIsFinite extends boolean, TFunc e
|
|
|
40
42
|
type DropFirst<T extends unknown[]> = T extends [unknown, ...infer Rest] ? Rest : [];
|
|
41
43
|
/**
|
|
42
44
|
* Drop first N items from an array/tuple and keep the rest
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
45
47
|
* ```typescript
|
|
46
48
|
* type MyTuple = [first: string, second: number, third: boolean]
|
|
47
49
|
* type MyTupleWO2 = DropFirstN<MyTuple, 2> // result: [third: boolean]
|
|
@@ -50,8 +52,8 @@ type DropFirst<T extends unknown[]> = T extends [unknown, ...infer Rest] ? Rest
|
|
|
50
52
|
type DropFirstN<T extends unknown[], N extends number, Dropped extends unknown[] = []> = TupleMaxLength<Dropped> extends N ? T : T extends [infer First, ...infer Rest] ? DropFirstN<Rest, N, [...Dropped, First]> : never;
|
|
51
53
|
/**
|
|
52
54
|
* Drop the last item from an array/tuple and keep the rest
|
|
53
|
-
*
|
|
54
|
-
* @example
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
55
57
|
* ```typescript
|
|
56
58
|
* type MyTuple = [first: string, second: number, third: boolean]
|
|
57
59
|
* type MyTupleWOLast = DropLast<MyTuple> // result: [first: string, second: number]
|
|
@@ -69,8 +71,8 @@ type IsFiniteTuple<T extends unknown[]> = number extends T['length'] ? false : t
|
|
|
69
71
|
type IsOptional<T, K extends keyof T> = {} extends Pick<T, K> ? true : false;
|
|
70
72
|
/**
|
|
71
73
|
* Keep the first item from an array/tuple and drop the rest
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
74
76
|
* ```typescript
|
|
75
77
|
* type MyTuple = [first: string, second: number, third: boolean]
|
|
76
78
|
* type MyTupleWFirst = KeepFirst<MyTuple> // result: [first: string]
|
|
@@ -82,8 +84,8 @@ type KeepFirst<T extends unknown[]> = T extends readonly [
|
|
|
82
84
|
] ? [First] : never;
|
|
83
85
|
/**
|
|
84
86
|
* Keep first N items from an array/tuple and drop the rest
|
|
85
|
-
*
|
|
86
|
-
* @example
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
87
89
|
* ```typescript
|
|
88
90
|
* type MyTuple = [first: string, second: number, third: boolean]
|
|
89
91
|
* type MyTupleWith1st2 = KeepFirstN<MyTuple, 2> // result: [first: string, second: number]
|
|
@@ -98,7 +100,7 @@ type KeepFirstN<T extends readonly unknown[], N extends number = 1> = TupleMaxLe
|
|
|
98
100
|
* Defaults to `false`
|
|
99
101
|
* @template TAlt (optional) Defaults to `undefined`
|
|
100
102
|
*
|
|
101
|
-
* @example
|
|
103
|
+
* @example
|
|
102
104
|
* ```typescript
|
|
103
105
|
* import { KeepOptionals } from '@superutils/core
|
|
104
106
|
* type MyTuple = [first: string, second?: number, third?: boolean]
|
|
@@ -126,8 +128,8 @@ type MakeOptional<Tuple extends unknown[], IndexStart extends number, IndexEnd e
|
|
|
126
128
|
] ? [...Left, ...Partial<Slice<Tuple, IndexStart>>, ...Right] : never;
|
|
127
129
|
/**
|
|
128
130
|
* Create a new slices tuple from an existing tuple
|
|
129
|
-
*
|
|
130
|
-
* @example
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
131
133
|
* ```typescript
|
|
132
134
|
* type MyTuple = [a: string, b: boolean, c: number, d: Record<string, unknown>]
|
|
133
135
|
* type FirstHalf = Slice<MyTuple, 0, 2>
|
|
@@ -145,8 +147,7 @@ type TimeoutId = Parameters<typeof clearTimeout>[0];
|
|
|
145
147
|
*
|
|
146
148
|
* This is particularly useful when a tuple (or function paramenters) contains optional members.
|
|
147
149
|
*
|
|
148
|
-
*
|
|
149
|
-
* @example usage
|
|
150
|
+
* @example
|
|
150
151
|
* ```typescript
|
|
151
152
|
* type MyTuple = [string, number?, boolean?]
|
|
152
153
|
* type Lengths = MyTuple['length'] // 1 | 2 | 3 // union because of optional parameters
|
|
@@ -157,8 +158,7 @@ type TupleMaxLength<T extends readonly unknown[]> = Required<T>['length'];
|
|
|
157
158
|
/**
|
|
158
159
|
* Add alt type to all members of a tuple.
|
|
159
160
|
*
|
|
160
|
-
*
|
|
161
|
-
* @example usage
|
|
161
|
+
* @example
|
|
162
162
|
* ```typescript
|
|
163
163
|
* type MyTuple = [first: boolean, second: string]
|
|
164
164
|
* type MyTupleWithUndefined = TupleWithAlt<MyTuple>
|
|
@@ -173,9 +173,7 @@ type TupleWithAlt<Tuple extends unknown[], TAlt = undefined> = {
|
|
|
173
173
|
/**
|
|
174
174
|
* Accept value or a function that returns the value
|
|
175
175
|
*
|
|
176
|
-
*
|
|
177
|
-
* ---
|
|
178
|
-
* @example usage
|
|
176
|
+
* @example
|
|
179
177
|
* ```typescript
|
|
180
178
|
* import { isFn, ValueOrFunc } from '@superutils/core'
|
|
181
179
|
* const print = (value: ValueOrFunc<string>) => isFn(value)
|
|
@@ -197,21 +195,20 @@ type ValueOrPromise<T> = T | Promise<T>;
|
|
|
197
195
|
* called with one or more or all arguments at a time. Once all arguments have been
|
|
198
196
|
* supplied, the original function is executed.
|
|
199
197
|
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
* PS:
|
|
198
|
+
* Note:
|
|
203
199
|
* ---
|
|
204
200
|
* To get around Typescript's limitations, all optional parameters to will be
|
|
205
201
|
* turned required and unioned with `undefined`.
|
|
206
202
|
*
|
|
207
|
-
* ---
|
|
208
|
-
*
|
|
209
203
|
* @param func The function to curry.
|
|
210
204
|
* @returns A new, curried function that is fully type-safe.
|
|
211
205
|
|
|
212
206
|
*
|
|
213
|
-
* @example
|
|
207
|
+
* @example
|
|
208
|
+
* #### Convert any function into a curried function
|
|
214
209
|
* ```typescript
|
|
210
|
+
* import { curry } from '@superutils/core'
|
|
211
|
+
*
|
|
215
212
|
* const func = (
|
|
216
213
|
* first: string,
|
|
217
214
|
* second: number,
|
|
@@ -231,24 +228,51 @@ type ValueOrPromise<T> = T | Promise<T>;
|
|
|
231
228
|
* const fnWith3 = fnWith2(false)
|
|
232
229
|
* // All args are now provided, the original function is called and result is returned.
|
|
233
230
|
* const result = fnWith3('last')
|
|
231
|
+
* console.log({ result })
|
|
232
|
+
* // Result: 'first-2-false-last'
|
|
234
233
|
* ```
|
|
235
234
|
*
|
|
236
|
-
* @example
|
|
235
|
+
* @example
|
|
236
|
+
* #### Flexible curried function arguments
|
|
237
237
|
* ```typescript
|
|
238
|
+
* import { curry } from '@superutils/core'
|
|
239
|
+
*
|
|
240
|
+
* const func = (
|
|
241
|
+
* first: string,
|
|
242
|
+
* second: number,
|
|
243
|
+
* third?: boolean,
|
|
244
|
+
* fourth?: string
|
|
245
|
+
* ) => `${first}-${second}-${third}-${fourth}`
|
|
246
|
+
* const curriedFunc = curry(func)
|
|
247
|
+
*
|
|
238
248
|
* // Provide as many arguments as you wish. Upto the limit of the original function.
|
|
239
249
|
* // Returns a function expecting only the remaining argument(s)
|
|
240
250
|
* const fnWith3 = curriedFunc('first', 2, false)
|
|
241
251
|
* // All args provided, returns the result
|
|
242
252
|
* const result = fnWith3('last')
|
|
253
|
+
* console.log({ result })
|
|
254
|
+
* // Result: 'first-2-false-last'
|
|
243
255
|
* ```
|
|
244
256
|
*
|
|
245
|
-
* @example
|
|
257
|
+
* @example
|
|
258
|
+
* #### Early invocation using "arity".
|
|
246
259
|
* Useful when a function has
|
|
247
260
|
* - non-finite arguments (eg: number[])
|
|
248
261
|
* - optional arguments and you do not want to avoid one or more optional arguments
|
|
262
|
+
*
|
|
249
263
|
* ```typescript
|
|
264
|
+
* import { curry } from '@superutils/core'
|
|
265
|
+
*
|
|
266
|
+
* const func = (
|
|
267
|
+
* first: string,
|
|
268
|
+
* second: number,
|
|
269
|
+
* third?: boolean,
|
|
270
|
+
* fourth?: string
|
|
271
|
+
* ) => `${first}-${second}-${third}-${fourth}`
|
|
250
272
|
* const curriedWArity3 = curry(func, 3)
|
|
251
273
|
* const result = curriedWArity3('first', 2, false)
|
|
274
|
+
* console.log({ result })
|
|
275
|
+
* // Result: 'first-2-false-undefined'
|
|
252
276
|
* ```
|
|
253
277
|
*/
|
|
254
278
|
declare function curry<TData, TArgs extends unknown[], TArgsIsFinite extends boolean = IsFiniteTuple<TArgs>, TArity extends TArgs['length'] = TArgsIsFinite extends true ? TupleMaxLength<TArgs> : number>(func: (...args: TArgs) => TData, ...[arity]: TArgsIsFinite extends true ? [arity?: TArity] : [
|
|
@@ -270,17 +294,16 @@ declare function curry<TData, TArgs extends unknown[], TArgsIsFinite extends boo
|
|
|
270
294
|
* function can still be wrapped inside a secondary `fallbackIfFails`. See the "Fallback chaining" example below.
|
|
271
295
|
* - If `target` a promise or async function, `fallback` can be either be a value, a promise or an async function.
|
|
272
296
|
*
|
|
273
|
-
* @returns
|
|
297
|
+
* @returns A `Promise` is returned if the `target` is a `Promise` or returns one.
|
|
298
|
+
* Otherwise, the direct value is returned.
|
|
274
299
|
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
* @example Async functions
|
|
278
|
-
* Working with async functions or functions that returns a promise
|
|
300
|
+
* @example
|
|
301
|
+
* #### Async functions: working with async functions or functions that returns a promise
|
|
279
302
|
*
|
|
280
303
|
* ```typescript
|
|
281
304
|
* import { fallbackIfFails } from '@superutils/core'
|
|
282
305
|
*
|
|
283
|
-
* const args = ['some value', true]
|
|
306
|
+
* const args = ['some value', true]
|
|
284
307
|
* const ensureValue = async (value: string, criteria?: boolean) => {
|
|
285
308
|
* if (criteria !== false && !value.trim()) throw new Error('No value. Should use fallback value')
|
|
286
309
|
* return value
|
|
@@ -288,18 +311,19 @@ declare function curry<TData, TArgs extends unknown[], TArgsIsFinite extends boo
|
|
|
288
311
|
* // This makes sure there's always a value without having to manually write try-catch block.
|
|
289
312
|
* const value = await fallbackIfFails(
|
|
290
313
|
* ensureValue,
|
|
291
|
-
* () => args
|
|
314
|
+
* () => args as Parameters<typeof ensureValue>,
|
|
292
315
|
* async () => 'fallback value'
|
|
293
316
|
* )
|
|
317
|
+
* console.log({ value }) // 'some value'
|
|
294
318
|
* ```
|
|
295
319
|
*
|
|
296
|
-
* @example
|
|
297
|
-
*
|
|
320
|
+
* @example
|
|
321
|
+
* #### Non-async functions: working synchronous function that returns value synchronously
|
|
298
322
|
*
|
|
299
323
|
* ```typescript
|
|
300
324
|
* import { fallbackIfFails } from '@superutils/core'
|
|
301
325
|
*
|
|
302
|
-
* const args = ['some value', true]
|
|
326
|
+
* const args = ['some value', true]
|
|
303
327
|
* const ensureValue = (value: string, criteria?: boolean) => {
|
|
304
328
|
* if (criteria !== false && !value.trim()) throw new Error('No value. Should use fallback value')
|
|
305
329
|
* return value
|
|
@@ -307,13 +331,14 @@ declare function curry<TData, TArgs extends unknown[], TArgsIsFinite extends boo
|
|
|
307
331
|
* // this makes sure there's always a value without having to manually write try-catch block.
|
|
308
332
|
* const value = fallbackIfFails(
|
|
309
333
|
* ensureValue,
|
|
310
|
-
* () => args
|
|
334
|
+
* () => args as Parameters<typeof ensureValue>,
|
|
311
335
|
* () => 'fallback value'
|
|
312
336
|
* )
|
|
337
|
+
* console.log({ value }) // 'some value'
|
|
313
338
|
* ```
|
|
314
339
|
*
|
|
315
|
-
* @example
|
|
316
|
-
*
|
|
340
|
+
* @example
|
|
341
|
+
* #### Hybrid functions: working with function that returns value sync/async circumstantially
|
|
317
342
|
*
|
|
318
343
|
* ```typescript
|
|
319
344
|
* import { fallbackIfFails } from '@superutils/core'
|
|
@@ -328,14 +353,18 @@ declare function curry<TData, TArgs extends unknown[], TArgsIsFinite extends boo
|
|
|
328
353
|
* })
|
|
329
354
|
* }
|
|
330
355
|
* // First call: no cache, will execute fetch and return a promise
|
|
331
|
-
* const first = await fallbackIfFails(getData, [false], {})
|
|
356
|
+
* const first = await fallbackIfFails(getData, [false], { fallback: true })
|
|
357
|
+
* console.log({ first }) // { fallback: true }
|
|
358
|
+
*
|
|
332
359
|
* // Second call: cache available and will return data synchronously
|
|
333
|
-
* const second = fallbackIfFails(getData, [true], {})
|
|
360
|
+
* const second = fallbackIfFails(getData, [true], { fallback: true })
|
|
361
|
+
* console.log({ second }) // { fallback: true }
|
|
334
362
|
* ```
|
|
335
363
|
*
|
|
336
|
-
* @example
|
|
364
|
+
* @example
|
|
365
|
+
* #### Fallback-chaining: gracefully handle the fallback function
|
|
337
366
|
*
|
|
338
|
-
* ```
|
|
367
|
+
* ```javascript
|
|
339
368
|
* import { fallbackIfFails } from '@superutils/core'
|
|
340
369
|
*
|
|
341
370
|
* const target = () => {
|
|
@@ -348,1087 +377,1300 @@ declare function curry<TData, TArgs extends unknown[], TArgsIsFinite extends boo
|
|
|
348
377
|
* target,
|
|
349
378
|
* [],
|
|
350
379
|
* // this function will only be invoked when
|
|
351
|
-
* () => fallbackIfFails(fallback, [],
|
|
380
|
+
* () => fallbackIfFails(fallback, [], 'fallback')
|
|
352
381
|
* )
|
|
353
382
|
*
|
|
354
|
-
* console.log({ value }) //
|
|
383
|
+
* console.log({ value }) // 'fallback'
|
|
355
384
|
* ```
|
|
356
385
|
*/
|
|
357
386
|
declare const fallbackIfFails: <T, TArgs extends unknown[] = unknown[], TFB = T, TFBVal = TFB extends (...args: unknown[]) => infer V ? V : TFB, Result = (T extends Promise<unknown> ? true : never) extends never ? T | TFBVal : Promise<Awaited<T | TFBVal>>>(target: T | ((...args: TArgs) => T), args: TArgs | (() => TArgs), fallback: IfPromiseAddValue<TFB> | ((reason: unknown) => IfPromiseAddValue<TFB>)) => Result;
|
|
358
387
|
|
|
359
|
-
/**
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
388
|
+
/** Configuration for finding {@link IterableList} items */
|
|
389
|
+
type FindOptions<K, V, IncludeKey extends boolean = false> = Omit<SearchOptions<K, V>, 'limit' | 'asMap'> & {
|
|
390
|
+
/**
|
|
391
|
+
* Whether to include the key in the return type.
|
|
392
|
+
*
|
|
393
|
+
* If `true`, return type is `[K, V]` else `V`
|
|
394
|
+
*
|
|
395
|
+
* Default: `false`
|
|
396
|
+
*/
|
|
397
|
+
includeKey?: IncludeKey;
|
|
398
|
+
};
|
|
399
|
+
/** A general type to capture all iterables like Array, Map, Set.... */
|
|
400
|
+
type IterableList<K = unknown, V = unknown> = {
|
|
401
|
+
entries: () => IterableIterator<[K, V]>;
|
|
402
|
+
hasOwnProperty: (name: string) => boolean;
|
|
403
|
+
keys: () => IterableIterator<K>;
|
|
404
|
+
values: () => IterableIterator<V>;
|
|
405
|
+
} & ({
|
|
406
|
+
clear: () => void;
|
|
407
|
+
size: number;
|
|
408
|
+
} | {
|
|
409
|
+
length: number;
|
|
410
|
+
});
|
|
411
|
+
/** Configuration for sorting iterables */
|
|
412
|
+
type SortOptions = {
|
|
413
|
+
/** Whether to treat the value as string. Default: `true` */
|
|
414
|
+
asString?: boolean;
|
|
415
|
+
/** case-insensitive sort for strings. Default: `true` */
|
|
416
|
+
ignoreCase?: boolean;
|
|
417
|
+
/**
|
|
418
|
+
* Whether to create a new instance of preserve original reference
|
|
419
|
+
*
|
|
420
|
+
* Default: `true` for Array, `false` for Map.
|
|
421
|
+
*/
|
|
422
|
+
newInstance?: boolean;
|
|
423
|
+
/** Reverse sorted result */
|
|
424
|
+
reverse?: boolean;
|
|
425
|
+
/**
|
|
426
|
+
* Whether to place undefined/null values at the beginning of the sorted array.
|
|
427
|
+
*
|
|
428
|
+
* Default: `false`
|
|
429
|
+
*/
|
|
430
|
+
undefinedFirst?: boolean;
|
|
431
|
+
};
|
|
432
|
+
/** Search criteria for searcheing iterables */
|
|
433
|
+
type SearchOptions<K, V, MatchExact extends boolean = false, AsMap extends boolean = false> = {
|
|
434
|
+
/** Whethere to return the result as a map (`true`) or array (`false`). Default: `true` */
|
|
435
|
+
asMap?: AsMap;
|
|
436
|
+
/** case-insensitive search for strings. Default: `false` */
|
|
437
|
+
ignoreCase?: boolean;
|
|
438
|
+
/** limit number of results. Default: `Infinity` */
|
|
439
|
+
limit?: number;
|
|
440
|
+
/** partial match for values. Default: `false` */
|
|
441
|
+
matchExact?: MatchExact;
|
|
442
|
+
/** match all supplied key-value pairs. Default: `false` */
|
|
443
|
+
matchAll?: boolean;
|
|
444
|
+
/** key-value pairs */
|
|
445
|
+
query: Record<PropertyKey, unknown> | RegExp | string;
|
|
446
|
+
/** If `true`, the results are sorted by relevance (match index). Default: `false` */
|
|
447
|
+
ranked?: boolean;
|
|
448
|
+
/** Map to store results in. Default: `new Map()` */
|
|
449
|
+
result?: Map<K, V>;
|
|
450
|
+
/**
|
|
451
|
+
* Boolean or Callback to prepare item or individual property for search by converting to string.
|
|
452
|
+
*
|
|
453
|
+
* - `true`: value will be stringified
|
|
454
|
+
* - `false`: value will not be stringified when `matchExact = true`
|
|
455
|
+
* - `function`: transformed value will be used to search
|
|
456
|
+
*
|
|
457
|
+
* Returning "empty" (`undefined | null | [] | '' | ...`) value will ignore the item/property.
|
|
458
|
+
*
|
|
459
|
+
* Default: `true`
|
|
460
|
+
*/
|
|
461
|
+
transform?: boolean | ((
|
|
462
|
+
/** List item */
|
|
463
|
+
item: V,
|
|
464
|
+
/** Item property value or `undefined` for global search across all properties. */
|
|
465
|
+
value?: V[keyof V],
|
|
466
|
+
/** Item property key provided by query or `undefined` for global search across all properties. */
|
|
467
|
+
key?: keyof V) => MatchExact extends true ? unknown : string | undefined);
|
|
468
|
+
};
|
|
391
469
|
|
|
392
470
|
/**
|
|
393
|
-
*
|
|
471
|
+
* Filter {@link IterableList} (Array, Map, Set) items.
|
|
394
472
|
*
|
|
395
|
-
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
398
|
-
*
|
|
399
|
-
*
|
|
400
|
-
*
|
|
473
|
+
* @param data
|
|
474
|
+
* @param predicate callback function to filter values
|
|
475
|
+
* Parameters:
|
|
476
|
+
* 1. `item`: current item
|
|
477
|
+
* 2. `key`: index/key
|
|
478
|
+
* 3. `data`: value provided in the first argument (`data`)
|
|
479
|
+
* @param limit (optional) limit number of results
|
|
480
|
+
* @param asArray
|
|
401
481
|
*
|
|
402
|
-
* @
|
|
403
|
-
* @param nonNumerable (optional) when `true`, considers non-enumerable properties
|
|
404
|
-
* while checking objects for emptiness. Default: `false`
|
|
405
|
-
* @param fallback (optional) value to return when type is unrecognized.
|
|
406
|
-
* Default: `false`
|
|
482
|
+
* @returns new Map with filtered items
|
|
407
483
|
*
|
|
408
|
-
* @example
|
|
484
|
+
* @example
|
|
409
485
|
* ```typescript
|
|
410
|
-
* import {
|
|
411
|
-
* isEmpty('') // true
|
|
412
|
-
* isEmpty(' ') // true
|
|
413
|
-
* isEmpty(`
|
|
414
|
-
*
|
|
415
|
-
*
|
|
416
|
-
* `) // true
|
|
417
|
-
* isEmpty(' not empty ') // false
|
|
418
|
-
* isEmpty(`
|
|
486
|
+
* import { filter } from '@superutils/core'
|
|
419
487
|
*
|
|
420
|
-
*
|
|
488
|
+
* const map = new Map<number, { name: string; age: number }>([
|
|
489
|
+
* [1, { name: 'Alice', age: 30 }],
|
|
490
|
+
* [2, { name: 'Bob', age: 25 }],
|
|
491
|
+
* [3, { name: 'Charlie', age: 35 }],
|
|
492
|
+
* ])
|
|
421
493
|
*
|
|
422
|
-
*
|
|
494
|
+
* const filtered = filter(map, item => item.age >= 30)
|
|
495
|
+
* console.log({ filtered })
|
|
496
|
+
* // result: Map(2) {
|
|
497
|
+
* // 1 => { name: 'Alice', age: 30 },
|
|
498
|
+
* // 3 => { name: 'Charlie', age: 35 }
|
|
499
|
+
* // }
|
|
423
500
|
* ```
|
|
501
|
+
*/
|
|
502
|
+
declare const filter: <K, V, AsArray extends boolean = false, Result = AsArray extends true ? V[] : Map<K, V>>(data: IterableList<K, V>, predicate: (item: V, key: K, data: IterableList<K, V>) => boolean, limit?: number, asArray?: AsArray, result?: Map<K, V>) => Result;
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Finds a first item using predicate in an iterable list (Array, Map or Set).
|
|
424
506
|
*
|
|
425
|
-
* @
|
|
426
|
-
* ```typescript
|
|
427
|
-
* import { isEmpty } from '@superutils/core'
|
|
428
|
-
* isEmpty(NaN) // true
|
|
429
|
-
* isEmpty(Infinity) // true
|
|
430
|
-
* isEmpty(0) // false
|
|
431
|
-
* ```
|
|
507
|
+
* @returns first item matched or `undefined` if not found
|
|
432
508
|
*
|
|
509
|
+
* @example
|
|
510
|
+
* #### Find item using predicate callback
|
|
433
511
|
*
|
|
434
|
-
* @example check objects (includes arrays, maps & sets)
|
|
435
512
|
* ```typescript
|
|
436
|
-
* import {
|
|
437
|
-
*
|
|
438
|
-
*
|
|
439
|
-
*
|
|
440
|
-
*
|
|
513
|
+
* import { find } from '@superutils/core'
|
|
514
|
+
*
|
|
515
|
+
* const map = new Map<number, { name: string; age: number }>([
|
|
516
|
+
* [1, { name: 'Alice', age: 30 }],
|
|
517
|
+
* [2, { name: 'Bob', age: 25 }],
|
|
518
|
+
* [3, { name: 'Charlie', age: 35 }],
|
|
519
|
+
* ])
|
|
520
|
+
* const result = find(map, ({ name }) => name === 'Bob')
|
|
521
|
+
* console.log({ result })
|
|
522
|
+
* // result: { name: 'Bob', age: 25 }
|
|
441
523
|
* ```
|
|
442
524
|
*/
|
|
443
|
-
declare
|
|
525
|
+
declare function find<K, V extends Record<string, unknown>, IncludeKey extends boolean = false, Return = undefined | (IncludeKey extends true ? [K, V] : V)>(data: IterableList<K, V>, predicate: Parameters<typeof filter<K, V>>[1]): Return;
|
|
444
526
|
/**
|
|
445
|
-
*
|
|
446
|
-
*
|
|
447
|
-
* CAUTION: much slower than {@link isEmpty} due to use of Object.prototype.toString.call()
|
|
527
|
+
* Find the first item in an iterable list (Array, Map or Set) using `search()` function
|
|
448
528
|
*
|
|
449
|
-
*
|
|
450
|
-
*
|
|
529
|
+
* @param data items to search
|
|
530
|
+
* @param options filter options. See {@link FindOptions}
|
|
451
531
|
*
|
|
532
|
+
* @example
|
|
533
|
+
* #### Find item using search options
|
|
452
534
|
*
|
|
453
|
-
*
|
|
535
|
+
* ```typescript
|
|
536
|
+
* import { find } from '@superutils/core'
|
|
454
537
|
*
|
|
455
|
-
*
|
|
538
|
+
* const map = new Map<number, { name: string; age: number }>([
|
|
539
|
+
* [1, { name: 'Alice', age: 30 }],
|
|
540
|
+
* [2, { name: 'Bob', age: 25 }],
|
|
541
|
+
* [3, { name: 'Charlie', age: 35 }],
|
|
542
|
+
* ])
|
|
543
|
+
* const result = find(map, {
|
|
544
|
+
* query: { name: 'Bob' }
|
|
545
|
+
* })
|
|
546
|
+
* console.log({ result })
|
|
547
|
+
* // result: { name: 'Bob', age: 25 }
|
|
548
|
+
* ```
|
|
456
549
|
*/
|
|
457
|
-
declare
|
|
550
|
+
declare function find<K, V extends Record<string, unknown>, IncludeKey extends boolean = false, Return = undefined | (IncludeKey extends true ? [K, V] : V)>(data: IterableList<K, V>, options: FindOptions<K, V, IncludeKey>): Return;
|
|
458
551
|
|
|
459
|
-
/**
|
|
460
|
-
declare const
|
|
461
|
-
/** Check if the environment is NodeJS */
|
|
462
|
-
declare const isEnvNode: () => boolean;
|
|
463
|
-
/** Check if page is loaded on a touchscreen device */
|
|
464
|
-
declare const isEnvTouchable: () => boolean;
|
|
552
|
+
/** Map entries as array */
|
|
553
|
+
declare const getEntries: <K, V>(map: Map<K, V>) => [K, V][];
|
|
465
554
|
|
|
466
|
-
/**
|
|
467
|
-
declare const
|
|
468
|
-
/**
|
|
469
|
-
* Check if value is an Async function.
|
|
470
|
-
* Caution: May not work at runtime when Babel/Webpack is used due to code compilation.
|
|
471
|
-
*
|
|
472
|
-
* ---
|
|
473
|
-
* @example usage
|
|
474
|
-
* ```typescript
|
|
475
|
-
* isAsyncFn(async () => {}) // result: true
|
|
476
|
-
* isAsyncFn(() => {}) // result: false
|
|
477
|
-
* ```
|
|
478
|
-
*/
|
|
479
|
-
declare const isAsyncFn: <TData = unknown, TArgs extends unknown[] = unknown[]>(x: unknown) => x is AsyncFn<TData, TArgs>;
|
|
555
|
+
/** Get {@link IterableList} keys as array */
|
|
556
|
+
declare const getKeys: <K, V>(data: IterableList<K, V>) => K[];
|
|
480
557
|
|
|
481
|
-
/**
|
|
482
|
-
declare const
|
|
483
|
-
/** Check if provided is a Map and all values are objects */
|
|
484
|
-
declare const isMapObj: <K extends PropertyKey, V, T extends Record<K, V>>(x: unknown, strict?: boolean) => x is Map<K, V>;
|
|
558
|
+
/** Get size/length of Array/Map/Set */
|
|
559
|
+
declare const getSize: (x: IterableList) => number;
|
|
485
560
|
|
|
486
|
-
/**
|
|
487
|
-
declare const
|
|
488
|
-
/** Check if value is a valid negative integer */
|
|
489
|
-
declare const isNegativeInteger: (x: unknown) => x is number;
|
|
490
|
-
/** Check if value is a valid negative number */
|
|
491
|
-
declare const isNegativeNumber: (x: unknown) => x is number;
|
|
492
|
-
/** Check if value is a valid finite number */
|
|
493
|
-
declare const isNumber: (x: unknown) => x is number;
|
|
494
|
-
/** Check if value is a positive integer */
|
|
495
|
-
declare const isPositiveInteger: (x: unknown) => x is number;
|
|
496
|
-
/** Check if value is a positive number */
|
|
497
|
-
declare const isPositiveNumber: (x: unknown) => x is number;
|
|
561
|
+
/** Get Map values as Array */
|
|
562
|
+
declare const getValues: <K, V>(data: IterableList<K, V>) => V[];
|
|
498
563
|
|
|
499
564
|
/**
|
|
500
|
-
*
|
|
565
|
+
* Reverse a {@link IterableList} (Array/Map/Set) conditionally
|
|
501
566
|
*
|
|
567
|
+
* @param data
|
|
568
|
+
* @param reverse (optional) condition to reverse the list. Default: `true`
|
|
569
|
+
* @param newInstance (optional) whether to return a new instance of the list. Default: `false`
|
|
502
570
|
*
|
|
503
|
-
* @
|
|
504
|
-
|
|
571
|
+
* @returns reversed data in original type or empty array for unsupported type
|
|
572
|
+
*/
|
|
573
|
+
declare const reverse: <K, V, T extends IterableList<K, V>>(data: T, reverse?: boolean, newInstance?: boolean) => V[] | [K, V][] | Map<K, V> | Set<V> | (T & Record<"clear", unknown>);
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* A versatile utility for searching through an iterable list (Array, Map, or Set) of objects.
|
|
577
|
+
* It supports both a global search (using a string or RegExp) across all properties of an item, and a
|
|
578
|
+
* detailed, field-specific search using a query object.
|
|
505
579
|
*
|
|
506
|
-
*
|
|
580
|
+
* @param data The list of objects to search within. Compatible types include:
|
|
581
|
+
* - `Array`
|
|
582
|
+
* - `Map`
|
|
583
|
+
* - `Set`
|
|
584
|
+
* - `NodeList` (in DOM environments): `options.transform()` required
|
|
585
|
+
* - `HTMLCollection` (in DOM environments): should accompany `options.transform()`
|
|
586
|
+
* @param options The search criteria.
|
|
587
|
+
* @param options.query The search query. Can be a string to search all fields, or an object for field-specific
|
|
588
|
+
* @param options.ranked (optional) If `true`, the results are sorted by relevance (match index). Default: `false`.
|
|
507
589
|
*
|
|
508
|
-
*
|
|
509
|
-
*
|
|
510
|
-
*
|
|
590
|
+
* searches (e.g., `{ name: 'John', city: 'New York' }`).
|
|
591
|
+
* @param options.asMap (optional) If `true`, returns a `Map`. If `false`, returns an `Array`. Default: `true`.
|
|
592
|
+
* @param options.ignoreCase (optional) If `true`, performs a case-insensitive search for strings. Default: `true`.
|
|
593
|
+
* @param options.limit (optional) The maximum number of results to return. Default: `Infinity`.
|
|
594
|
+
* @param options.matchAll (optional) If `true`, an item must match all key-value pairs in the `query` object. If
|
|
595
|
+
* `false`, it matches if at least one pair is found. Default: `false`.
|
|
596
|
+
* @param options.matchExact (optional) If `true`, performs an exact match. If `false`, performs a partial match
|
|
597
|
+
* (i.e., `includes()`). Default: `false`.
|
|
598
|
+
* @param options.result (optional) An optional `Map` to which the results will be added.
|
|
599
|
+
* @param options.transform (optional) Callback to transform item/item-property to string
|
|
511
600
|
*
|
|
512
|
-
*
|
|
513
|
-
* - all of above
|
|
514
|
-
* - buit-in objects like Date, Error, Map, Set, Uint8Array etc
|
|
515
|
-
* - custom Class instances
|
|
516
|
-
* - objects created using `Object.create(object)`
|
|
601
|
+
* @returns A `Map` or an `Array` containing the matched items, based on the `asMap` option.
|
|
517
602
|
*
|
|
518
|
-
*
|
|
519
|
-
*
|
|
520
|
-
*
|
|
603
|
+
* @example
|
|
604
|
+
* ```javascript
|
|
605
|
+
* import { search } from '@superutils/core'
|
|
521
606
|
*
|
|
522
|
-
*
|
|
523
|
-
*
|
|
524
|
-
*
|
|
607
|
+
* const users = [
|
|
608
|
+
* { id: 1, name: 'John Doe', city: 'New York' },
|
|
609
|
+
* { id: 2, name: 'Jane Doe', city: 'London' },
|
|
610
|
+
* { id: 3, name: 'Peter Jones', city: 'New York' },
|
|
611
|
+
* ]
|
|
525
612
|
*
|
|
526
|
-
*
|
|
527
|
-
*
|
|
528
|
-
* console.log(
|
|
529
|
-
*
|
|
530
|
-
*
|
|
531
|
-
*
|
|
532
|
-
*
|
|
533
|
-
*
|
|
534
|
-
*
|
|
535
|
-
*
|
|
536
|
-
*
|
|
537
|
-
*
|
|
538
|
-
*
|
|
539
|
-
*
|
|
613
|
+
* // Simple string search (case-insensitive, partial match by default)
|
|
614
|
+
* const doeUsers = search(users, { query: 'doe' })
|
|
615
|
+
* console.log({ doeUsers })
|
|
616
|
+
* // Map(2) {
|
|
617
|
+
* // 0 => { id: 1, name: 'John Doe', city: 'New York' },
|
|
618
|
+
* // 1 => { id: 2, name: 'Jane Doe', city: 'London' }
|
|
619
|
+
* // }
|
|
620
|
+
* // }
|
|
621
|
+
*
|
|
622
|
+
* // Field-specific search, requiring all fields to match
|
|
623
|
+
* const peterInNY = search(users, {
|
|
624
|
+
* asMap: false, // return result as array
|
|
625
|
+
* query: { name: 'Peter', city: 'New York' },
|
|
626
|
+
* matchAll: true,
|
|
627
|
+
* })
|
|
628
|
+
* console.log({ peterInNY })
|
|
629
|
+
* // [ { id: 3, name: 'Peter Jones', city: 'New York' } ]
|
|
540
630
|
* ```
|
|
541
631
|
*/
|
|
542
|
-
declare const
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
632
|
+
declare const search: {
|
|
633
|
+
<K, V, MatchExact extends boolean = false, AsMap extends boolean = true, Result = AsMap extends true ? Map<K, V> : V[]>(data: IterableList<K, V>, options: SearchOptions<K, V, MatchExact, AsMap>): Result;
|
|
634
|
+
defaults: Pick<Required<SearchOptions<unknown, unknown, false, true>>, "matchAll" | "limit" | "asMap" | "ignoreCase" | "ranked" | "transform">;
|
|
635
|
+
};
|
|
546
636
|
/**
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
* @param x The value to check.
|
|
550
|
-
* @param strict If true:
|
|
551
|
-
* - requires a domain name with a TLDs etc.
|
|
552
|
-
* - and x is string, catches any auto-correction (eg: trailing spaces being removed, spaces being replaced by `%20`)
|
|
553
|
-
* by `new URL()` to ensure string URL is valid
|
|
554
|
-
* Defaults to `true`.
|
|
555
|
-
* @param tldExceptions when in strict mode, treat these hosts as valid despite no domain name extensions
|
|
556
|
-
* Defaults to `['localhost']`
|
|
637
|
+
* Internal utility for use with {@link search} function
|
|
557
638
|
*
|
|
558
|
-
* @returns
|
|
639
|
+
* @returns match index (`-1` means didn't match)
|
|
559
640
|
*/
|
|
560
|
-
declare
|
|
641
|
+
declare function matchObjOrProp<K, V>(// extends Record<string, unknown>
|
|
642
|
+
{ query, ignoreCase, matchExact, transform, }: Pick<SearchOptions<K, V, boolean>, 'transform' | 'query' | 'ignoreCase' | 'matchExact'>, item: V, propertyName?: string): number;
|
|
561
643
|
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
/**
|
|
565
|
-
|
|
566
|
-
/**
|
|
567
|
-
|
|
568
|
-
/**
|
|
569
|
-
|
|
570
|
-
/**
|
|
571
|
-
|
|
572
|
-
/**
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
declare const isStr: (x: unknown) => x is string;
|
|
644
|
+
type SliceMapTransform<Data, Value, Key> = (item: Value, key: Key, data: Data) => Value;
|
|
645
|
+
type SliceMapOptions<Data, Value, Key, AsMap extends boolean = false> = {
|
|
646
|
+
/** Whether to return the result as a Map (preserving original keys) or an Array */
|
|
647
|
+
asMap?: AsMap;
|
|
648
|
+
/** Callback to transform each item from the selected range */
|
|
649
|
+
transform?: SliceMapTransform<Data, Value, Key>;
|
|
650
|
+
/** End index (exclusive). Default: `undefined` (end of the list) */
|
|
651
|
+
end?: number;
|
|
652
|
+
/** Whether to exclude item if value is `undefined | null` */
|
|
653
|
+
ignoreEmpty?: boolean;
|
|
654
|
+
/** Start index. Default: `0` */
|
|
655
|
+
start?: number;
|
|
656
|
+
};
|
|
576
657
|
/**
|
|
577
|
-
*
|
|
658
|
+
* Slice an iterable list and map the values into an Array/Map.
|
|
578
659
|
*
|
|
579
|
-
* @param
|
|
580
|
-
* @param
|
|
660
|
+
* @param data Array, Map, Set...
|
|
661
|
+
* @param options One of the following is required to create a new list:
|
|
662
|
+
* 1. A callback function {@link SliceMapTransform} to transform all items.
|
|
663
|
+
* 2. Advanced options {@link SliceMapOptions}.
|
|
664
|
+
* @param options.asMap (optional) whether return a Map or Array.
|
|
665
|
+
* @param options.end (optional) End index (exclusive). Default: `undefined` (end of the list)
|
|
666
|
+
* @param options.ignoreEmpty (optional) Whether to exclude item if value is `undefined | null`
|
|
667
|
+
* @param options.start (optional) Default: `0`
|
|
668
|
+
* @param options.transform (optional)
|
|
581
669
|
*
|
|
582
|
-
*
|
|
583
|
-
*/
|
|
584
|
-
declare const isSubjectLike: <T>(x: unknown, withValue?: boolean) => boolean;
|
|
585
|
-
/** Check if value is a Symbol */
|
|
586
|
-
declare const isSymbol: (x: unknown) => x is symbol;
|
|
587
|
-
/**
|
|
588
|
-
* Compilation of all the compile-time & runtime utilities functions above
|
|
589
|
-
*/
|
|
590
|
-
declare const is: {
|
|
591
|
-
arr: <Item = unknown>(x: unknown) => x is Item[];
|
|
592
|
-
arr2D: <Item = unknown>(x: unknown) => x is Item[][];
|
|
593
|
-
arrLike: (x: any) => x is typeof x extends (infer Value)[] ? Value[] : typeof x extends Set<infer Value> ? Set<Value> : typeof x extends Map<infer Key, infer Value> ? Map<Key, Value> : never;
|
|
594
|
-
arrLikeSafe: <T = unknown, MapKey = unknown>(x: unknown) => x is Set<T> | Map<MapKey, T> | T[];
|
|
595
|
-
arrObj: <K extends PropertyKey, V, T extends Record<K, V>[]>(x: unknown, strict?: boolean) => x is T[];
|
|
596
|
-
arrUnique: <T = unknown>(arr: T[]) => boolean;
|
|
597
|
-
asyncFn: <TData = unknown, TArgs extends unknown[] = unknown[]>(x: unknown) => x is AsyncFn<TData, TArgs>;
|
|
598
|
-
bool: (x: unknown) => x is boolean;
|
|
599
|
-
date: (x: unknown) => x is Date;
|
|
600
|
-
dateValid: (date: unknown) => boolean;
|
|
601
|
-
defined: <T = unknown>(x: T | undefined | null) => x is T;
|
|
602
|
-
empty: (x: unknown, nonNumerable?: boolean, fallback?: boolean | 0 | 1) => boolean | 0 | 1;
|
|
603
|
-
emptySafe: (x: unknown, numberableOnly?: boolean) => boolean | 1;
|
|
604
|
-
envBrowser: () => boolean;
|
|
605
|
-
envNode: () => boolean;
|
|
606
|
-
envTouchable: () => boolean;
|
|
607
|
-
error: (x: unknown) => x is Error;
|
|
608
|
-
fn: <T extends (...args: any[]) => any>(x: unknown) => x is T;
|
|
609
|
-
integer: (x: unknown) => x is number;
|
|
610
|
-
map: <TKey = unknown, TValue = unknown>(x: unknown) => x is Map<TKey, TValue>;
|
|
611
|
-
mapObj: <K extends PropertyKey, V, T extends Record<K, V>>(x: unknown, strict?: boolean) => x is Map<K, V>;
|
|
612
|
-
negativeInteger: (x: unknown) => x is number;
|
|
613
|
-
negativeNumber: (x: unknown) => x is number;
|
|
614
|
-
number: (x: unknown) => x is number;
|
|
615
|
-
obj: <T = Record<PropertyKey, unknown>>(x: unknown, strict?: boolean) => x is T;
|
|
616
|
-
positiveInteger: (x: unknown) => x is number;
|
|
617
|
-
positiveNumber: (x: unknown) => x is number;
|
|
618
|
-
promise: <T = unknown>(x: unknown) => x is Promise<T>;
|
|
619
|
-
regExp: (x: unknown) => x is RegExp;
|
|
620
|
-
set: <T = unknown>(x: unknown) => x is Set<T>;
|
|
621
|
-
str: (x: unknown) => x is string;
|
|
622
|
-
subjectLike: <T>(x: unknown, withValue?: boolean) => boolean;
|
|
623
|
-
symbol: (x: unknown) => x is symbol;
|
|
624
|
-
uint8Arr: (arr: unknown) => arr is Uint8Array<ArrayBufferLike>;
|
|
625
|
-
url: (x: unknown) => x is URL;
|
|
626
|
-
urlValid: (x: unknown, strict?: boolean, tldExceptions?: string[]) => boolean;
|
|
627
|
-
};
|
|
628
|
-
|
|
629
|
-
/** Placeholder function that does nothing */
|
|
630
|
-
declare function noop(): void;
|
|
631
|
-
declare function noopAsync(): Promise<void>;
|
|
632
|
-
|
|
633
|
-
/**
|
|
634
|
-
* Convert timestamp to `input["datetime-local"]` compatible format.
|
|
670
|
+
* If callback throws error or returnes `undefined`, the item will be ignored.
|
|
635
671
|
*
|
|
636
|
-
*
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
-
*
|
|
640
|
-
* // result: "2000-01-01T01:01" // assuming local timezone is UTC+0
|
|
641
|
-
* ```
|
|
672
|
+
* Callback Params:
|
|
673
|
+
* - item: current item
|
|
674
|
+
* - key: index/key of the current item
|
|
675
|
+
* - data: original list
|
|
642
676
|
*
|
|
643
|
-
* @
|
|
644
|
-
* ```typescript
|
|
645
|
-
* const date = new Date('2000-01-01T01:01:01.001Z')
|
|
646
|
-
* toDatetimeLocal(date)
|
|
647
|
-
* // result: "2000-01-01T01:01" // assuming local timezone is UTC+0
|
|
648
|
-
* ```
|
|
677
|
+
* @returns Array/Map depending on `options.asMap`
|
|
649
678
|
*
|
|
650
|
-
* @example
|
|
651
|
-
*
|
|
652
|
-
*
|
|
653
|
-
*
|
|
654
|
-
* // result: "2000-01-01T01:01" // assuming local timezone is UTC+0
|
|
655
|
-
* ```
|
|
656
|
-
*/
|
|
657
|
-
declare const toDatetimeLocal: (dateStr: string | Date | number) => "" | `${number}-${number}-${number}T${number}:${number}`;
|
|
658
|
-
|
|
659
|
-
/**
|
|
660
|
-
* Constructs a new object with only the supplied property names (keys) and their respective values
|
|
679
|
+
* @example
|
|
680
|
+
* #### Slice a list of items and map values into new array
|
|
681
|
+
* ```javascript
|
|
682
|
+
* import { sliceMap } from '@superutils/core'
|
|
661
683
|
*
|
|
662
|
-
*
|
|
663
|
-
*
|
|
664
|
-
*
|
|
684
|
+
* const data = new Map([
|
|
685
|
+
* [0, { age: 30, name: 'Alice' }],
|
|
686
|
+
* [1, { age: 25, name: 'Bob' }],
|
|
687
|
+
* [2, undefined],
|
|
688
|
+
* [3, {}],
|
|
689
|
+
* [4, { age: 35, name: 'Charlie' }],
|
|
690
|
+
* [5, { age: 28, name: 'Dave' }],
|
|
691
|
+
* [6, { age: 22, name: 'Eve' }],
|
|
692
|
+
* ])
|
|
693
|
+
* const result = sliceMap(data, {
|
|
694
|
+
* asMap: false, // whether to return the result as a Map
|
|
695
|
+
* end: 5, // last index (exclusive)
|
|
696
|
+
* ignoreEmpty: true, // ignore items with no value
|
|
697
|
+
* start: 1, // first index
|
|
698
|
+
* })
|
|
699
|
+
* console.log(result)
|
|
700
|
+
* // [ { age: 25, name: 'Bob' }, { age: 35, name: 'Charlie' } ]
|
|
701
|
+
* ```
|
|
665
702
|
*/
|
|
666
|
-
declare const
|
|
703
|
+
declare const sliceMap: <Data extends IterableList, Key = Data extends IterableList<infer Key_1, unknown> ? Key_1 : never, Value = Data extends IterableList<unknown, infer Value_1> ? Value_1 : never, AsMap extends boolean = false, Result = AsMap extends false ? Value[] : Map<Key, Value>>(data: Data, options?: SliceMapOptions<Data, Value, Key, AsMap> | SliceMapTransform<Data, Value, Key>) => Result;
|
|
667
704
|
|
|
705
|
+
type EntryComparator<K, V> = (a: [K, V], b: [K, V]) => number;
|
|
706
|
+
type ArrayComparator<V> = (a: V, b: V) => number;
|
|
668
707
|
/**
|
|
669
|
-
*
|
|
670
|
-
*
|
|
671
|
-
* @param input input object
|
|
672
|
-
* @param output (optional) output object
|
|
673
|
-
* @param ignoreKeys (optional) input peroperties to be ignored. Prevents output's property to be overriden.
|
|
674
|
-
*
|
|
675
|
-
* For child object properties use "." (dot) separated path.
|
|
708
|
+
* Sort iterable lists (Array/Map/Set).
|
|
676
709
|
*
|
|
677
|
-
* Eg: `"child.grandchild1"` where input is `{ child: { grandchild1: 1, grandchild2: 2 }}`
|
|
678
710
|
*
|
|
679
|
-
* @param
|
|
680
|
-
*
|
|
681
|
-
*
|
|
682
|
-
* - `
|
|
683
|
-
* - `
|
|
684
|
-
*
|
|
685
|
-
*
|
|
686
|
-
*
|
|
687
|
-
*
|
|
688
|
-
*
|
|
689
|
-
*
|
|
690
|
-
*
|
|
711
|
+
* @param data
|
|
712
|
+
* @param propertyName Accepted values:
|
|
713
|
+
* - `string`: value object property name
|
|
714
|
+
* - `function`: comparator function. Recommended for performance.
|
|
715
|
+
* - `true`: indicates to sort by Map keys instead of values.
|
|
716
|
+
* @param options (optional) extra sorting opitons
|
|
717
|
+
* @param options.asString (optonal) whether to convert values to string for sorting purposes only. Default: `true`
|
|
718
|
+
* @param options.ignoreCase (optional) case-insensitive sort for strings. Default: `true`
|
|
719
|
+
* @param options.reverse (optional) True: accending sort. False: descending sort. Default: `false`
|
|
720
|
+
* @param options.undefinedFirst (optional) Where to place undefined/null values.
|
|
721
|
+
* Not avaible when `comparator` function is used.
|
|
722
|
+
* - `true`: at the beginning
|
|
723
|
+
* - `false`: at the end
|
|
691
724
|
*
|
|
692
725
|
* Default: `false`
|
|
693
726
|
*
|
|
694
|
-
* @
|
|
727
|
+
* @returns sorted map
|
|
695
728
|
*
|
|
729
|
+
* @example
|
|
730
|
+
* #### Sort map of objects
|
|
731
|
+
* ```javascript
|
|
732
|
+
* import { sort } from '@superutils/core'
|
|
696
733
|
*
|
|
697
|
-
*
|
|
734
|
+
* const map = new Map([
|
|
735
|
+
* [0, { name: 'Charlie' }],
|
|
736
|
+
* [1, { name: 'Alice' }],
|
|
737
|
+
* [2, { name: 'Bob' }],
|
|
738
|
+
* ])
|
|
739
|
+
* console.log(sort(map, 'name'))
|
|
740
|
+
* // result: Map(3) {
|
|
741
|
+
* // 1 => { name: 'Alice' },
|
|
742
|
+
* // 2 => { name: 'Bob' },
|
|
743
|
+
* // 0 => { name: 'Charlie' }
|
|
744
|
+
* // }
|
|
745
|
+
* ```
|
|
698
746
|
*/
|
|
699
|
-
declare
|
|
700
|
-
|
|
747
|
+
declare function sort<K, V extends Record<PropertyKey, unknown>, T extends IterableList<K, V>>(data: T, propertyName: keyof V & string, options?: SortOptions): T;
|
|
701
748
|
/**
|
|
702
|
-
*
|
|
703
|
-
* It pairs each key with the value at the same index.
|
|
704
|
-
*
|
|
705
|
-
* @param keys An array of property keys (strings or symbols).
|
|
706
|
-
* @param values (optional) An array of property values. The value at each index corresponds to the key at the same index. If a value is missing for a key, it will be `undefined`.
|
|
707
|
-
* @param result (optional) An existing object to add or overwrite properties on. If not provided, a new object is created.
|
|
708
|
-
* @returns The newly created object, or the `result` object merged with the new properties.
|
|
749
|
+
* Sort `Map` by map-keys `K`
|
|
709
750
|
*
|
|
710
|
-
* @example
|
|
711
|
-
*
|
|
712
|
-
*
|
|
713
|
-
*
|
|
714
|
-
* const newObj = objCreate(keys, values);
|
|
715
|
-
* // newObj is { a: 1, b: 2, c: 3 }
|
|
716
|
-
* ```
|
|
751
|
+
* @example
|
|
752
|
+
* #### Sort map of simple values (string/number/boolean)
|
|
753
|
+
* ```javascript
|
|
754
|
+
* import { sort } from '@superutils/core'
|
|
717
755
|
*
|
|
718
|
-
*
|
|
719
|
-
*
|
|
720
|
-
*
|
|
721
|
-
*
|
|
722
|
-
*
|
|
723
|
-
*
|
|
724
|
-
* //
|
|
756
|
+
* const map = new Map([
|
|
757
|
+
* [1, 1],
|
|
758
|
+
* [2, 2],
|
|
759
|
+
* [0, 0],
|
|
760
|
+
* ])
|
|
761
|
+
* console.log(sort(map, false, { asString: false }))
|
|
762
|
+
* // result: Map(3) { 0 => 0, 1 => 1, 2 => 2 }
|
|
725
763
|
* ```
|
|
726
764
|
*/
|
|
727
|
-
declare
|
|
765
|
+
declare function sort<K, V>(data: Map<K, V>,
|
|
766
|
+
/** Whether to sort by key or Value */
|
|
767
|
+
byKey?: boolean, options?: SortOptions): Map<K, V>;
|
|
768
|
+
/** Sort `Map` with comparator function */
|
|
769
|
+
declare function sort<K, V>(map: Map<K, V>, comparator: EntryComparator<K, V>, options?: SortOptions): Map<K, V>;
|
|
770
|
+
/** Sort `Array` with comparator function */
|
|
771
|
+
declare function sort<V>(array: V[], comparator: ArrayComparator<V>, options?: SortOptions): V[];
|
|
772
|
+
/** Sort `Set` with comparator function */
|
|
773
|
+
declare function sort<V>(set: Set<V>, comparator: ArrayComparator<V>, options?: SortOptions): Set<V>;
|
|
774
|
+
/** Sort Array/Map/Set with `string | boolean | number` values */
|
|
775
|
+
declare function sort<K, V extends string | boolean | number, T extends IterableList<K, V>>(data: T, options?: SortOptions): T;
|
|
776
|
+
declare namespace sort {
|
|
777
|
+
var defaults: {
|
|
778
|
+
asString: true;
|
|
779
|
+
ignoreCase: true;
|
|
780
|
+
newInstance: false;
|
|
781
|
+
reverse: false;
|
|
782
|
+
undefinedFirst: false;
|
|
783
|
+
};
|
|
784
|
+
}
|
|
728
785
|
|
|
786
|
+
/** Check if value is an array */
|
|
787
|
+
declare const isArr: <Item = unknown>(x: unknown) => x is Item[];
|
|
729
788
|
/**
|
|
730
|
-
*
|
|
731
|
-
*
|
|
732
|
-
* @param input
|
|
733
|
-
* @param keys
|
|
734
|
-
* @param requireValue (optional) whether each property should have some value.
|
|
789
|
+
* Check if value is an array of objects
|
|
735
790
|
*/
|
|
736
|
-
declare
|
|
737
|
-
|
|
791
|
+
declare const isArrObj: <K extends PropertyKey, V, T extends Record<K, V>[]>(x: unknown, strict?: boolean) => x is T[];
|
|
792
|
+
/** Check if argument is a 2-dimentional array */
|
|
793
|
+
declare const isArr2D: <T = unknown>(x: unknown) => x is T[][];
|
|
794
|
+
/** Check if value is convertible to an array by using `Array.from(x)` */
|
|
795
|
+
declare const isArrLike: <K = unknown, V = unknown>(x: unknown) => x is IterableList<K, V>;
|
|
738
796
|
/**
|
|
739
|
-
*
|
|
797
|
+
* Check if value is convertible to an array by using `Array.from(x)` even if it comes from a different realm
|
|
798
|
+
* (eg: iframe, iframes, worker contexts, node vm contexts, browser extensions).
|
|
740
799
|
*
|
|
741
|
-
* @
|
|
742
|
-
* @param sorted (optional) Whether to sort the keys. Default: `true`
|
|
743
|
-
* @param includeSymbols (optional) Whether to include `Symbol` object. Default: `false`
|
|
800
|
+
* Caution: much slower than {@link isArrLike} due to use of `Object.prototype.toString.call()`
|
|
744
801
|
*/
|
|
745
|
-
declare const
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
add?: boolean | ReadOnlyAllowAddFn<T>;
|
|
751
|
-
/** Default: `false` */
|
|
752
|
-
revocable?: Revocable;
|
|
753
|
-
/** Whether to throw error when a write operation is rejected. Default: `true` */
|
|
754
|
-
silent?: boolean;
|
|
755
|
-
};
|
|
802
|
+
declare const isArrLikeSafe: <K = unknown, V = unknown>(x: unknown) => x is IterableList<K, V>;
|
|
803
|
+
/** Check if all values in the array are unique */
|
|
804
|
+
declare const isArrUnique: <T = unknown>(x: unknown) => x is T[];
|
|
805
|
+
/** Check if value is instance of Uint8Array */
|
|
806
|
+
declare const isUint8Arr: (x: unknown) => x is Uint8Array;
|
|
756
807
|
|
|
808
|
+
/** Check if value is instance of Date */
|
|
809
|
+
declare const isDate: (x: unknown) => x is Date;
|
|
757
810
|
/**
|
|
758
|
-
*
|
|
759
|
-
*
|
|
760
|
-
* Applies only to the top-level properties.
|
|
811
|
+
* Check if a value is a valid date.
|
|
761
812
|
*
|
|
762
|
-
* @param
|
|
763
|
-
* @param config (optional) extra configuration
|
|
764
|
-
* @param config.add (optional) Whether to allow adding new properties. Default: `false`
|
|
765
|
-
* @param config.revocable (optional) Whether to create a revokable proxy. Default: `false`
|
|
766
|
-
* @param config.silent (optional) whether to throw error when a write operation is rejected. Default: `false`
|
|
813
|
+
* @param date The value to check. Can be a Date object or a string.
|
|
767
814
|
*
|
|
768
|
-
* @returns
|
|
815
|
+
* @returns `true` if the value is a valid date, `false` otherwise.
|
|
769
816
|
*/
|
|
770
|
-
declare const
|
|
817
|
+
declare const isDateValid: (date: unknown) => boolean;
|
|
771
818
|
|
|
772
819
|
/**
|
|
773
|
-
*
|
|
820
|
+
* Check if variable contains empty, null-ish value.
|
|
774
821
|
*
|
|
775
|
-
*
|
|
776
|
-
*
|
|
777
|
-
*
|
|
778
|
-
*
|
|
779
|
-
*
|
|
780
|
-
|
|
781
|
-
declare const objSetProp: <K extends PropertyKey, V, OutKey extends K | string>(obj: Record<K, V>, key: OutKey, falsyValue?: V, condition?: boolean | ((value: V | undefined, key: OutKey, obj: Record<K, V>) => boolean), truthyValue?: V) => Record<OutKey, V>;
|
|
782
|
-
/** Assign value to an object property only if current value is undefined */
|
|
783
|
-
declare const objSetPropUndefined: (...[obj, key, ...args]: Parameters<typeof objSetProp>) => Record<PropertyKey, unknown>;
|
|
784
|
-
|
|
785
|
-
/** create a new object with properties sorted by key */
|
|
786
|
-
declare const objSort: <T>(obj: T, recursive?: boolean, _done?: Map<unknown, boolean>) => T;
|
|
787
|
-
|
|
788
|
-
/**
|
|
789
|
-
* Creates a new object excluding specific properties
|
|
822
|
+
* Depending on the type certain criteria applies:
|
|
823
|
+
* - `null` | `undefined`: always empty
|
|
824
|
+
* - `String`: empty text or only white-spaces
|
|
825
|
+
* - `Number`: non-finite or NaN
|
|
826
|
+
* - `Array` | `Uint8Array` | `Map` | `Set` | `Object`: contains zero items/properties
|
|
827
|
+
* - `Boolean` | `Function` | `Symbol` | `BigInt`: never empty
|
|
790
828
|
*
|
|
791
|
-
* @param
|
|
792
|
-
* @param
|
|
793
|
-
*
|
|
794
|
-
*
|
|
829
|
+
* @param x The value to check for emptiness.
|
|
830
|
+
* @param nonNumerable (optional) when `true`, considers non-enumerable properties
|
|
831
|
+
* while checking objects for emptiness. Default: `false`
|
|
832
|
+
* @param fallback (optional) value to return when type is unrecognized.
|
|
833
|
+
* Default: `false`
|
|
795
834
|
*
|
|
796
|
-
* @
|
|
835
|
+
* @example
|
|
836
|
+
* #### Check strings
|
|
837
|
+
* ```javascript
|
|
838
|
+
* import { isEmpty } from '@superutils/core'
|
|
839
|
+
*
|
|
840
|
+
* isEmpty('') // true
|
|
841
|
+
* isEmpty(' ') // true
|
|
842
|
+
* isEmpty(`
|
|
843
|
+
*
|
|
844
|
+
*
|
|
845
|
+
* `) // true
|
|
846
|
+
* isEmpty(' not empty ') // false
|
|
847
|
+
* isEmpty(`
|
|
848
|
+
*
|
|
849
|
+
* not empty
|
|
797
850
|
*
|
|
798
|
-
*
|
|
851
|
+
* `) // false
|
|
852
|
+
* ```
|
|
799
853
|
*
|
|
854
|
+
* @example
|
|
855
|
+
* #### check numbers
|
|
800
856
|
* ```javascript
|
|
801
|
-
* import {
|
|
857
|
+
* import { isEmpty } from '@superutils/core'
|
|
802
858
|
*
|
|
803
|
-
*
|
|
804
|
-
*
|
|
859
|
+
* isEmpty(NaN) // true
|
|
860
|
+
* isEmpty(Infinity) // true
|
|
861
|
+
* isEmpty(0) // false
|
|
805
862
|
* ```
|
|
806
863
|
*
|
|
807
|
-
* @example
|
|
808
|
-
*
|
|
864
|
+
* @example
|
|
865
|
+
* #### check objects (includes arrays, maps & sets)
|
|
809
866
|
* ```javascript
|
|
810
|
-
* import {
|
|
867
|
+
* import { isEmpty } from '@superutils/core'
|
|
811
868
|
*
|
|
812
|
-
*
|
|
813
|
-
*
|
|
814
|
-
*
|
|
815
|
-
*
|
|
816
|
-
* console.log(result === dest) // true
|
|
869
|
+
* isEmpty({}) // true
|
|
870
|
+
* isEmpty([]) // true
|
|
871
|
+
* isEmpty(new Map()) // true
|
|
872
|
+
* isEmpty(new Set()) // true
|
|
817
873
|
* ```
|
|
818
874
|
*/
|
|
819
|
-
declare const
|
|
820
|
-
|
|
875
|
+
declare const isEmpty: (x: unknown, nonNumerable?: boolean, fallback?: boolean | 0 | 1) => boolean | 0 | 1;
|
|
821
876
|
/**
|
|
822
|
-
*
|
|
877
|
+
* Safe version of {@link isEmpty} with extended type checks and cross-realm handling.
|
|
823
878
|
*
|
|
824
|
-
* @
|
|
825
|
-
* @param config (optional)
|
|
826
|
-
* @param config.add (optional) Whether to allow adding new properties. Default: `false`
|
|
827
|
-
* @param config.revocable (optional) Default: `false`
|
|
828
|
-
* @param config.silent (optional) Whether to throw error when a write operation is rejected. Default: `true`
|
|
879
|
+
* CAUTION: much slower than {@link isEmpty} due to use of Object.prototype.toString.call()
|
|
829
880
|
*
|
|
830
|
-
*
|
|
831
|
-
|
|
832
|
-
declare const arrReadOnly: <T>(arr: T[], config?: Omit<ReadOnlyConfig<T[]>, "revoke">) => T[];
|
|
833
|
-
|
|
834
|
-
/**
|
|
835
|
-
* @ignore exclude from documentation
|
|
836
|
-
* Helper class for creating read-only arrays.
|
|
881
|
+
* Cross-realm means objects created in different JavaScript contexts.
|
|
882
|
+
* Eg: iframe, node vm context, worker context, browser extensions etc.
|
|
837
883
|
*
|
|
838
|
-
*
|
|
839
|
-
*
|
|
884
|
+
*
|
|
885
|
+
* @param x The value to check for emptiness.
|
|
886
|
+
*
|
|
887
|
+
* @returns `true` if the value is considered empty, `false` otherwise.
|
|
840
888
|
*/
|
|
841
|
-
declare
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
splice: (..._ignoredArgs: unknown[]) => never[];
|
|
850
|
-
unshift: (..._ignoredArgs: T[]) => number;
|
|
851
|
-
}
|
|
889
|
+
declare const isEmptySafe: (x: unknown, numberableOnly?: boolean) => boolean | 1;
|
|
890
|
+
|
|
891
|
+
/** Check if the environment is Browser */
|
|
892
|
+
declare const isEnvBrowser: () => boolean;
|
|
893
|
+
/** Check if the environment is NodeJS */
|
|
894
|
+
declare const isEnvNode: () => boolean;
|
|
895
|
+
/** Check if page is loaded on a touchscreen device */
|
|
896
|
+
declare const isEnvTouchable: () => boolean;
|
|
852
897
|
|
|
898
|
+
/** Check if value is a function */
|
|
899
|
+
declare const isFn: <T extends (...args: any[]) => any>(x: unknown) => x is T;
|
|
853
900
|
/**
|
|
854
|
-
*
|
|
901
|
+
* Check if value is an Async function.
|
|
902
|
+
* Caution: May not work at runtime when Babel/Webpack is used due to code compilation.
|
|
855
903
|
*
|
|
856
|
-
* @
|
|
857
|
-
*
|
|
858
|
-
*
|
|
904
|
+
* @example
|
|
905
|
+
* ```javascript
|
|
906
|
+
* import { isAsyncFn } from '@superutils/core'
|
|
859
907
|
*
|
|
860
|
-
*
|
|
908
|
+
* isAsyncFn(async () => {}) // result: true
|
|
909
|
+
* isAsyncFn(() => {}) // result: false
|
|
910
|
+
* ```
|
|
861
911
|
*/
|
|
862
|
-
declare const
|
|
912
|
+
declare const isAsyncFn: <TData = unknown, TArgs extends unknown[] = unknown[]>(x: unknown) => x is AsyncFn<TData, TArgs>;
|
|
913
|
+
|
|
914
|
+
/** Check if value is instance of Map */
|
|
915
|
+
declare const isMap: <TKey = unknown, TValue = unknown>(x: unknown) => x is Map<TKey, TValue>;
|
|
916
|
+
/** Check if provided is a Map and all values are objects */
|
|
917
|
+
declare const isMapObj: <K extends PropertyKey, V, T extends Record<K, V>>(x: unknown, strict?: boolean) => x is Map<K, V>;
|
|
918
|
+
|
|
919
|
+
/** Check if value is an integer */
|
|
920
|
+
declare const isInteger: (x: unknown) => x is number;
|
|
921
|
+
/** Check if value is a valid negative integer */
|
|
922
|
+
declare const isNegativeInteger: (x: unknown) => x is number;
|
|
923
|
+
/** Check if value is a valid negative number */
|
|
924
|
+
declare const isNegativeNumber: (x: unknown) => x is number;
|
|
925
|
+
/** Check if value is a valid finite number */
|
|
926
|
+
declare const isNumber: (x: unknown) => x is number;
|
|
927
|
+
/** Check if value is a positive integer */
|
|
928
|
+
declare const isPositiveInteger: (x: unknown) => x is number;
|
|
929
|
+
/** Check if value is a positive number */
|
|
930
|
+
declare const isPositiveNumber: (x: unknown) => x is number;
|
|
863
931
|
|
|
864
932
|
/**
|
|
865
|
-
*
|
|
933
|
+
* Check if value is an object.
|
|
866
934
|
*
|
|
867
|
-
* @param arr
|
|
868
|
-
* @param key (optional) Array object-item property name or a function to generate keys for each array items.
|
|
869
|
-
* @param flatDepth (optional) maximum recursion depth to flatten the array. Default: `0`
|
|
870
935
|
*
|
|
871
|
-
* @
|
|
936
|
+
* @param x value to check
|
|
937
|
+
* @param strict (optional) whether to exclude anything other than plain object. Eg: Array, Map, RegExp, Set etc.
|
|
872
938
|
*
|
|
873
|
-
*
|
|
874
|
-
* ```typescript
|
|
875
|
-
* type Item = { a: number }
|
|
876
|
-
* const arr: Item[] = [{ a: 1 }, { a: 2 }, { a: 3 }, [{ a: 4 }]]
|
|
877
|
-
* const map: Map<number, Item> = arrToMap(
|
|
878
|
-
* arr,
|
|
879
|
-
* (_: Item, i: number) => item.a,
|
|
880
|
-
* )
|
|
881
|
-
* ```
|
|
939
|
+
* Default: `true`
|
|
882
940
|
*
|
|
883
|
-
*
|
|
884
|
-
*
|
|
885
|
-
*
|
|
886
|
-
*
|
|
887
|
-
*
|
|
888
|
-
*
|
|
889
|
-
*
|
|
890
|
-
*
|
|
891
|
-
*
|
|
892
|
-
*
|
|
941
|
+
* Valid objects:
|
|
942
|
+
* - object literals (Prototype: `Object.prototype`)
|
|
943
|
+
* - objects created using `Object.create(null)`
|
|
944
|
+
*
|
|
945
|
+
* Valid when strict mode off (false):
|
|
946
|
+
* - all of above
|
|
947
|
+
* - buit-in objects like Date, Error, Map, Set, Uint8Array etc
|
|
948
|
+
* - custom Class instances
|
|
949
|
+
* - objects created using `Object.create(object)`
|
|
950
|
+
*
|
|
951
|
+
* Always invalid:
|
|
952
|
+
* - `null`, `undefined`, `NaN`, `Infinity`
|
|
953
|
+
* - primitive: String, Number, BigInt...
|
|
954
|
+
*
|
|
955
|
+
* @example
|
|
956
|
+
* ```javascript
|
|
957
|
+
* import { isObj } from '@superutils/core'
|
|
958
|
+
*
|
|
959
|
+
* console.log(isObj(null)) // false
|
|
960
|
+
* console.log(isObj(undefined)) // false
|
|
961
|
+
* console.log(isObj(NaN)) // false
|
|
962
|
+
* console.log(isObj(Infinity)) // false
|
|
963
|
+
* console.log(isObj({})) // true
|
|
964
|
+
* console.log(isObj({ a: 1, b: 2})) // true
|
|
965
|
+
* console.log(isObj(Object.create(null))) // true
|
|
966
|
+
* console.log(isObj(new Map())) // false (strict)
|
|
967
|
+
* console.log(isObj(new Map(), false)) // true (non-strict)
|
|
968
|
+
* console.log(isObj(new Error('error'))) // false (strict)
|
|
969
|
+
* console.log(isObj(new Error('error'), false)) // true (non-strict)
|
|
970
|
+
* class Test { a = 1 } // a custom class
|
|
971
|
+
* console.log(isObj(new Test())) // false
|
|
972
|
+
* console.log(isObj(new Test(), false)) // true
|
|
893
973
|
* ```
|
|
894
974
|
*/
|
|
895
|
-
declare
|
|
896
|
-
declare function arrToMap<T extends unknown[], FlatDepth extends number = 0>(arr: T, flatDepth?: FlatDepth): Map<number, FlatArray<T, FlatDepth>>;
|
|
897
|
-
declare function arrToMap<T extends unknown[], FlatDepth extends number = 0, MapItem = FlatArray<T, FlatDepth>, MapKey = unknown>(arr: T, key: (item: MapItem, index: number, flatArr: MapItem[]) => MapKey, flatDepth?: FlatDepth): Map<MapKey, MapItem>;
|
|
975
|
+
declare const isObj: <T = Record<PropertyKey, unknown>>(x: unknown, strict?: boolean) => x is T;
|
|
898
976
|
|
|
977
|
+
/** Check if value is instance of URL */
|
|
978
|
+
declare const isUrl: (x: unknown) => x is URL;
|
|
899
979
|
/**
|
|
900
|
-
*
|
|
901
|
-
*
|
|
980
|
+
* Check if a value is a valid URL/string-URL.
|
|
981
|
+
*
|
|
982
|
+
* @param x The value to check.
|
|
983
|
+
* @param strict If true:
|
|
984
|
+
* - requires a domain name with a TLDs etc.
|
|
985
|
+
* - and x is string, catches any auto-correction (eg: trailing spaces being removed, spaces being replaced by `%20`)
|
|
986
|
+
* by `new URL()` to ensure string URL is valid
|
|
987
|
+
* Defaults to `true`.
|
|
988
|
+
* @param tldExceptions when in strict mode, treat these hosts as valid despite no domain name extensions
|
|
989
|
+
* Defaults to `['localhost']`
|
|
990
|
+
*
|
|
991
|
+
* @returns `true` if the value is a valid URL, `false` otherwise.
|
|
902
992
|
*/
|
|
903
|
-
declare const
|
|
993
|
+
declare const isUrlValid: (x: unknown, strict?: boolean, tldExceptions?: string[]) => boolean;
|
|
904
994
|
|
|
995
|
+
/** Check if value is boolean */
|
|
996
|
+
declare const isBool: (x: unknown) => x is boolean;
|
|
997
|
+
/** Check if value is not undefined or null */
|
|
998
|
+
declare const isDefined: <T = unknown>(x: T | undefined | null) => x is T;
|
|
999
|
+
/** Check if value is instance of Error */
|
|
1000
|
+
declare const isError: (x: unknown) => x is Error;
|
|
1001
|
+
/** Check if value is a Promise */
|
|
1002
|
+
declare const isPromise: <T = unknown>(x: unknown) => x is Promise<T>;
|
|
1003
|
+
/** Check if value is a regular expession */
|
|
1004
|
+
declare const isRegExp: (x: unknown) => x is RegExp;
|
|
1005
|
+
/** Check if value is instance of Set */
|
|
1006
|
+
declare const isSet: <T = unknown>(x: unknown) => x is Set<T>;
|
|
1007
|
+
/** Check if value is string */
|
|
1008
|
+
declare const isStr: (x: unknown) => x is string;
|
|
905
1009
|
/**
|
|
906
|
-
*
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
tid?: TimeoutId;
|
|
913
|
-
};
|
|
914
|
-
/**
|
|
915
|
-
* Deferred function options
|
|
1010
|
+
* Check if value is similar to a RxJS subject with .subscribe & .next functions
|
|
1011
|
+
*
|
|
1012
|
+
* @param x The value to check
|
|
1013
|
+
* @param withValue When `true`, also checks if `value` property exists in `x`
|
|
1014
|
+
*
|
|
1015
|
+
* @returns `true` if the value is subject-like, `false` otherwise.
|
|
916
1016
|
*/
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
throttle?: false;
|
|
921
|
-
} & DebounceOptions<ThisArg>);
|
|
1017
|
+
declare const isSubjectLike: <T>(x: unknown, withValue?: boolean) => boolean;
|
|
1018
|
+
/** Check if value is a Symbol */
|
|
1019
|
+
declare const isSymbol: (x: unknown) => x is symbol;
|
|
922
1020
|
/**
|
|
923
|
-
*
|
|
1021
|
+
* Compilation of all the compile-time & runtime utilities functions above
|
|
924
1022
|
*/
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
1023
|
+
declare const is: {
|
|
1024
|
+
arr: <Item = unknown>(x: unknown) => x is Item[];
|
|
1025
|
+
arr2D: <T = unknown>(x: unknown) => x is T[][];
|
|
1026
|
+
arrLike: <K = unknown, V = unknown>(x: unknown) => x is IterableList<K, V>;
|
|
1027
|
+
arrLikeSafe: <K = unknown, V = unknown>(x: unknown) => x is IterableList<K, V>;
|
|
1028
|
+
arrObj: <K extends PropertyKey, V, T extends Record<K, V>[]>(x: unknown, strict?: boolean) => x is T[];
|
|
1029
|
+
arrUnique: <T = unknown>(x: unknown) => x is T[];
|
|
1030
|
+
asyncFn: <TData = unknown, TArgs extends unknown[] = unknown[]>(x: unknown) => x is AsyncFn<TData, TArgs>;
|
|
1031
|
+
bool: (x: unknown) => x is boolean;
|
|
1032
|
+
date: (x: unknown) => x is Date;
|
|
1033
|
+
dateValid: (date: unknown) => boolean;
|
|
1034
|
+
defined: <T = unknown>(x: T | undefined | null) => x is T;
|
|
1035
|
+
empty: (x: unknown, nonNumerable?: boolean, fallback?: boolean | 0 | 1) => boolean | 0 | 1;
|
|
1036
|
+
emptySafe: (x: unknown, numberableOnly?: boolean) => boolean | 1;
|
|
1037
|
+
envBrowser: () => boolean;
|
|
1038
|
+
envNode: () => boolean;
|
|
1039
|
+
envTouchable: () => boolean;
|
|
1040
|
+
error: (x: unknown) => x is Error;
|
|
1041
|
+
fn: <T extends (...args: any[]) => any>(x: unknown) => x is T;
|
|
1042
|
+
integer: (x: unknown) => x is number;
|
|
1043
|
+
map: <TKey = unknown, TValue = unknown>(x: unknown) => x is Map<TKey, TValue>;
|
|
1044
|
+
mapObj: <K extends PropertyKey, V, T extends Record<K, V>>(x: unknown, strict?: boolean) => x is Map<K, V>;
|
|
1045
|
+
negativeInteger: (x: unknown) => x is number;
|
|
1046
|
+
negativeNumber: (x: unknown) => x is number;
|
|
1047
|
+
number: (x: unknown) => x is number;
|
|
1048
|
+
obj: <T = Record<PropertyKey, unknown>>(x: unknown, strict?: boolean) => x is T;
|
|
1049
|
+
positiveInteger: (x: unknown) => x is number;
|
|
1050
|
+
positiveNumber: (x: unknown) => x is number;
|
|
1051
|
+
promise: <T = unknown>(x: unknown) => x is Promise<T>;
|
|
1052
|
+
regExp: (x: unknown) => x is RegExp;
|
|
1053
|
+
set: <T = unknown>(x: unknown) => x is Set<T>;
|
|
1054
|
+
str: (x: unknown) => x is string;
|
|
1055
|
+
subjectLike: <T>(x: unknown, withValue?: boolean) => boolean;
|
|
1056
|
+
symbol: (x: unknown) => x is symbol;
|
|
1057
|
+
uint8Arr: (x: unknown) => x is Uint8Array;
|
|
1058
|
+
url: (x: unknown) => x is URL;
|
|
1059
|
+
urlValid: (x: unknown, strict?: boolean, tldExceptions?: string[]) => boolean;
|
|
935
1060
|
};
|
|
936
1061
|
|
|
1062
|
+
/** Placeholder function that does nothing */
|
|
1063
|
+
declare function noop(): void;
|
|
1064
|
+
/** Placeholder async function that does nothing */
|
|
1065
|
+
declare function noopAsync(): Promise<void>;
|
|
1066
|
+
|
|
937
1067
|
/**
|
|
938
|
-
*
|
|
939
|
-
* All errors will be gracefully swallowed.
|
|
940
|
-
*
|
|
941
|
-
* @param callback function to be invoked after timeout
|
|
942
|
-
* @param delay (optional) timeout duration in milliseconds. Default: 50
|
|
943
|
-
* @param config.onError (optional)
|
|
944
|
-
* @param config.leading (optional) if true, will enable leading-edge debounce mechanism.
|
|
945
|
-
* @param config.thisArg (optional) the special `thisArgs` to be used when invoking the callback.
|
|
946
|
-
* @param config.tid (optional) Timeout Id. If provided, will clear the timeout on first invocation.
|
|
1068
|
+
* Convert timestamp to `input["datetime-local"]` compatible format.
|
|
947
1069
|
*
|
|
948
|
-
* @example
|
|
1070
|
+
* @example
|
|
1071
|
+
* #### Convert ISO datetime string
|
|
949
1072
|
* ```javascript
|
|
950
|
-
*
|
|
1073
|
+
* toDatetimeLocal('2000-01-01T01:01:01.001Z')
|
|
1074
|
+
* // result: "2000-01-01T01:01" // assuming local timezone is UTC+0
|
|
1075
|
+
* ```
|
|
951
1076
|
*
|
|
952
|
-
*
|
|
953
|
-
*
|
|
954
|
-
*
|
|
955
|
-
* )
|
|
1077
|
+
* @example
|
|
1078
|
+
* #### Convert Date object
|
|
1079
|
+
* ```javascript
|
|
1080
|
+
* const date = new Date('2000-01-01T01:01:01.001Z')
|
|
1081
|
+
* toDatetimeLocal(date)
|
|
1082
|
+
* // result: "2000-01-01T01:01" // assuming local timezone is UTC+0
|
|
1083
|
+
* ```
|
|
956
1084
|
*
|
|
957
|
-
*
|
|
958
|
-
*
|
|
959
|
-
*
|
|
1085
|
+
* @example
|
|
1086
|
+
* #### Convert Unix Timestamp (epoch time) number
|
|
1087
|
+
* ```javascript
|
|
1088
|
+
* const epoch = new Date('2000-01-01T01:01:01.001Z').getTime()
|
|
1089
|
+
* toDatetimeLocal(epoch)
|
|
1090
|
+
* // result: "2000-01-01T01:01" // assuming local timezone is UTC+0
|
|
960
1091
|
* ```
|
|
961
1092
|
*/
|
|
962
|
-
declare const
|
|
963
|
-
<TArgs extends unknown[], ThisArg>(callback: (this: ThisArg, ...args: TArgs) => ValueOrPromise<unknown>, delay?: number, config?: DebounceOptions<ThisArg>): (...args: TArgs) => void;
|
|
964
|
-
defaults: {
|
|
965
|
-
/**
|
|
966
|
-
* Set the default value of argument `leading` for the `deferred` function.
|
|
967
|
-
* This change is applicable application-wide and only applies to any new invocation of `deferred()`.
|
|
968
|
-
*/
|
|
969
|
-
leading: false;
|
|
970
|
-
/**
|
|
971
|
-
* Set the default value of argument `onError` for the `deferred` function.
|
|
972
|
-
* This change is applicable application-wide and only applies to any new invocation of `deferred()`.
|
|
973
|
-
*/
|
|
974
|
-
onError: undefined;
|
|
975
|
-
};
|
|
976
|
-
};
|
|
1093
|
+
declare const toDatetimeLocal: (dateStr: string | Date | number) => "" | `${number}-${number}-${number}T${number}:${number}`;
|
|
977
1094
|
|
|
978
1095
|
/**
|
|
979
|
-
*
|
|
980
|
-
* All errors will be gracefully swallowed. See {@link debounce} and {@link throttle} for more information.
|
|
1096
|
+
* Constructs a new object with only the supplied property names (keys) and their respective values
|
|
981
1097
|
*
|
|
982
|
-
* @param
|
|
983
|
-
* @param
|
|
984
|
-
* @param
|
|
1098
|
+
* @param obj
|
|
1099
|
+
* @param keys property names to keep
|
|
1100
|
+
* @param ignoreIfNotExist (optional) if truthy, will ignore non-existent `keys`. Default: `true`
|
|
985
1101
|
*
|
|
986
|
-
* @
|
|
987
|
-
*
|
|
988
|
-
*
|
|
1102
|
+
* @example
|
|
1103
|
+
* #### Create a new object from an existing object by only extracting specified keys and relevant values
|
|
1104
|
+
*
|
|
1105
|
+
* ```javascript
|
|
1106
|
+
* import { objClean } from '@superutils/core'
|
|
1107
|
+
*
|
|
1108
|
+
* const obj = {
|
|
1109
|
+
* a: 1,
|
|
1110
|
+
* b: 2,
|
|
1111
|
+
* c: 3,
|
|
1112
|
+
* d: 4
|
|
1113
|
+
* }
|
|
1114
|
+
* console.log(objClean(obj, [ 'a', 'b']))
|
|
1115
|
+
* // { a: 1, b: 2 }
|
|
1116
|
+
* ```
|
|
989
1117
|
*/
|
|
990
|
-
declare const
|
|
1118
|
+
declare const objClean: <T extends Record<PropertyKey, unknown>, Key extends keyof T = keyof T>(obj: T, keys: Key[], ignoreIfNotExist?: boolean) => Record<Key, T[Key]>;
|
|
991
1119
|
|
|
992
1120
|
/**
|
|
993
|
-
*
|
|
994
|
-
* All errors will be gracefully swallowed.
|
|
1121
|
+
* Deep-copy an object to another object
|
|
995
1122
|
*
|
|
996
|
-
*
|
|
1123
|
+
* @param input input object
|
|
1124
|
+
* @param output (optional) output object
|
|
1125
|
+
* @param ignoreKeys (optional) input peroperties to be ignored. Prevents output's property to be overriden.
|
|
997
1126
|
*
|
|
998
|
-
*
|
|
999
|
-
* the callback runs again at the end of the delay with the most recent arguments.
|
|
1127
|
+
* For child object properties use "." (dot) separated path.
|
|
1000
1128
|
*
|
|
1001
|
-
*
|
|
1002
|
-
* @param delay (optional) interval duration in milliseconds. Default: 50
|
|
1003
|
-
* @param config (optional)
|
|
1004
|
-
* @param config.onError (optional) callback to be invoked on error
|
|
1005
|
-
* @param config.tid (optional)
|
|
1006
|
-
* @param config.thisArg (optional) the special `thisArgs` to be used when invoking the callback.
|
|
1007
|
-
* @param config.trailing (optional) whether to enable trailing edge execution. Default: `true`
|
|
1129
|
+
* Eg: `"child.grandchild1"` where input is `{ child: { grandchild1: 1, grandchild2: 2 }}`
|
|
1008
1130
|
*
|
|
1009
|
-
* @
|
|
1010
|
-
*
|
|
1011
|
-
*
|
|
1131
|
+
* @param override (optional) whether to allow override output properties.
|
|
1132
|
+
* This will only be used if `output` object is provided and has own property.
|
|
1133
|
+
* Accepted values:
|
|
1134
|
+
* - `true`: input property will override output property
|
|
1135
|
+
* - `false`: no overriding if output contains the property. Even if the property value is `undefined`.
|
|
1136
|
+
* - `"empty"`: only allow overriding output property if it's value is empty by using {@link isEmpty}.
|
|
1137
|
+
* - `function`: decide whether to override on a per property basis.
|
|
1012
1138
|
*
|
|
1013
|
-
*
|
|
1014
|
-
*
|
|
1015
|
-
*
|
|
1016
|
-
*
|
|
1017
|
-
* handleChange({ target: { value: 1 } }) // will be executed
|
|
1018
|
-
* handleChange({ target: { value: 2 } }) // will be ignored
|
|
1019
|
-
* handleChange({ target: { value: 3 } }) // will be ignored
|
|
1139
|
+
* Function Arguments:
|
|
1140
|
+
* 1. key: current property name/key
|
|
1141
|
+
* 2. outputValue: `output` property value
|
|
1142
|
+
* 3. inputValue: `input` property value
|
|
1020
1143
|
*
|
|
1021
|
-
*
|
|
1022
|
-
* handleChange({ target: { value: 4 } }) // will be executed (after 300ms)
|
|
1023
|
-
* handleChange({ target: { value: 5 } }) // will be ignored
|
|
1024
|
-
* }, 400)
|
|
1025
|
-
* ```
|
|
1144
|
+
* Default: `false`
|
|
1026
1145
|
*
|
|
1027
|
-
* @
|
|
1028
|
-
* ```javascript
|
|
1029
|
-
* import { throttle } from '@superutils/core'
|
|
1146
|
+
* @param recursive (optional) whether to recursively copy nested objects. Default: `false`
|
|
1030
1147
|
*
|
|
1031
|
-
* const handleChange = throttle(
|
|
1032
|
-
* event => console.log('Value:', event.target.value),
|
|
1033
|
-
* 300, // throttle duration in milliseconds
|
|
1034
|
-
* )
|
|
1035
|
-
* handleChange({ target: { value: 1 } }) // will be executed
|
|
1036
|
-
* handleChange({ target: { value: 2 } }) // will be ignored
|
|
1037
|
-
* handleChange({ target: { value: 3 } }) // will be executed
|
|
1038
1148
|
*
|
|
1039
|
-
*
|
|
1040
|
-
*
|
|
1041
|
-
*
|
|
1042
|
-
*
|
|
1149
|
+
* @returns copied and/or merged object
|
|
1150
|
+
*
|
|
1151
|
+
* @example
|
|
1152
|
+
* #### Copy or merge objects
|
|
1153
|
+
*
|
|
1154
|
+
* ```javascript
|
|
1155
|
+
* import { objCopy } from '@superutils/core'
|
|
1156
|
+
*
|
|
1157
|
+
* const source = {
|
|
1158
|
+
* a: 1,
|
|
1159
|
+
* b: 2,
|
|
1160
|
+
* c: 3,
|
|
1161
|
+
* x: {
|
|
1162
|
+
* a: 1,
|
|
1163
|
+
* b: 2,
|
|
1164
|
+
* },
|
|
1165
|
+
* }
|
|
1166
|
+
* const dest = {
|
|
1167
|
+
* d: 4,
|
|
1168
|
+
* e: 5,
|
|
1169
|
+
* }
|
|
1170
|
+
* const copied = objCopy(
|
|
1171
|
+
* source,
|
|
1172
|
+
* dest,
|
|
1173
|
+
* ['a', 'x.b'], // exclude source property
|
|
1174
|
+
* 'empty', // only override if dest doesn't have the property or value is "empty" (check `is.emtpy()`)
|
|
1175
|
+
* true, // recursively copies child objects. If false, child objects are copied by reference.
|
|
1176
|
+
* )
|
|
1177
|
+
* console.log({ copied })
|
|
1178
|
+
* // Result:
|
|
1179
|
+
* // {
|
|
1180
|
+
* // b: 2,
|
|
1181
|
+
* // c: 33,
|
|
1182
|
+
* // d: 4,
|
|
1183
|
+
* // e: 5,
|
|
1184
|
+
* // }
|
|
1185
|
+
* console.log(dest === copied) // true (dest is returned)
|
|
1043
1186
|
* ```
|
|
1044
1187
|
*/
|
|
1045
|
-
declare const
|
|
1046
|
-
<TArgs extends unknown[], ThisArg>(callback: (this: ThisArg, ...args: TArgs) => ValueOrPromise<unknown>, delay?: number, config?: ThrottleOptions<ThisArg>): (...args: TArgs) => void;
|
|
1047
|
-
/**
|
|
1048
|
-
* Set the default values
|
|
1049
|
-
* This change is applicable application-wide and only applies to any new invocation of `throttle()`.
|
|
1050
|
-
*/
|
|
1051
|
-
defaults: {
|
|
1052
|
-
onError: undefined;
|
|
1053
|
-
trailing: false;
|
|
1054
|
-
};
|
|
1055
|
-
};
|
|
1056
|
-
/**
|
|
1057
|
-
* For legacy compatibility
|
|
1058
|
-
* @deprecated use `throttle` instead
|
|
1059
|
-
*/
|
|
1060
|
-
declare const throttled: {
|
|
1061
|
-
<TArgs extends unknown[], ThisArg>(callback: (this: ThisArg, ...args: TArgs) => ValueOrPromise<unknown>, delay?: number, config?: ThrottleOptions<ThisArg>): (...args: TArgs) => void;
|
|
1062
|
-
/**
|
|
1063
|
-
* Set the default values
|
|
1064
|
-
* This change is applicable application-wide and only applies to any new invocation of `throttle()`.
|
|
1065
|
-
*/
|
|
1066
|
-
defaults: {
|
|
1067
|
-
onError: undefined;
|
|
1068
|
-
trailing: false;
|
|
1069
|
-
};
|
|
1070
|
-
};
|
|
1188
|
+
declare const objCopy: <Key extends PropertyKey, InValue, OutValue, IgnoredKey extends Key | string>(input: Record<Key, InValue>, output?: Record<PropertyKey, OutValue>, ignoreKeys?: IgnoredKey[] | Set<IgnoredKey>, override?: boolean | "empty" | ((key: Key, outputValue: OutValue, inputValue: InValue) => boolean), recursive?: boolean) => Record<PropertyKey, unknown>;
|
|
1071
1189
|
|
|
1072
|
-
/**
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
/**
|
|
1148
|
-
|
|
1190
|
+
/**
|
|
1191
|
+
* Creates an object from an array of keys and a corresponding array of values.
|
|
1192
|
+
* It pairs each key with the value at the same index.
|
|
1193
|
+
*
|
|
1194
|
+
* @param keys An array of property keys (strings or symbols).
|
|
1195
|
+
* @param values (optional) An array of property values. The value at each index corresponds to the key at the same index. If a value is missing for a key, it will be `undefined`.
|
|
1196
|
+
* @param result (optional) An existing object to add or overwrite properties on. If not provided, a new object is created.
|
|
1197
|
+
* @returns The newly created object, or the `result` object merged with the new properties.
|
|
1198
|
+
*
|
|
1199
|
+
* @example
|
|
1200
|
+
* #### Creating a new object from arrays of keys and values
|
|
1201
|
+
* ```javascript
|
|
1202
|
+
* import { objCreate } from '@superutils/core'
|
|
1203
|
+
*
|
|
1204
|
+
* const keys = ['a', 'b', 'c']
|
|
1205
|
+
* const values = [1, 2, 3]
|
|
1206
|
+
* const newObj = objCreate(keys, values)
|
|
1207
|
+
* console.log(newObj)
|
|
1208
|
+
* // newObj is { a: 1, b: 2, c: 3 }
|
|
1209
|
+
* ```
|
|
1210
|
+
*
|
|
1211
|
+
* @example
|
|
1212
|
+
* #### Merging into an existing object
|
|
1213
|
+
* ```typescript
|
|
1214
|
+
* import { objCreate } from '@superutils/core'
|
|
1215
|
+
*
|
|
1216
|
+
* const existingObj = { a: 0, d: 4 }
|
|
1217
|
+
* const keys = ['b', 'c']
|
|
1218
|
+
* const values = [2, 3]
|
|
1219
|
+
* const newObj = objCreate(keys, values, existingObj)
|
|
1220
|
+
* console.log(newObj)
|
|
1221
|
+
* // existingObj is now { a: 0, d: 4, b: 2, c: 3 }
|
|
1222
|
+
* ```
|
|
1223
|
+
*/
|
|
1224
|
+
declare const objCreate: <V, K extends PropertyKey, RV, RK extends PropertyKey, Result extends Record<K | RK, V | RV>>(keys?: K[], values?: V[], result?: Result) => Result;
|
|
1225
|
+
|
|
1226
|
+
/**
|
|
1227
|
+
* Checks if all the supplied keys exist in an object
|
|
1228
|
+
*
|
|
1229
|
+
* @param input
|
|
1230
|
+
* @param keys
|
|
1231
|
+
* @param requireValue (optional) whether each property should have some value.
|
|
1232
|
+
*
|
|
1233
|
+
* @example
|
|
1234
|
+
* #### Check if object contains specified properties
|
|
1235
|
+
* ```javascript
|
|
1236
|
+
* import { objHasKeys } from '@superutils/core'
|
|
1237
|
+
*
|
|
1238
|
+
* const obj = { a: 1, b: null, c: 0 }
|
|
1239
|
+
* console.log(objHasKeys(obj, ['a', 'b']))
|
|
1240
|
+
* // true
|
|
1241
|
+
*
|
|
1242
|
+
* console.log(
|
|
1243
|
+
* objHasKeys(
|
|
1244
|
+
* obj,
|
|
1245
|
+
* ['a', 'b'],
|
|
1246
|
+
* true // check if all the respective values of the provided keys are not "empty". Uses `ìsEmpty()`.
|
|
1247
|
+
* )
|
|
1248
|
+
* )
|
|
1249
|
+
* // false
|
|
1250
|
+
* ```
|
|
1251
|
+
*/
|
|
1252
|
+
declare function objHasKeys(input?: object | unknown[], keys?: PropertyKey[], requireValue?: boolean): boolean;
|
|
1253
|
+
|
|
1254
|
+
/**
|
|
1255
|
+
* Get object property names/keys
|
|
1256
|
+
*
|
|
1257
|
+
* @param obj target object
|
|
1258
|
+
* @param sorted (optional) Whether to sort the keys. Default: `true`
|
|
1259
|
+
* @param includeSymbols (optional) Whether to include `Symbol` object. Default: `false`
|
|
1260
|
+
*/
|
|
1261
|
+
declare const objKeys: <T extends object, Include extends true | false = true>(obj: T, sorted?: boolean, includeSymbols?: Include) => Include extends true ? (keyof T)[] : (keyof T)[] & string[];
|
|
1262
|
+
|
|
1263
|
+
type ObjReadOnlyAllowAddFn<T> = (obj: T, key: string | symbol, value: unknown) => boolean;
|
|
1264
|
+
type ObjReadOnlyConfig<T, Revocable extends true | false = false> = {
|
|
1265
|
+
/** Whether to allow adding new properties. Default: `false` */
|
|
1266
|
+
add?: boolean | ObjReadOnlyAllowAddFn<T>;
|
|
1267
|
+
/** Default: `false` */
|
|
1268
|
+
revocable?: Revocable;
|
|
1269
|
+
/** Whether to throw error when a write operation is rejected. Default: `true` */
|
|
1270
|
+
silent?: boolean;
|
|
1149
1271
|
};
|
|
1150
1272
|
|
|
1151
1273
|
/**
|
|
1152
|
-
*
|
|
1274
|
+
* Constructs a read-only proxy of an object.
|
|
1275
|
+
* Prevents modification or deletion of existing properties based on configuration.
|
|
1153
1276
|
*
|
|
1154
|
-
*
|
|
1155
|
-
* @param predicate callback function to filter values
|
|
1156
|
-
* Parameters:
|
|
1157
|
-
* 1. `item`: current item
|
|
1158
|
-
* 2. `key`: index/key
|
|
1159
|
-
* 3. `data`: value provided in the first argument (`data`)
|
|
1160
|
-
* @param limit (optional) limit number of results
|
|
1161
|
-
* @param asArray
|
|
1277
|
+
* Applies only to top-level properties.
|
|
1162
1278
|
*
|
|
1163
|
-
* @
|
|
1279
|
+
* @param obj input object
|
|
1280
|
+
* @param config (optional) extra configuration
|
|
1281
|
+
* @param config.add (optional) Whether to allow adding new properties. Default: `false`
|
|
1282
|
+
* @param config.revocable (optional) Whether to create a revokable proxy. Default: `false`
|
|
1283
|
+
* @param config.silent (optional) whether to throw error when a write operation is rejected. Default: `true`
|
|
1284
|
+
*
|
|
1285
|
+
* @returns Readonly object or object containing readonly object and revoke function
|
|
1164
1286
|
*
|
|
1165
1287
|
* @example
|
|
1166
|
-
*
|
|
1167
|
-
* import { filter } from '@superutils/core'
|
|
1288
|
+
* Create a readonly object and silently ignore any attempt of property add, update and delete operations
|
|
1168
1289
|
*
|
|
1169
|
-
*
|
|
1170
|
-
*
|
|
1171
|
-
*
|
|
1172
|
-
*
|
|
1173
|
-
*
|
|
1290
|
+
* ```javascript
|
|
1291
|
+
* import { objReadOnly } from '@superutils/core'
|
|
1292
|
+
*
|
|
1293
|
+
* const obj = objReadOnly({ a: 1, b: 2})
|
|
1294
|
+
* obj.a = 3
|
|
1295
|
+
* delete obj.a
|
|
1296
|
+
* console.log(obj.a) // 1
|
|
1297
|
+
* obj.c = 4
|
|
1298
|
+
* console.log(obj.c) // undefined
|
|
1299
|
+
* ```
|
|
1174
1300
|
*
|
|
1175
|
-
*
|
|
1176
|
-
*
|
|
1177
|
-
*
|
|
1178
|
-
*
|
|
1301
|
+
* @example
|
|
1302
|
+
* Create a readonly object and throw error on any attempt of property add, update and delete operations
|
|
1303
|
+
*
|
|
1304
|
+
* ```javascript
|
|
1305
|
+
* import { fallbackIfFails, objReadOnly } from '@superutils/core'
|
|
1306
|
+
*
|
|
1307
|
+
* const obj = objReadOnly(
|
|
1308
|
+
* { a: 1, b: 2},
|
|
1309
|
+
* { silent: false }
|
|
1310
|
+
* )
|
|
1311
|
+
*
|
|
1312
|
+
* try {
|
|
1313
|
+
* obj.a = 3
|
|
1314
|
+
* } catch(err) { console.log('update failed:', err.message) }
|
|
1315
|
+
*
|
|
1316
|
+
* try {
|
|
1317
|
+
* delete obj.a
|
|
1318
|
+
* } catch(err) { console.log('delete failed:', err.message) }
|
|
1319
|
+
*
|
|
1320
|
+
* try {
|
|
1321
|
+
* obj.c = 4
|
|
1322
|
+
* } catch(err) { console.log('add failed:', err.message) }
|
|
1323
|
+
* console.log(obj) // { a: 1, b: 2 }
|
|
1324
|
+
* ```
|
|
1325
|
+
*
|
|
1326
|
+
* @example
|
|
1327
|
+
* Create a readonly object and throw error on any attempt of property update and delete operations
|
|
1328
|
+
* but allow adding new properties
|
|
1329
|
+
*
|
|
1330
|
+
* ```javascript
|
|
1331
|
+
* import { fallbackIfFails, objReadOnly } from '@superutils/core'
|
|
1332
|
+
*
|
|
1333
|
+
* const obj = objReadOnly(
|
|
1334
|
+
* { a: 1, b: 2},
|
|
1335
|
+
* {
|
|
1336
|
+
* add: true,
|
|
1337
|
+
* silent: false
|
|
1338
|
+
* }
|
|
1339
|
+
* )
|
|
1340
|
+
* obj.c = 4
|
|
1341
|
+
* console.log(obj.c) // 4
|
|
1342
|
+
* ```
|
|
1343
|
+
*/
|
|
1344
|
+
declare const objReadOnly: <T extends object | unknown[], Revocable extends true | false = false, Result = Revocable extends true ? ReturnType<typeof Proxy.revocable<T>> : T>(obj: T, config?: ObjReadOnlyConfig<T, Revocable>) => Result;
|
|
1345
|
+
|
|
1346
|
+
type SetObPropPredicate<K extends PropertyKey, V, OutKey> = (value: V | undefined, key: OutKey, obj: Record<K, V>) => boolean;
|
|
1347
|
+
/**
|
|
1348
|
+
* Conditionally assign value to an object's property
|
|
1349
|
+
*
|
|
1350
|
+
* @param obj target object
|
|
1351
|
+
* @param key object property name
|
|
1352
|
+
* @param falsyValue (optional) value to assign when condition is falsy Default: `obj[key]`
|
|
1353
|
+
* @param condition (optional) condition to determine which value to provide. Default: `false`
|
|
1354
|
+
* @param truthyValue (optional) value to use if condition is truthy
|
|
1355
|
+
*/
|
|
1356
|
+
declare const objSetProp: <K extends PropertyKey, V, OutKey extends K | string>(obj: Record<K, V>, key: OutKey, falsyValue?: V, condition?: boolean | SetObPropPredicate<K, V, OutKey>, truthyValue?: V) => Record<OutKey, V>;
|
|
1357
|
+
/** Assign value to an object property only if current value is undefined */
|
|
1358
|
+
declare const objSetPropUndefined: (...[obj, key, ...args]: Parameters<typeof objSetProp>) => Record<PropertyKey, unknown>;
|
|
1359
|
+
|
|
1360
|
+
/**
|
|
1361
|
+
* Create a new object with properties sorted by key
|
|
1362
|
+
*
|
|
1363
|
+
* @example
|
|
1364
|
+
* #### Sort an object recursively
|
|
1365
|
+
*
|
|
1366
|
+
* ```javascript
|
|
1367
|
+
* import { objSort } from '@superutils/core'
|
|
1368
|
+
*
|
|
1369
|
+
* const d = Symbol('d')
|
|
1370
|
+
* const obj = { c: 3, a: 1, [d]: 4, b: 2, e: { g: 1, f: 2 } }
|
|
1371
|
+
* console.log(objSort(obj))
|
|
1372
|
+
* // Result:
|
|
1373
|
+
* // {
|
|
1374
|
+
* // a: 1,
|
|
1375
|
+
* // b: 2,
|
|
1376
|
+
* // c: 3,
|
|
1377
|
+
* // [d]: 4,
|
|
1378
|
+
* // e: { f: 2, g: 1 },
|
|
1179
1379
|
* // }
|
|
1180
1380
|
* ```
|
|
1181
1381
|
*/
|
|
1182
|
-
declare const
|
|
1382
|
+
declare const objSort: <T>(obj: T, recursive?: boolean, _done?: Map<unknown, boolean>) => T;
|
|
1183
1383
|
|
|
1184
1384
|
/**
|
|
1185
|
-
*
|
|
1385
|
+
* Creates a new object excluding specific properties
|
|
1186
1386
|
*
|
|
1187
|
-
* @
|
|
1387
|
+
* @param {Object} input
|
|
1388
|
+
* @param {Array} keys property names to exclude
|
|
1389
|
+
* @param {Object} output (optional) to delete unwanted props from the original `input` use it here.
|
|
1390
|
+
* Default: a copy of the `input` object
|
|
1188
1391
|
*
|
|
1189
|
-
* @
|
|
1190
|
-
* ```typescript
|
|
1191
|
-
* import { find } from '@superutils/core'
|
|
1392
|
+
* @returns {Object}
|
|
1192
1393
|
*
|
|
1193
|
-
*
|
|
1194
|
-
*
|
|
1195
|
-
*
|
|
1196
|
-
*
|
|
1197
|
-
*
|
|
1198
|
-
*
|
|
1199
|
-
*
|
|
1394
|
+
* @example
|
|
1395
|
+
* #### Create a new object excluding specific properties
|
|
1396
|
+
*
|
|
1397
|
+
* ```javascript
|
|
1398
|
+
* import { objWithoutKeys } from '@superutils/core'
|
|
1399
|
+
*
|
|
1400
|
+
* const result = objWithoutKeys({ a: 1, b: '2', c: false }, ['b', 'c'])
|
|
1401
|
+
* console.log(result) // { a: 1 }
|
|
1200
1402
|
* ```
|
|
1201
1403
|
*
|
|
1202
|
-
* @example
|
|
1203
|
-
*
|
|
1204
|
-
* import { find } from '@superutils/core'
|
|
1404
|
+
* @example
|
|
1405
|
+
* #### Copy one object's properties to another while ignoring specific properties
|
|
1205
1406
|
*
|
|
1206
|
-
*
|
|
1207
|
-
*
|
|
1208
|
-
*
|
|
1209
|
-
*
|
|
1210
|
-
*
|
|
1211
|
-
*
|
|
1212
|
-
*
|
|
1213
|
-
*
|
|
1214
|
-
* })
|
|
1215
|
-
* // result: { name: 'Bob', age: 25 }
|
|
1407
|
+
* ```javascript
|
|
1408
|
+
* import { objWithoutKeys } from '@superutils/core'
|
|
1409
|
+
*
|
|
1410
|
+
* const source = { a: 1, b: '2', c: false }
|
|
1411
|
+
* const dest = { d: 4, e: 5 }
|
|
1412
|
+
* const result = objWithoutKeys(source, ['b', 'c'], dest)
|
|
1413
|
+
* console.log(result) // { d: 4, e: 5, a: 1 }
|
|
1414
|
+
* console.log(result === dest) // true
|
|
1216
1415
|
* ```
|
|
1217
1416
|
*/
|
|
1218
|
-
declare
|
|
1219
|
-
declare function find<K, V extends Record<string, unknown>, IncludeKey extends boolean = false, Return = undefined | (IncludeKey extends true ? [K, V] : V)>(data: IterableList<K, V>, options: FindOptions<K, V, IncludeKey>): Return;
|
|
1220
|
-
|
|
1221
|
-
/** Map entries as array */
|
|
1222
|
-
declare const getEntries: <K, V>(map: Map<K, V>) => [K, V][];
|
|
1417
|
+
declare const objWithoutKeys: (input: unknown, keys: string[], output?: Record<PropertyKey, unknown>) => Record<PropertyKey, unknown>;
|
|
1223
1418
|
|
|
1224
|
-
/**
|
|
1225
|
-
|
|
1419
|
+
/**
|
|
1420
|
+
* Sugar for `objReadOnly()` for an Array
|
|
1421
|
+
*
|
|
1422
|
+
* @param arr
|
|
1423
|
+
* @param config (optional)
|
|
1424
|
+
* @param config.add (optional) Whether to allow adding new properties. Default: `false`
|
|
1425
|
+
* @param config.revocable (optional) Default: `false`
|
|
1426
|
+
* @param config.silent (optional) Whether to throw error when a write operation is rejected. Default: `true`
|
|
1427
|
+
*
|
|
1428
|
+
* @returns Readonly Array or object containing readonly Array and revoke function
|
|
1429
|
+
*/
|
|
1430
|
+
declare const arrReadOnly: <T>(arr: T[], config?: Omit<ObjReadOnlyConfig<T[]>, "revoke">) => T[];
|
|
1226
1431
|
|
|
1227
|
-
/**
|
|
1228
|
-
|
|
1432
|
+
/**
|
|
1433
|
+
* @ignore exclude from documentation
|
|
1434
|
+
* Helper class for creating read-only arrays.
|
|
1435
|
+
*
|
|
1436
|
+
* Caution: This class can by itself only make the array partially read-only.
|
|
1437
|
+
* Use {@link arrReadOnly} instead.
|
|
1438
|
+
*/
|
|
1439
|
+
declare class ReadOnlyArrayHelper<T> extends Array<T> {
|
|
1440
|
+
readonly config: Omit<ObjReadOnlyConfig<T[]>, 'revoke'>;
|
|
1441
|
+
constructor(config: Omit<ObjReadOnlyConfig<T[]>, 'revoke'>, arr: T[]);
|
|
1442
|
+
private ignoreOrThrow;
|
|
1443
|
+
pop: () => T;
|
|
1444
|
+
push: (...items: T[]) => number;
|
|
1445
|
+
reverse: () => this;
|
|
1446
|
+
shift: () => T;
|
|
1447
|
+
splice: (..._ignoredArgs: unknown[]) => never[];
|
|
1448
|
+
unshift: (..._ignoredArgs: T[]) => number;
|
|
1449
|
+
}
|
|
1229
1450
|
|
|
1230
|
-
/**
|
|
1231
|
-
|
|
1451
|
+
/**
|
|
1452
|
+
* Reverse an array conditionally
|
|
1453
|
+
*
|
|
1454
|
+
* @param arr
|
|
1455
|
+
* @param reverse (optional) condition to reverse the array. Default: `true`
|
|
1456
|
+
* @param newArray (optional) whether to cnstruct new array or use input. Default: `false`
|
|
1457
|
+
*
|
|
1458
|
+
* @returns array
|
|
1459
|
+
*/
|
|
1460
|
+
declare const arrReverse: <T = unknown>(arr: T[], reverse?: boolean, newArray?: boolean) => T[];
|
|
1232
1461
|
|
|
1233
1462
|
/**
|
|
1234
|
-
*
|
|
1463
|
+
* Generate a Map from one or more arrays
|
|
1235
1464
|
*
|
|
1236
|
-
* @param
|
|
1237
|
-
* @param
|
|
1238
|
-
* @param
|
|
1465
|
+
* @param arr
|
|
1466
|
+
* @param key (optional) Array object-item property name or a function to generate keys for each array items.
|
|
1467
|
+
* @param flatDepth (optional) maximum recursion depth to flatten the array. Default: `0`
|
|
1239
1468
|
*
|
|
1240
|
-
* @returns
|
|
1469
|
+
* @returns Converted Map
|
|
1470
|
+
*
|
|
1471
|
+
* @example
|
|
1472
|
+
* #### Convert Array to Map
|
|
1473
|
+
* ```typescript
|
|
1474
|
+
* type Item = { a: number }
|
|
1475
|
+
* const arr: Item[] = [{ a: 1 }, { a: 2 }, { a: 3 }, [{ a: 4 }]]
|
|
1476
|
+
* const map: Map<number, Item> = arrToMap(
|
|
1477
|
+
* arr,
|
|
1478
|
+
* (_: Item, i: number) => item.a,
|
|
1479
|
+
* )
|
|
1480
|
+
* ```
|
|
1481
|
+
*
|
|
1482
|
+
* @example
|
|
1483
|
+
* #### Flatten and convert Array to Map
|
|
1484
|
+
* ```typescript
|
|
1485
|
+
* type Item = { key: number; value: string }
|
|
1486
|
+
* const arr: (Item | Item[])[] = [
|
|
1487
|
+
* { key: 1, value: 'a' },
|
|
1488
|
+
* { key: 2, value: 'b' },
|
|
1489
|
+
* { key: 3, value: 'c' },
|
|
1490
|
+
* [{ key: 4, value: 'd' }],
|
|
1491
|
+
* ]
|
|
1492
|
+
* const map = arrToMap(arr, (item: Item) => item.key, 1) // Map<number, Item>
|
|
1493
|
+
* ```
|
|
1241
1494
|
*/
|
|
1242
|
-
declare
|
|
1495
|
+
declare function arrToMap<T extends unknown[], FlatDepth extends number = 0, MapItem = FlatArray<T, FlatDepth>, KeyProp extends keyof MapItem = keyof MapItem>(arr: T, key: KeyProp, flatDepth?: FlatDepth): Map<MapItem[KeyProp], MapItem>;
|
|
1496
|
+
declare function arrToMap<T extends unknown[], FlatDepth extends number = 0>(arr: T, flatDepth?: FlatDepth): Map<number, FlatArray<T, FlatDepth>>;
|
|
1497
|
+
declare function arrToMap<T extends unknown[], FlatDepth extends number = 0, MapItem = FlatArray<T, FlatDepth>, MapKey = unknown>(arr: T, key: (item: MapItem, index: number, flatArr: MapItem[]) => MapKey, flatDepth?: FlatDepth): Map<MapKey, MapItem>;
|
|
1498
|
+
|
|
1499
|
+
/**
|
|
1500
|
+
* @function arrUnique
|
|
1501
|
+
* @summary constructs a new array of unique values
|
|
1502
|
+
*/
|
|
1503
|
+
declare const arrUnique: <T = unknown>(arr: T[], flatDepth?: number) => FlatArray<T, 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -1 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[];
|
|
1504
|
+
|
|
1505
|
+
/**
|
|
1506
|
+
* Debounce function options
|
|
1507
|
+
*/
|
|
1508
|
+
type DebounceOptions<ThisArg = unknown> = {
|
|
1509
|
+
leading?: boolean | 'global';
|
|
1510
|
+
onError?: (this: ThisArg, err: unknown) => ValueOrPromise<unknown>;
|
|
1511
|
+
thisArg?: ThisArg;
|
|
1512
|
+
tid?: TimeoutId;
|
|
1513
|
+
};
|
|
1514
|
+
/**
|
|
1515
|
+
* Deferred function options
|
|
1516
|
+
*/
|
|
1517
|
+
type DeferredOptions<ThisArg = unknown> = ({
|
|
1518
|
+
throttle: true;
|
|
1519
|
+
} & ThrottleOptions<ThisArg>) | ({
|
|
1520
|
+
throttle?: false;
|
|
1521
|
+
} & DebounceOptions<ThisArg>);
|
|
1522
|
+
/**
|
|
1523
|
+
* Throttle function options
|
|
1524
|
+
*/
|
|
1525
|
+
type ThrottleOptions<ThisArg = unknown> = {
|
|
1526
|
+
/**
|
|
1527
|
+
*
|
|
1528
|
+
* @param err Error object thrown by the callback function.
|
|
1529
|
+
* @returns
|
|
1530
|
+
*/
|
|
1531
|
+
onError?: (this: ThisArg, err: unknown) => ValueOrPromise<unknown>;
|
|
1532
|
+
thisArg?: ThisArg;
|
|
1533
|
+
trailing?: boolean;
|
|
1534
|
+
tid?: TimeoutId;
|
|
1535
|
+
};
|
|
1243
1536
|
|
|
1244
1537
|
/**
|
|
1245
|
-
*
|
|
1246
|
-
*
|
|
1247
|
-
* detailed, field-specific search using a query object.
|
|
1248
|
-
*
|
|
1249
|
-
* @param data The list of objects to search within. Compatible types include:
|
|
1250
|
-
* - `Array`
|
|
1251
|
-
* - `Map`
|
|
1252
|
-
* - `Set`
|
|
1253
|
-
* - `NodeList` (in DOM environments): `options.transform()` required
|
|
1254
|
-
* - `HTMLCollection` (in DOM environments): should accompany `options.transform()`
|
|
1255
|
-
* @param options The search criteria.
|
|
1256
|
-
* @param options.query The search query. Can be a string to search all fields, or an object for field-specific
|
|
1257
|
-
* @param options.ranked (optional) If `true`, the results are sorted by relevance (match index). Default: `false`.
|
|
1258
|
-
*
|
|
1259
|
-
* searches (e.g., `{ name: 'John', city: 'New York' }`).
|
|
1260
|
-
* @param options.asMap (optional) If `true`, returns a `Map`. If `false`, returns an `Array`. Default: `true`.
|
|
1261
|
-
* @param options.ignoreCase (optional) If `true`, performs a case-insensitive search for strings. Default: `true`.
|
|
1262
|
-
* @param options.limit (optional) The maximum number of results to return. Default: `Infinity`.
|
|
1263
|
-
* @param options.matchAll (optional) If `true`, an item must match all key-value pairs in the `query` object. If
|
|
1264
|
-
* `false`, it matches if at least one pair is found. Default: `false`.
|
|
1265
|
-
* @param options.matchExact (optional) If `true`, performs an exact match. If `false`, performs a partial match
|
|
1266
|
-
* (i.e., `includes()`). Default: `false`.
|
|
1267
|
-
* @param options.result (optional) An optional `Map` to which the results will be added.
|
|
1268
|
-
* @param options.transform (optional) Callback to transform item/item-property to string
|
|
1538
|
+
* Returns a function that invokes the callback function after certain delay/timeout.
|
|
1539
|
+
* All errors will be gracefully swallowed.
|
|
1269
1540
|
*
|
|
1270
|
-
* @
|
|
1541
|
+
* @param callback function to be invoked after timeout
|
|
1542
|
+
* @param delay (optional) timeout duration in milliseconds. Default: 50
|
|
1543
|
+
* @param config.onError (optional)
|
|
1544
|
+
* @param config.leading (optional) if true, will enable leading-edge debounce mechanism.
|
|
1545
|
+
* @param config.thisArg (optional) the special `thisArgs` to be used when invoking the callback.
|
|
1546
|
+
* @param config.tid (optional) Timeout Id. If provided, will clear the timeout on first invocation.
|
|
1271
1547
|
*
|
|
1272
1548
|
* @example
|
|
1273
|
-
*
|
|
1274
|
-
*
|
|
1275
|
-
*
|
|
1276
|
-
* { id: 2, name: 'Jane Doe', city: 'London' },
|
|
1277
|
-
* { id: 3, name: 'Peter Jones', city: 'New York' },
|
|
1278
|
-
* ];
|
|
1549
|
+
* #### Debounce function calls
|
|
1550
|
+
* ```javascript
|
|
1551
|
+
* import { debounce } from '@superutils/core'
|
|
1279
1552
|
*
|
|
1280
|
-
*
|
|
1281
|
-
*
|
|
1282
|
-
* //
|
|
1553
|
+
* const handleChange = debounce(
|
|
1554
|
+
* event => console.log('Value:', event.target.value),
|
|
1555
|
+
* 300 // debounce delay in milliseconds
|
|
1556
|
+
* )
|
|
1283
1557
|
*
|
|
1284
|
-
*
|
|
1285
|
-
*
|
|
1286
|
-
*
|
|
1287
|
-
* matchAll: true,
|
|
1288
|
-
* });
|
|
1289
|
-
* // Returns: [{ id: 3, ... }]
|
|
1558
|
+
* handleChange({ target: { value: 1 } }) // will be ignored
|
|
1559
|
+
* handleChange({ target: { value: 2 } }) // will be ingored
|
|
1560
|
+
* handleChange({ target: { value: 3 } }) // will be invoked
|
|
1290
1561
|
* ```
|
|
1291
1562
|
*/
|
|
1292
|
-
declare const
|
|
1293
|
-
<
|
|
1294
|
-
defaults:
|
|
1563
|
+
declare const debounce: {
|
|
1564
|
+
<TArgs extends unknown[], ThisArg>(callback: (this: ThisArg, ...args: TArgs) => ValueOrPromise<unknown>, delay?: number, config?: DebounceOptions<ThisArg>): (...args: TArgs) => void;
|
|
1565
|
+
defaults: {
|
|
1566
|
+
/**
|
|
1567
|
+
* Set the default value of argument `leading` for the `deferred` function.
|
|
1568
|
+
* This change is applicable application-wide and only applies to any new invocation of `deferred()`.
|
|
1569
|
+
*/
|
|
1570
|
+
leading: false;
|
|
1571
|
+
/**
|
|
1572
|
+
* Set the default value of argument `onError` for the `deferred` function.
|
|
1573
|
+
* This change is applicable application-wide and only applies to any new invocation of `deferred()`.
|
|
1574
|
+
*/
|
|
1575
|
+
onError: undefined;
|
|
1576
|
+
};
|
|
1295
1577
|
};
|
|
1578
|
+
|
|
1296
1579
|
/**
|
|
1297
|
-
*
|
|
1580
|
+
* Returns a function that can be used to debounce/throttle calls to the provided callback function.
|
|
1581
|
+
* All errors will be gracefully swallowed. See {@link debounce} and {@link throttle} for more information.
|
|
1298
1582
|
*
|
|
1299
|
-
* @
|
|
1583
|
+
* @param callback function to be invoked after delay
|
|
1584
|
+
* @param delay (optional) delay duration in milliseconds. Default: `50`
|
|
1585
|
+
* @param config (optional) debounce or throttle configuration options
|
|
1586
|
+
*
|
|
1587
|
+
* @returns Callback function that can be invoked in one of the following 2 methods:
|
|
1588
|
+
* - debounced: when `throttle` is `false` or `undefined`
|
|
1589
|
+
* - throttled: when `throttle` is `true`
|
|
1300
1590
|
*/
|
|
1301
|
-
declare
|
|
1302
|
-
{ query, ignoreCase, matchExact, transform, }: Pick<SearchOptions<K, V, boolean>, 'transform' | 'query' | 'ignoreCase' | 'matchExact'>, item: V, propertyName?: string): number;
|
|
1591
|
+
declare const deferred: <TArgs extends unknown[], ThisArg, Delay = unknown>(callback: (this: ThisArg, ...args: TArgs) => ValueOrPromise<unknown>, delay?: Delay, config?: DeferredOptions<ThisArg>) => (...args: TArgs) => void;
|
|
1303
1592
|
|
|
1304
|
-
type SliceMapTransform<Data, Value, Key> = (item: Value, key: Key, data: Data) => Value;
|
|
1305
|
-
type SliceMapOptions<Data, Value, Key, AsMap extends boolean = false> = {
|
|
1306
|
-
/** Whether to return the result as a Map (preserving original keys) or an Array */
|
|
1307
|
-
asMap?: AsMap;
|
|
1308
|
-
/** Callback to transform each item from the selected range */
|
|
1309
|
-
transform?: SliceMapTransform<Data, Value, Key>;
|
|
1310
|
-
/** End index (exclusive). Default: `undefined` (end of the list) */
|
|
1311
|
-
end?: number;
|
|
1312
|
-
/** Whether to exclude item if value is `undefined | null` */
|
|
1313
|
-
ignoreEmpty?: boolean;
|
|
1314
|
-
/** Start index. Default: `0` */
|
|
1315
|
-
start?: number;
|
|
1316
|
-
};
|
|
1317
1593
|
/**
|
|
1318
|
-
*
|
|
1319
|
-
*
|
|
1320
|
-
* @param data Array, Map, Set...
|
|
1321
|
-
* @param options One of the following is required to create a new list:
|
|
1322
|
-
* 1. A callback function {@link SliceMapTransform} to transform all items.
|
|
1323
|
-
* 2. Advanced options {@link SliceMapOptions}.
|
|
1324
|
-
* @param options.asMap (optional) whether return a Map or Array.
|
|
1325
|
-
* @param options.end (optional) End index (exclusive). Default: `undefined` (end of the list)
|
|
1326
|
-
* @param options.ignoreEmpty (optional) Whether to exclude item if value is `undefined | null`
|
|
1327
|
-
* @param options.start (optional) Default: `0`
|
|
1328
|
-
* @param options.transform (optional)
|
|
1594
|
+
* Returns a throttled function that ensures the callback is invoked at most once in the specified delay interval.
|
|
1595
|
+
* All errors will be gracefully swallowed.
|
|
1329
1596
|
*
|
|
1330
|
-
* If
|
|
1597
|
+
* If the throttled function is called multiple times during the delay interval, only the first call will invoke the callback immediately.
|
|
1331
1598
|
*
|
|
1332
|
-
*
|
|
1333
|
-
*
|
|
1334
|
-
* - key: index/key of the current item
|
|
1335
|
-
* - data: original list
|
|
1599
|
+
* If `trailing` is enabled and if returned function is invoked more than once during the delay interval,
|
|
1600
|
+
* the callback runs again at the end of the delay with the most recent arguments.
|
|
1336
1601
|
*
|
|
1337
|
-
* @
|
|
1602
|
+
* @param callback function to be invoked after timeout
|
|
1603
|
+
* @param delay (optional) interval duration in milliseconds. Default: 50
|
|
1604
|
+
* @param config (optional)
|
|
1605
|
+
* @param config.onError (optional) callback to be invoked on error
|
|
1606
|
+
* @param config.tid (optional)
|
|
1607
|
+
* @param config.thisArg (optional) the special `thisArgs` to be used when invoking the callback.
|
|
1608
|
+
* @param config.trailing (optional) whether to enable trailing edge execution. Default: `true`
|
|
1338
1609
|
*
|
|
1339
1610
|
* @example
|
|
1611
|
+
* #### Throttle function calls
|
|
1340
1612
|
* ```javascript
|
|
1341
|
-
*
|
|
1342
|
-
* [0, { age: 30, name: 'Alice' }],
|
|
1343
|
-
* [1, { age: 25, name: 'Bob' }],
|
|
1344
|
-
* [2, undefined],
|
|
1345
|
-
* [3, {}],
|
|
1346
|
-
* [4, { age: 35, name: 'Charlie' }],
|
|
1347
|
-
* [5, { age: 28, name: 'Dave' }],
|
|
1348
|
-
* [6, { age: 22, name: 'Eve' }],
|
|
1349
|
-
* ])
|
|
1350
|
-
* const result = sliceMap(data, {
|
|
1351
|
-
* asMap: false, // whether to return the result as a Map
|
|
1352
|
-
* end: 5, // last index (exclusive)
|
|
1353
|
-
* ignoreEmpty: true, // ignore items with no value
|
|
1354
|
-
* start: 1, // first index
|
|
1355
|
-
* })
|
|
1356
|
-
* console.log(result)
|
|
1357
|
-
* // [ { age: 25, name: 'Bob' }, { age: 35, name: 'Charlie' } ]
|
|
1358
|
-
* ```
|
|
1359
|
-
*/
|
|
1360
|
-
declare const sliceMap: <Data extends IterableList, Key = Data extends IterableList<infer Key_1, unknown> ? Key_1 : never, Value = Data extends IterableList<unknown, infer Value_1> ? Value_1 : never, AsMap extends boolean = false, Result = AsMap extends false ? Value[] : Map<Key, Value>>(data: Data, options?: SliceMapOptions<Data, Value, Key, AsMap> | SliceMapTransform<Data, Value, Key>) => Result;
|
|
1361
|
-
|
|
1362
|
-
type EntryComparator<K, V> = (a: [K, V], b: [K, V]) => number;
|
|
1363
|
-
type ArrayComparator<V> = (a: V, b: V) => number;
|
|
1364
|
-
/**
|
|
1365
|
-
* Sort iterable lists (Array/Map/Set).
|
|
1366
|
-
*
|
|
1613
|
+
* import { throttle } from '@superutils/core'
|
|
1367
1614
|
*
|
|
1368
|
-
*
|
|
1369
|
-
*
|
|
1370
|
-
*
|
|
1371
|
-
*
|
|
1372
|
-
*
|
|
1373
|
-
*
|
|
1374
|
-
*
|
|
1375
|
-
* @param options.reverse (optional) True: accending sort. False: descending sort. Default: `false`
|
|
1376
|
-
* @param options.undefinedFirst (optional) Where to place undefined/null values.
|
|
1377
|
-
* Not avaible when `comparator` function is used.
|
|
1378
|
-
* - `true`: at the beginning
|
|
1379
|
-
* - `false`: at the end
|
|
1615
|
+
* const handleChange = throttle(
|
|
1616
|
+
* event => console.log('Value:', event.target.value),
|
|
1617
|
+
* 300, // throttle duration in milliseconds
|
|
1618
|
+
* )
|
|
1619
|
+
* handleChange({ target: { value: 1 } }) // will be executed
|
|
1620
|
+
* handleChange({ target: { value: 2 } }) // will be ignored
|
|
1621
|
+
* handleChange({ target: { value: 3 } }) // will be ignored
|
|
1380
1622
|
*
|
|
1381
|
-
*
|
|
1623
|
+
* setTimeout(() => {
|
|
1624
|
+
* handleChange({ target: { value: 4 } }) // will be executed (after 300ms)
|
|
1625
|
+
* handleChange({ target: { value: 5 } }) // will be ignored
|
|
1626
|
+
* }, 400)
|
|
1627
|
+
* ```
|
|
1382
1628
|
*
|
|
1383
|
-
* @
|
|
1629
|
+
* @example
|
|
1630
|
+
* #### Throttle with trailing enabled
|
|
1631
|
+
* ```javascript
|
|
1632
|
+
* import { throttle } from '@superutils/core'
|
|
1384
1633
|
*
|
|
1385
|
-
*
|
|
1386
|
-
*
|
|
1387
|
-
*
|
|
1388
|
-
*
|
|
1389
|
-
*
|
|
1390
|
-
*
|
|
1391
|
-
*
|
|
1392
|
-
* ])
|
|
1393
|
-
* sort(map)
|
|
1394
|
-
* // result: Map(3) { 0 => 0, 1 => 1, 2 => 2 }
|
|
1395
|
-
* ```
|
|
1634
|
+
* const handleChange = throttle(
|
|
1635
|
+
* event => console.log('Value:', event.target.value),
|
|
1636
|
+
* 300, // throttle duration in milliseconds
|
|
1637
|
+
* )
|
|
1638
|
+
* handleChange({ target: { value: 1 } }) // will be executed
|
|
1639
|
+
* handleChange({ target: { value: 2 } }) // will be ignored
|
|
1640
|
+
* handleChange({ target: { value: 3 } }) // will be executed
|
|
1396
1641
|
*
|
|
1397
|
-
*
|
|
1398
|
-
*
|
|
1399
|
-
*
|
|
1400
|
-
*
|
|
1401
|
-
* [0, { name: 'Charlie' }],
|
|
1402
|
-
* [1, { name: 'Alice' }],
|
|
1403
|
-
* [2, { name: 'Bob' }],
|
|
1404
|
-
* ])
|
|
1405
|
-
* sort(map, 'name')
|
|
1406
|
-
* // result: Map(3) {
|
|
1407
|
-
* // 1 => { name: 'Alice' },
|
|
1408
|
-
* // 2 => { name: 'Bob' },
|
|
1409
|
-
* // 0 => { name: 'Charlie' }
|
|
1410
|
-
* // }
|
|
1642
|
+
* setTimeout(() => {
|
|
1643
|
+
* handleChange({ target: { value: 4 } }) // will be executed
|
|
1644
|
+
* handleChange({ target: { value: 5 } }) // will be ignored
|
|
1645
|
+
* }, 400)
|
|
1411
1646
|
* ```
|
|
1412
1647
|
*/
|
|
1413
|
-
declare
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
/** Sort Array/Map/Set with `string | boolean | number` values */
|
|
1423
|
-
declare function sort<K, V extends string | boolean | number, T extends IterableList<K, V>>(data: T, options?: SortOptions): T;
|
|
1424
|
-
declare namespace sort {
|
|
1425
|
-
var defaults: {
|
|
1426
|
-
ignoreCase: true;
|
|
1427
|
-
newInstance: false;
|
|
1428
|
-
reverse: false;
|
|
1429
|
-
undefinedFirst: false;
|
|
1648
|
+
declare const throttle: {
|
|
1649
|
+
<TArgs extends unknown[], ThisArg>(callback: (this: ThisArg, ...args: TArgs) => ValueOrPromise<unknown>, delay?: number, config?: ThrottleOptions<ThisArg>): (...args: TArgs) => void;
|
|
1650
|
+
/**
|
|
1651
|
+
* Set the default values
|
|
1652
|
+
* This change is applicable application-wide and only applies to any new invocation of `throttle()`.
|
|
1653
|
+
*/
|
|
1654
|
+
defaults: {
|
|
1655
|
+
onError: undefined;
|
|
1656
|
+
trailing: false;
|
|
1430
1657
|
};
|
|
1431
|
-
}
|
|
1658
|
+
};
|
|
1659
|
+
/**
|
|
1660
|
+
* For legacy compatibility
|
|
1661
|
+
* @deprecated use `throttle` instead
|
|
1662
|
+
*/
|
|
1663
|
+
declare const throttled: {
|
|
1664
|
+
<TArgs extends unknown[], ThisArg>(callback: (this: ThisArg, ...args: TArgs) => ValueOrPromise<unknown>, delay?: number, config?: ThrottleOptions<ThisArg>): (...args: TArgs) => void;
|
|
1665
|
+
/**
|
|
1666
|
+
* Set the default values
|
|
1667
|
+
* This change is applicable application-wide and only applies to any new invocation of `throttle()`.
|
|
1668
|
+
*/
|
|
1669
|
+
defaults: {
|
|
1670
|
+
onError: undefined;
|
|
1671
|
+
trailing: false;
|
|
1672
|
+
};
|
|
1673
|
+
};
|
|
1432
1674
|
|
|
1433
1675
|
/**
|
|
1434
1676
|
* Creates a new Map by combining two or more Maps
|
|
@@ -1437,27 +1679,26 @@ declare namespace sort {
|
|
|
1437
1679
|
*
|
|
1438
1680
|
* @returns new combined Map
|
|
1439
1681
|
*
|
|
1440
|
-
* @example
|
|
1441
|
-
*
|
|
1682
|
+
* @example
|
|
1683
|
+
* #### Join two Maps
|
|
1684
|
+
* ```javascript
|
|
1442
1685
|
* import { mapJoin } from '@superutils/core'
|
|
1443
1686
|
*
|
|
1444
|
-
* const
|
|
1445
|
-
*
|
|
1446
|
-
*
|
|
1447
|
-
* ]
|
|
1448
|
-
* const joined = mapJoin(...maps)
|
|
1687
|
+
* const map1 = new Map([['a', 1]])
|
|
1688
|
+
* const map2 = new Map([['b', 2]])
|
|
1689
|
+
* console.log(mapJoin(map1, map2))
|
|
1449
1690
|
* // Result: Map(2) {'a' => 1, 'b' => 2}
|
|
1450
1691
|
* ```
|
|
1451
1692
|
*
|
|
1452
|
-
* @example
|
|
1453
|
-
*
|
|
1693
|
+
* @example
|
|
1694
|
+
* #### Join entries and Maps into a single Map
|
|
1695
|
+
* ```javascript
|
|
1454
1696
|
* import { mapJoin } from '@superutils/core'
|
|
1455
1697
|
*
|
|
1456
|
-
* const
|
|
1457
|
-
*
|
|
1458
|
-
*
|
|
1459
|
-
*
|
|
1460
|
-
* )
|
|
1698
|
+
* const map1 = new Map([['a', 1]])
|
|
1699
|
+
* const entries = [['b', 2], ['c', 2]]
|
|
1700
|
+
* const map2 = new Map([['c', 3]])
|
|
1701
|
+
* console.log(mapJoin(map1, entries, map2))
|
|
1461
1702
|
* // Result: Map(2) {'a' => 1, 'b' => 2, 'c' => 3}
|
|
1462
1703
|
* ```
|
|
1463
1704
|
*/
|
|
@@ -1558,4 +1799,4 @@ declare const HASH_REGEX: RegExp;
|
|
|
1558
1799
|
*/
|
|
1559
1800
|
declare const strToArr: (value: unknown, seperator?: string) => string[];
|
|
1560
1801
|
|
|
1561
|
-
export { type ArrayComparator, type AsyncFn, type CreateTuple, type CurriedArgs, type Curry, type DebounceOptions, type DeferredOptions, type DropFirst, type DropFirstN, type DropLast, EMAIL_REGEX, type EntryComparator, type FindOptions, type FiniteNumber, HASH_REGEX, HEX_REGEX, type IfPromiseAddValue, type Integer, type IsFiniteTuple, type IsOptional, type IterableList, type KeepFirst, type KeepFirstN, type KeepOptionals, type KeepRequired, type MakeOptional, type MinLength, type NegativeInteger, type NegativeNumber, type
|
|
1802
|
+
export { type ArrayComparator, type AsyncFn, type CreateTuple, type CurriedArgs, type Curry, type DebounceOptions, type DeferredOptions, type DropFirst, type DropFirstN, type DropLast, EMAIL_REGEX, type EntryComparator, type FindOptions, type FiniteNumber, HASH_REGEX, HEX_REGEX, type IfPromiseAddValue, type Integer, type IsFiniteTuple, type IsOptional, type IterableList, type KeepFirst, type KeepFirstN, type KeepOptionals, type KeepRequired, type MakeOptional, type MinLength, type NegativeInteger, type NegativeNumber, type ObjReadOnlyAllowAddFn, type ObjReadOnlyConfig, type OptionalIf, type PositiveInteger, type PositiveNumber, ReadOnlyArrayHelper, type SearchOptions, type SetObPropPredicate, type Slice, type SliceMapOptions, type SliceMapTransform, type SortOptions, type ThrottleOptions, type TimeoutId, type TupleMaxLength, type TupleWithAlt, type ValueOrFunc, type ValueOrPromise, arrReadOnly, arrReverse, arrToMap, arrUnique, clearClutter, copyToClipboard, curry, debounce, deferred, fallbackIfFails, filter, find, getEntries, getKeys, getSize, getUrlParam, getValues, is, isArr, isArr2D, isArrLike, isArrLikeSafe, isArrObj, isArrUnique, isAsyncFn, isBool, isDate, isDateValid, isDefined, isEmpty, isEmptySafe, isEnvBrowser, isEnvNode, isEnvTouchable, isError, isFn, isInteger, isMap, isMapObj, isNegativeInteger, isNegativeNumber, isNumber, isObj, isPositiveInteger, isPositiveNumber, isPromise, isRegExp, isSet, isStr, isSubjectLike, isSymbol, isUint8Arr, isUrl, isUrlValid, mapJoin, matchObjOrProp, noop, noopAsync, objClean, objCopy, objCreate, objHasKeys, objKeys, objReadOnly, objSetProp, objSetPropUndefined, objSort, objWithoutKeys, randomInt, reverse, search, sliceMap, sort, strToArr, throttle, throttled, toDatetimeLocal };
|