@superutils/core 1.1.4 → 1.1.6
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 +135 -10
- package/dist/index.d.ts +304 -132
- package/dist/index.js +92 -78
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,11 +13,16 @@ For full API reference check out the [docs page](https://alien45.github.io/super
|
|
|
13
13
|
- [Installation](#installation)
|
|
14
14
|
- [Usage](#usage)
|
|
15
15
|
- [`is`](#is): Type checkers
|
|
16
|
-
- [`
|
|
16
|
+
- [`debounce()`](#debounce): Debounce callbacks
|
|
17
17
|
- [`throttle()`](#throttle): Throttle callbacks
|
|
18
|
+
- [`deferred()`](#deferred): Debounce/Throttle callbacks
|
|
18
19
|
- [`fallbackIfFails()`](#fallback-if-fails): Gracefully invoke functions or promises with a fallback
|
|
19
20
|
- [`objCopy()`](#obj-copy): Deep-copy objects
|
|
20
21
|
- [`search()`](#search): Search iterable collections
|
|
22
|
+
- [Advanced search options](#search-advanced)
|
|
23
|
+
- [`Ranked search`](#search-ranked): sort results by relevance
|
|
24
|
+
- [Combine `search()` with `deferred()`](#search-deferred): simulate a search input with debounce mechanism
|
|
25
|
+
- [`curry()`](#curry): Convert any function into a curried function
|
|
21
26
|
|
|
22
27
|
## Installation
|
|
23
28
|
|
|
@@ -72,27 +77,29 @@ import {
|
|
|
72
77
|
} from '@superutils/core'
|
|
73
78
|
```
|
|
74
79
|
|
|
75
|
-
<div id="
|
|
76
|
-
|
|
77
|
-
### `deferred(fn)`: debounce callbacks
|
|
80
|
+
<div id="debouce"></div>
|
|
78
81
|
|
|
79
|
-
`
|
|
82
|
+
### `debouce(fn, delay, options)`: debounce callbacks
|
|
80
83
|
|
|
81
84
|
```javascript
|
|
82
|
-
import {
|
|
85
|
+
import { debouce } from '@superutils/core'
|
|
83
86
|
|
|
84
|
-
const handleChange =
|
|
87
|
+
const handleChange = debouce(
|
|
85
88
|
event => console.log(event.target.value),
|
|
86
89
|
300, // debounce delay in milliseconds
|
|
90
|
+
{
|
|
91
|
+
leading: false, // default
|
|
92
|
+
},
|
|
87
93
|
)
|
|
88
|
-
handleChange({ target: { value: 1 } }) // will be ignored
|
|
94
|
+
handleChange({ target: { value: 1 } }) // will be ignored, unless `leading = true`
|
|
89
95
|
handleChange({ target: { value: 2 } }) // will be ignored
|
|
90
|
-
handleChange({ target: { value: 3 } }) // will be
|
|
96
|
+
handleChange({ target: { value: 3 } }) // will be ignored
|
|
97
|
+
handleChange({ target: { value: 4 } }) // will be executed
|
|
91
98
|
```
|
|
92
99
|
|
|
93
100
|
<div id="throttle"></div>
|
|
94
101
|
|
|
95
|
-
### `throttle(fn)`: throttle callbacks
|
|
102
|
+
### `throttle(fn, delay, options)`: throttle callbacks
|
|
96
103
|
|
|
97
104
|
```javascript
|
|
98
105
|
import { throttle } from '@superutils/core'
|
|
@@ -100,10 +107,33 @@ import { throttle } from '@superutils/core'
|
|
|
100
107
|
const handleChange = throttle(
|
|
101
108
|
event => console.log(event.target.value),
|
|
102
109
|
300, // throttle duration in milliseconds
|
|
110
|
+
{
|
|
111
|
+
trailing: false, // default
|
|
112
|
+
},
|
|
103
113
|
)
|
|
104
114
|
handleChange({ target: { value: 1 } }) // will be executed
|
|
105
115
|
handleChange({ target: { value: 2 } }) // will be ignored
|
|
106
116
|
handleChange({ target: { value: 3 } }) // will be ignored
|
|
117
|
+
handleChange({ target: { value: 4 } }) // will be ignored, unless `trailing = true`
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
<div id="deferred"></div>
|
|
121
|
+
|
|
122
|
+
### `deferred(fn, delay, options)`: debounce/throttle callbacks
|
|
123
|
+
|
|
124
|
+
Create debounced/throttled functions using the `throttle` switch.
|
|
125
|
+
|
|
126
|
+
```javascript
|
|
127
|
+
import { deferred } from '@superutils/core'
|
|
128
|
+
|
|
129
|
+
const handleChange = deferred(
|
|
130
|
+
event => console.log(event.target.value),
|
|
131
|
+
300, // delay in milliseconds
|
|
132
|
+
{ throttle: false }, // determines whether to create a debounced or throttled function
|
|
133
|
+
)
|
|
134
|
+
handleChange({ target: { value: 1 } }) // will be ignored
|
|
135
|
+
handleChange({ target: { value: 2 } }) // will be ignored
|
|
136
|
+
handleChange({ target: { value: 3 } }) // will be executed
|
|
107
137
|
```
|
|
108
138
|
|
|
109
139
|
<div id="fallback-if-fails"></div>
|
|
@@ -236,6 +266,8 @@ search(data, { query: /li/i }) // Using regular expression
|
|
|
236
266
|
// ])
|
|
237
267
|
```
|
|
238
268
|
|
|
269
|
+
<div id="search-advanced"></div>
|
|
270
|
+
|
|
239
271
|
#### Advanced Search Options:
|
|
240
272
|
|
|
241
273
|
```javascript
|
|
@@ -275,3 +307,96 @@ search(data, {
|
|
|
275
307
|
// { age: 28, name: 'Dave' }
|
|
276
308
|
// ]
|
|
277
309
|
```
|
|
310
|
+
|
|
311
|
+
<div id="search-ranked"></div>
|
|
312
|
+
|
|
313
|
+
#### Search Ranked: sort results by relevance
|
|
314
|
+
|
|
315
|
+
When `ranked` is set to `true`, results are sorted by relevance. In this example, "Alice" is ranked higher than "Charlie" because the match "li" appears earlier in the string.
|
|
316
|
+
|
|
317
|
+
```javascript
|
|
318
|
+
import { search } from '@superutils/core'
|
|
319
|
+
|
|
320
|
+
// Sample colletion
|
|
321
|
+
const data = new Map([
|
|
322
|
+
[2, { age: 25, name: 'Bob' }],
|
|
323
|
+
[3, { age: 35, name: 'Charlie' }],
|
|
324
|
+
[4, { age: 28, name: 'Dave' }],
|
|
325
|
+
[5, { age: 22, name: 'Eve' }],
|
|
326
|
+
[1, { age: 30, name: 'Alice' }],
|
|
327
|
+
])
|
|
328
|
+
const result = search(data, {
|
|
329
|
+
asMap: false, // Result type: true => Map (default, keys preserved), false => Array
|
|
330
|
+
limit: 10, // Number of items returned. Default: no limit
|
|
331
|
+
query: /li/i,
|
|
332
|
+
ranked: true,
|
|
333
|
+
})
|
|
334
|
+
console.log(result)
|
|
335
|
+
// [ { age: 30, name: 'Alice' }, { age: 35, name: 'Charlie' } ]
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
<div id="search-deferred"></div>
|
|
339
|
+
|
|
340
|
+
#### Combine `search()` with `deferred()`: simulate a search input with debounce mechanism
|
|
341
|
+
|
|
342
|
+
```javascript
|
|
343
|
+
import { deferred, search } from '@superutils/core'
|
|
344
|
+
|
|
345
|
+
// sample colletion
|
|
346
|
+
const data = new Map([
|
|
347
|
+
[1, { age: 30, name: 'Alice' }],
|
|
348
|
+
[2, { age: 25, name: 'Bob' }],
|
|
349
|
+
[3, { age: 35, name: 'Charlie' }],
|
|
350
|
+
[4, { age: 28, name: 'Dave' }],
|
|
351
|
+
[5, { age: 22, name: 'Eve' }],
|
|
352
|
+
])
|
|
353
|
+
|
|
354
|
+
const searchDeferred = deferred(
|
|
355
|
+
event => {
|
|
356
|
+
const result = search(data, {
|
|
357
|
+
query: {
|
|
358
|
+
name: new RegExp(event?.target?.value, 'i'),
|
|
359
|
+
},
|
|
360
|
+
})
|
|
361
|
+
// print result to console
|
|
362
|
+
console.log(result)
|
|
363
|
+
},
|
|
364
|
+
300, // debounce duration in milliseconds
|
|
365
|
+
{ leading: false }, // optional defer options
|
|
366
|
+
)
|
|
367
|
+
|
|
368
|
+
// ignored
|
|
369
|
+
searchDeferred({ target: { value: 'l' } })
|
|
370
|
+
// ignored
|
|
371
|
+
setTimeout(() => searchDeferred({ target: { value: 'li' } }), 50)
|
|
372
|
+
// executed: prints `Map(1) { 3 => { age: 35, name: 'Charlie' } }`
|
|
373
|
+
setTimeout(() => searchDeferred({ target: { value: 'lie' } }), 200)
|
|
374
|
+
// executed: prints `Map(1) { 1 => { age: 30, name: 'Alice' } }`
|
|
375
|
+
setTimeout(() => searchDeferred({ target: { value: 'lic' } }), 510)
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
<div id="curry"></div>
|
|
379
|
+
|
|
380
|
+
### `curry(fn, arity)`: Convert any function into a curried function
|
|
381
|
+
|
|
382
|
+
```typescript
|
|
383
|
+
const func = (
|
|
384
|
+
first: string,
|
|
385
|
+
second: number,
|
|
386
|
+
third?: boolean,
|
|
387
|
+
fourth?: string,
|
|
388
|
+
) => `${first}-${second}-${third}-${fourth}`
|
|
389
|
+
// We create a new function from the `func()` function that acts like a type-safe curry function
|
|
390
|
+
// while also being flexible with the number of arguments supplied.
|
|
391
|
+
const curriedFunc = curry(func)
|
|
392
|
+
|
|
393
|
+
// Example 1: usage like a regular curry function and provide one argument at a time.
|
|
394
|
+
// Returns a function expecting args: second, third, fourth
|
|
395
|
+
const fnWith1 = curriedFunc('first')
|
|
396
|
+
// Returns a function expecting args: third, fourth
|
|
397
|
+
const fnWith2 = fnWith1(2)
|
|
398
|
+
// returns a function epecting only fourth arg
|
|
399
|
+
const fnWith3 = fnWith2(false)
|
|
400
|
+
// All args are now provided, the original function is called and result is returned.
|
|
401
|
+
const result = fnWith3('last')
|
|
402
|
+
```
|
package/dist/index.d.ts
CHANGED
|
@@ -28,15 +28,6 @@ type CurriedArgs<TArgs extends unknown[], TArgsIsFinite extends boolean, TFunc e
|
|
|
28
28
|
...KeepRequired<TArgs>,
|
|
29
29
|
...KeepOptionals<TArgs, true, undefined>
|
|
30
30
|
], TArity>;
|
|
31
|
-
/**
|
|
32
|
-
* Deferred function options
|
|
33
|
-
*/
|
|
34
|
-
type DeferredOptions<ThisArg = unknown> = {
|
|
35
|
-
leading?: boolean | 'global';
|
|
36
|
-
onError?: (err: unknown) => ValueOrPromise<unknown>;
|
|
37
|
-
thisArg?: ThisArg;
|
|
38
|
-
tid?: TimeoutId;
|
|
39
|
-
};
|
|
40
31
|
/**
|
|
41
32
|
* Drop the first item from an array/tuple and keep the rest
|
|
42
33
|
* ---
|
|
@@ -67,6 +58,13 @@ type DropFirstN<T extends unknown[], N extends number, Dropped extends unknown[]
|
|
|
67
58
|
* ```
|
|
68
59
|
*/
|
|
69
60
|
type DropLast<T extends unknown[]> = T extends [...infer Rest, unknown] ? Rest : [];
|
|
61
|
+
/**
|
|
62
|
+
* If `T` is a promise turn it into an union type by adding the value type
|
|
63
|
+
*/
|
|
64
|
+
type IfPromiseAddValue<T> = T extends Promise<infer V> ? T | V : T;
|
|
65
|
+
/**
|
|
66
|
+
* Check if a tuple/array has a finite length
|
|
67
|
+
*/
|
|
70
68
|
type IsFiniteTuple<T extends unknown[]> = number extends T['length'] ? false : true;
|
|
71
69
|
type IsOptional<T, K extends keyof T> = {} extends Pick<T, K> ? true : false;
|
|
72
70
|
/**
|
|
@@ -188,7 +186,10 @@ type TupleWithAlt<Tuple extends unknown[], TAlt = undefined> = {
|
|
|
188
186
|
* ```
|
|
189
187
|
*/
|
|
190
188
|
type ValueOrFunc<Value, Args extends unknown[] = []> = Value | ((...args: Args) => Value);
|
|
191
|
-
/**
|
|
189
|
+
/**
|
|
190
|
+
* Represents a value that can be either a direct value of type `T` or a `Promise` that resolves to `T`.
|
|
191
|
+
* This is useful for functions that can handle both synchronous and asynchronous results.
|
|
192
|
+
*/
|
|
192
193
|
type ValueOrPromise<T> = T | Promise<T>;
|
|
193
194
|
|
|
194
195
|
/**
|
|
@@ -254,80 +255,20 @@ declare function curry<TData, TArgs extends unknown[], TArgsIsFinite extends boo
|
|
|
254
255
|
arity: TArity
|
|
255
256
|
]): Curry<TData, CurriedArgs<TArgs, TArgsIsFinite, (...args: TArgs) => TData, TArity>>;
|
|
256
257
|
|
|
257
|
-
/**
|
|
258
|
-
*
|
|
259
|
-
* Returns a function that invokes the callback function after certain delay/timeout.
|
|
260
|
-
* All errors will be gracefully swallowed.
|
|
261
|
-
*
|
|
262
|
-
* @param callback function to be invoked after timeout
|
|
263
|
-
* @param delay (optional) timeout duration in milliseconds. Default: 50
|
|
264
|
-
* @param config.onError (optional)
|
|
265
|
-
* @param config.leading (optional) if true, will enable leading-edge debounce mechanism.
|
|
266
|
-
* @param config.thisArg (optional) the special `thisArgs` to be used when invoking the callback.
|
|
267
|
-
* @param config.tid (optional) Timeout Id. If provided, will clear the timeout on first invocation.
|
|
268
|
-
*
|
|
269
|
-
* @example Debounce function calls
|
|
270
|
-
* ```javascript
|
|
271
|
-
* import { deferred } from '@superutils/core'
|
|
272
|
-
*
|
|
273
|
-
* const handleChange = deferred(
|
|
274
|
-
* event => console.log('Value:', event.target.value),
|
|
275
|
-
* 300 // debounce delay in milliseconds
|
|
276
|
-
* )
|
|
277
|
-
*
|
|
278
|
-
* handleChange({ target: { value: 1 } }) // will be ignored
|
|
279
|
-
* handleChange({ target: { value: 2 } }) // will be ingored
|
|
280
|
-
* handleChange({ target: { value: 3 } }) // will be invoked
|
|
281
|
-
* ```
|
|
282
|
-
*/
|
|
283
|
-
declare const deferred: {
|
|
284
|
-
<TArgs extends unknown[], ThisArg>(callback: (this: ThisArg, ...args: TArgs) => ValueOrPromise<unknown>, delay?: number, config?: DeferredOptions<ThisArg>): (...args: TArgs) => void;
|
|
285
|
-
defaults: {
|
|
286
|
-
/**
|
|
287
|
-
* Set the default value of argument `leading` for the `deferred` function.
|
|
288
|
-
* This change is applicable application-wide and only applies to any new invocation of `deferred()`.
|
|
289
|
-
*/
|
|
290
|
-
leading: false;
|
|
291
|
-
/**
|
|
292
|
-
* Set the default value of argument `onError` for the `deferred` function.
|
|
293
|
-
* This change is applicable application-wide and only applies to any new invocation of `deferred()`.
|
|
294
|
-
*/
|
|
295
|
-
onError: undefined;
|
|
296
|
-
};
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
/** Super for `deferred()` function */
|
|
300
|
-
declare const debounce: {
|
|
301
|
-
<TArgs extends unknown[], ThisArg>(callback: (this: ThisArg, ...args: TArgs) => ValueOrPromise<unknown>, delay?: number, config?: DeferredOptions<ThisArg>): (...args: TArgs) => void;
|
|
302
|
-
defaults: {
|
|
303
|
-
leading: false;
|
|
304
|
-
onError: undefined;
|
|
305
|
-
};
|
|
306
|
-
};
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* If `T` is a promise turn it into an union type by adding the value type
|
|
310
|
-
*/
|
|
311
|
-
type IfPromiseAddValue<T> = T extends Promise<infer V> ? T | V : T;
|
|
312
258
|
/**
|
|
313
259
|
* @function fallbackIfFails
|
|
314
260
|
* @summary a flexible try-catch wrapper for invoking functions and ignore errors gracefully.
|
|
315
261
|
* Yes, the goal of `fallbackIfFails` is to ignore all runtime errors
|
|
316
262
|
* and ensure there's always a value returned.
|
|
317
263
|
*
|
|
318
|
-
*
|
|
319
|
-
*
|
|
320
|
-
*
|
|
264
|
+
* @param target promise or function to execute
|
|
265
|
+
* @param args arguments to be supplied to `func` fuction
|
|
266
|
+
* @param fallback alternative value to be used when target throws error.
|
|
321
267
|
*
|
|
322
|
-
*
|
|
323
|
-
* A fallback of the fallback is out of the scope of
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
* ---
|
|
327
|
-
*
|
|
328
|
-
* @param target promise or function to execute
|
|
329
|
-
* @param args arguments to be supplied to `func` fuction
|
|
330
|
-
* @param fallbackValue alternative value to be used when target throws error.
|
|
268
|
+
* - If `fallback` is a function and it raises exception, it will cause to raise an exception instead of gracefully
|
|
269
|
+
* ignoring it. A fallback of the fallback function is out of the scope of `fallbackIfFails`. However, the fallback
|
|
270
|
+
* function can still be wrapped inside a secondary `fallbackIfFails`. See the "Fallback chaining" example below.
|
|
271
|
+
* - If `target` a promise or async function, `fallback` can be either be a value, a promise or an async function.
|
|
331
272
|
*
|
|
332
273
|
* @returns if func is a promise the return a promise
|
|
333
274
|
*
|
|
@@ -335,7 +276,10 @@ type IfPromiseAddValue<T> = T extends Promise<infer V> ? T | V : T;
|
|
|
335
276
|
* ---
|
|
336
277
|
* @example Async functions
|
|
337
278
|
* Working with async functions or functions that returns a promise
|
|
279
|
+
*
|
|
338
280
|
* ```typescript
|
|
281
|
+
* import { fallbackIfFails } from '@superutils/core'
|
|
282
|
+
*
|
|
339
283
|
* const args = ['some value', true] as const
|
|
340
284
|
* const ensureValue = async (value: string, criteria?: boolean) => {
|
|
341
285
|
* if (criteria !== false && !value.trim()) throw new Error('No value. Should use fallback value')
|
|
@@ -351,7 +295,10 @@ type IfPromiseAddValue<T> = T extends Promise<infer V> ? T | V : T;
|
|
|
351
295
|
*
|
|
352
296
|
* @example Non-async functions
|
|
353
297
|
* Working synchronous function that returns value synchronously
|
|
298
|
+
*
|
|
354
299
|
* ```typescript
|
|
300
|
+
* import { fallbackIfFails } from '@superutils/core'
|
|
301
|
+
*
|
|
355
302
|
* const args = ['some value', true] as const
|
|
356
303
|
* const ensureValue = (value: string, criteria?: boolean) => {
|
|
357
304
|
* if (criteria !== false && !value.trim()) throw new Error('No value. Should use fallback value')
|
|
@@ -367,7 +314,10 @@ type IfPromiseAddValue<T> = T extends Promise<infer V> ? T | V : T;
|
|
|
367
314
|
*
|
|
368
315
|
* @example Hybrid functions
|
|
369
316
|
* Working with function that returns value sync/async circumstantially
|
|
317
|
+
*
|
|
370
318
|
* ```typescript
|
|
319
|
+
* import { fallbackIfFails } from '@superutils/core'
|
|
320
|
+
*
|
|
371
321
|
* const getData = (useCache = true, cacheKey = 'data-cache') => {
|
|
372
322
|
* if (useCache && localStorage[cacheKey]) return localStorage[cacheKey]
|
|
373
323
|
* return fetch('https://my.domain.com/api')
|
|
@@ -382,8 +332,29 @@ type IfPromiseAddValue<T> = T extends Promise<infer V> ? T | V : T;
|
|
|
382
332
|
* // Second call: cache available and will return data synchronously
|
|
383
333
|
* const second = fallbackIfFails(getData, [true], {})
|
|
384
334
|
* ```
|
|
335
|
+
*
|
|
336
|
+
* @example Fallback-chaining: gracefully handle the fallback function
|
|
337
|
+
*
|
|
338
|
+
* ```typescript
|
|
339
|
+
* import { fallbackIfFails } from '@superutils/core'
|
|
340
|
+
*
|
|
341
|
+
* const target = () => {
|
|
342
|
+
* if (new Date().getTime() > 0) throw new Error('I will raise error')
|
|
343
|
+
* }
|
|
344
|
+
* const fallback = () => {
|
|
345
|
+
* throw new Error('I will also raise an exception')
|
|
346
|
+
* }
|
|
347
|
+
* const value = fallbackIfFails(
|
|
348
|
+
* target,
|
|
349
|
+
* [],
|
|
350
|
+
* // this function will only be invoked when
|
|
351
|
+
* () => fallbackIfFails(fallback, [], undefined)
|
|
352
|
+
* )
|
|
353
|
+
*
|
|
354
|
+
* console.log({ value }) // undefined
|
|
355
|
+
* ```
|
|
385
356
|
*/
|
|
386
|
-
declare const fallbackIfFails: <T, TArgs extends unknown[] = unknown[]>(target: T | ((...args: TArgs) => T), args: TArgs | (() => TArgs),
|
|
357
|
+
declare const fallbackIfFails: <T, TArgs extends unknown[] = unknown[]>(target: T | ((...args: TArgs) => T), args: TArgs | (() => TArgs), fallback: IfPromiseAddValue<T> | ((reason: unknown) => IfPromiseAddValue<T>)) => T;
|
|
387
358
|
|
|
388
359
|
/** Check if value is an array */
|
|
389
360
|
declare const isArr: <Item = unknown>(x: unknown) => x is Item[];
|
|
@@ -514,12 +485,16 @@ declare const isMapObj: <K extends PropertyKey, V, T extends Record<K, V>>(x: un
|
|
|
514
485
|
|
|
515
486
|
/** Check if value is an integer */
|
|
516
487
|
declare const isInteger: (x: unknown) => x is number;
|
|
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;
|
|
517
494
|
/** Check if value is a positive integer */
|
|
518
495
|
declare const isPositiveInteger: (x: unknown) => x is number;
|
|
519
496
|
/** Check if value is a positive number */
|
|
520
497
|
declare const isPositiveNumber: (x: unknown) => x is number;
|
|
521
|
-
/** Check if value is a valid finite number */
|
|
522
|
-
declare const isNumber: (x: unknown) => x is number;
|
|
523
498
|
|
|
524
499
|
/**
|
|
525
500
|
* Check if value is an object.
|
|
@@ -598,6 +573,15 @@ declare const isRegExp: (x: unknown) => x is RegExp;
|
|
|
598
573
|
declare const isSet: <T = unknown>(x: unknown) => x is Set<T>;
|
|
599
574
|
/** Check if value is string */
|
|
600
575
|
declare const isStr: (x: unknown) => x is string;
|
|
576
|
+
/**
|
|
577
|
+
* Check if value is similar to a RxJS subject with .subscribe & .next functions
|
|
578
|
+
*
|
|
579
|
+
* @param x The value to check
|
|
580
|
+
* @param withValue When `true`, also checks if `value` property exists in `x`
|
|
581
|
+
*
|
|
582
|
+
* @returns `true` if the value is subject-like, `false` otherwise.
|
|
583
|
+
*/
|
|
584
|
+
declare const isSubjectLike: <T>(x: unknown, withValue?: boolean) => boolean;
|
|
601
585
|
/** Check if value is a Symbol */
|
|
602
586
|
declare const isSymbol: (x: unknown) => x is symbol;
|
|
603
587
|
/**
|
|
@@ -625,6 +609,8 @@ declare const is: {
|
|
|
625
609
|
integer: (x: unknown) => x is number;
|
|
626
610
|
map: <TKey = unknown, TValue = unknown>(x: unknown) => x is Map<TKey, TValue>;
|
|
627
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;
|
|
628
614
|
number: (x: unknown) => x is number;
|
|
629
615
|
obj: <T = Record<PropertyKey, unknown>>(x: unknown, strict?: boolean) => x is T;
|
|
630
616
|
positiveInteger: (x: unknown) => x is number;
|
|
@@ -633,6 +619,7 @@ declare const is: {
|
|
|
633
619
|
regExp: (x: unknown) => x is RegExp;
|
|
634
620
|
set: <T = unknown>(x: unknown) => x is Set<T>;
|
|
635
621
|
str: (x: unknown) => x is string;
|
|
622
|
+
subjectLike: <T>(x: unknown, withValue?: boolean) => boolean;
|
|
636
623
|
symbol: (x: unknown) => x is symbol;
|
|
637
624
|
uint8Arr: (arr: unknown) => arr is Uint8Array<ArrayBufferLike>;
|
|
638
625
|
url: (x: unknown) => x is URL;
|
|
@@ -643,48 +630,6 @@ declare const is: {
|
|
|
643
630
|
declare function noop(): void;
|
|
644
631
|
declare function noopAsync(): Promise<void>;
|
|
645
632
|
|
|
646
|
-
type ThrottleOptions<ThisArg = unknown> = {
|
|
647
|
-
onError?: (err: unknown) => ValueOrPromise<unknown>;
|
|
648
|
-
thisArg?: ThisArg;
|
|
649
|
-
trailing?: boolean;
|
|
650
|
-
tid?: TimeoutId;
|
|
651
|
-
};
|
|
652
|
-
/**
|
|
653
|
-
* @function throttle
|
|
654
|
-
* @summary returns a function that invokes the `callback` maximum twice (once if `executeLast = false`) per interval
|
|
655
|
-
*
|
|
656
|
-
* @param callback function to be invoked after timeout
|
|
657
|
-
* @param delay (optional) interval duration in milliseconds. Default: 50
|
|
658
|
-
* @param config.onError (optional)
|
|
659
|
-
* @param config.tid (optional)
|
|
660
|
-
* @param config.thisArg (optional) the special `thisArgs` to be used when invoking the callback.
|
|
661
|
-
* @param config.trailing (optional) whether to enable trailing edge execution. Default: `true`
|
|
662
|
-
*
|
|
663
|
-
* @example
|
|
664
|
-
* ```javascript
|
|
665
|
-
* import { throttle } from '@superutils/core'
|
|
666
|
-
*
|
|
667
|
-
* const handleChange = throttle(
|
|
668
|
-
* event => console.log('Value:', event.target.value),
|
|
669
|
-
* 300, // throttle duration in milliseconds
|
|
670
|
-
* )
|
|
671
|
-
* handleChange({ target: { value: 1 } }) // will be executed
|
|
672
|
-
* handleChange({ target: { value: 2 } }) // will be ignored
|
|
673
|
-
* handleChange({ target: { value: 3 } }) // will be ignored
|
|
674
|
-
* ```
|
|
675
|
-
*/
|
|
676
|
-
declare const throttled: {
|
|
677
|
-
<TArgs extends unknown[], ThisArg>(callback: (this: ThisArg, ...args: TArgs) => ValueOrPromise<unknown>, delay?: number, config?: ThrottleOptions<ThisArg>): (...args: TArgs) => void;
|
|
678
|
-
/**
|
|
679
|
-
* Set the default values
|
|
680
|
-
* This change is applicable application-wide and only applies to any new invocation of `throttle()`.
|
|
681
|
-
*/
|
|
682
|
-
defaults: {
|
|
683
|
-
onError: undefined;
|
|
684
|
-
trailing: false;
|
|
685
|
-
};
|
|
686
|
-
};
|
|
687
|
-
|
|
688
633
|
/**
|
|
689
634
|
* Convert timestamp to `input["datetime-local"]` compatible format.
|
|
690
635
|
*
|
|
@@ -841,8 +786,7 @@ declare const objSetPropUndefined: (...[obj, key, ...args]: Parameters<typeof ob
|
|
|
841
786
|
declare const objSort: <T>(obj: T, recursive?: boolean, _done?: Map<unknown, boolean>) => T;
|
|
842
787
|
|
|
843
788
|
/**
|
|
844
|
-
*
|
|
845
|
-
* @summary constructs a new object excluding specific properties
|
|
789
|
+
* Creates a new object excluding specific properties
|
|
846
790
|
*
|
|
847
791
|
* @param {Object} input
|
|
848
792
|
* @param {Array} keys property names to exclude
|
|
@@ -850,6 +794,27 @@ declare const objSort: <T>(obj: T, recursive?: boolean, _done?: Map<unknown, boo
|
|
|
850
794
|
* Default: a copy of the `input` object
|
|
851
795
|
*
|
|
852
796
|
* @returns {Object}
|
|
797
|
+
*
|
|
798
|
+
* @example Create a new object excluding specific properties
|
|
799
|
+
*
|
|
800
|
+
* ```javascript
|
|
801
|
+
* import { objWithoutKeys } from '@superutils/core'
|
|
802
|
+
*
|
|
803
|
+
* const result = objWithoutKeys({ a: 1, b: '2', c: false }, ['b', 'c'])
|
|
804
|
+
* console.log(result) // { a: 1 }
|
|
805
|
+
* ```
|
|
806
|
+
*
|
|
807
|
+
* @example Copy one object's properties to another while ignoring specific properties
|
|
808
|
+
*
|
|
809
|
+
* ```javascript
|
|
810
|
+
* import { objWithoutKeys } from '@superutils/core'
|
|
811
|
+
*
|
|
812
|
+
* const source = { a: 1, b: '2', c: false }
|
|
813
|
+
* const dest = { d: 4, e: 5 }
|
|
814
|
+
* const result = objWithoutKeys(source, ['b', 'c'], dest)
|
|
815
|
+
* console.log(result) // { d: 4, e: 5, a: 1 }
|
|
816
|
+
* console.log(result === dest) // true
|
|
817
|
+
* ```
|
|
853
818
|
*/
|
|
854
819
|
declare const objWithoutKeys: (input: unknown, keys: string[], output?: Record<PropertyKey, unknown>) => Record<PropertyKey, unknown>;
|
|
855
820
|
|
|
@@ -913,6 +878,7 @@ declare const arrReverse: <T = unknown>(arr: T[], reverse?: boolean, newArray?:
|
|
|
913
878
|
* arr,
|
|
914
879
|
* (_: Item, i: number) => item.a,
|
|
915
880
|
* )
|
|
881
|
+
* ```
|
|
916
882
|
*
|
|
917
883
|
* @example Flatten and convert Array to Map
|
|
918
884
|
* ```typescript
|
|
@@ -936,6 +902,173 @@ declare function arrToMap<T extends unknown[], FlatDepth extends number = 0, Map
|
|
|
936
902
|
*/
|
|
937
903
|
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>[];
|
|
938
904
|
|
|
905
|
+
/**
|
|
906
|
+
* Debounce function options
|
|
907
|
+
*/
|
|
908
|
+
type DebounceOptions<ThisArg = unknown> = {
|
|
909
|
+
leading?: boolean | 'global';
|
|
910
|
+
onError?: (this: ThisArg, err: unknown) => ValueOrPromise<unknown>;
|
|
911
|
+
thisArg?: ThisArg;
|
|
912
|
+
tid?: TimeoutId;
|
|
913
|
+
};
|
|
914
|
+
/**
|
|
915
|
+
* Deferred function options
|
|
916
|
+
*/
|
|
917
|
+
type DeferredOptions<ThisArg = unknown> = ({
|
|
918
|
+
throttle: true;
|
|
919
|
+
} & ThrottleOptions<ThisArg>) | ({
|
|
920
|
+
throttle?: false;
|
|
921
|
+
} & DebounceOptions<ThisArg>);
|
|
922
|
+
/**
|
|
923
|
+
* Throttle function options
|
|
924
|
+
*/
|
|
925
|
+
type ThrottleOptions<ThisArg = unknown> = {
|
|
926
|
+
/**
|
|
927
|
+
*
|
|
928
|
+
* @param err Error object thrown by the callback function.
|
|
929
|
+
* @returns
|
|
930
|
+
*/
|
|
931
|
+
onError?: (this: ThisArg, err: unknown) => ValueOrPromise<unknown>;
|
|
932
|
+
thisArg?: ThisArg;
|
|
933
|
+
trailing?: boolean;
|
|
934
|
+
tid?: TimeoutId;
|
|
935
|
+
};
|
|
936
|
+
|
|
937
|
+
/**
|
|
938
|
+
* Returns a function that invokes the callback function after certain delay/timeout.
|
|
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.
|
|
947
|
+
*
|
|
948
|
+
* @example Debounce function calls
|
|
949
|
+
* ```javascript
|
|
950
|
+
* import { deferred } from '@superutils/core'
|
|
951
|
+
*
|
|
952
|
+
* const handleChange = deferred(
|
|
953
|
+
* event => console.log('Value:', event.target.value),
|
|
954
|
+
* 300 // debounce delay in milliseconds
|
|
955
|
+
* )
|
|
956
|
+
*
|
|
957
|
+
* handleChange({ target: { value: 1 } }) // will be ignored
|
|
958
|
+
* handleChange({ target: { value: 2 } }) // will be ingored
|
|
959
|
+
* handleChange({ target: { value: 3 } }) // will be invoked
|
|
960
|
+
* ```
|
|
961
|
+
*/
|
|
962
|
+
declare const debounce: {
|
|
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
|
+
};
|
|
977
|
+
|
|
978
|
+
/**
|
|
979
|
+
* Returns a function that can be used to debounce/throttle calls to the provided callback function.
|
|
980
|
+
* All errors will be gracefully swallowed. See {@link debounce} and {@link throttle} for more information.
|
|
981
|
+
*
|
|
982
|
+
* @param callback function to be invoked after delay
|
|
983
|
+
* @param delay (optional) delay duration in milliseconds. Default: `50`
|
|
984
|
+
* @param config (optional) debounce or throttle configuration options
|
|
985
|
+
*
|
|
986
|
+
* @returns Callback function that can be invoked in one of the following 2 methods:
|
|
987
|
+
* - debounced: when `throttle` is `false` or `undefined`
|
|
988
|
+
* - throttled: when `throttle` is `true`
|
|
989
|
+
*/
|
|
990
|
+
declare const deferred: <TArgs extends unknown[], ThisArg, Delay = unknown>(callback: (this: ThisArg, ...args: TArgs) => ValueOrPromise<unknown>, delay?: Delay, config?: DeferredOptions<ThisArg>) => (...args: TArgs) => void;
|
|
991
|
+
|
|
992
|
+
/**
|
|
993
|
+
* Returns a throttled function that ensures the callback is invoked at most once in the specified delay interval.
|
|
994
|
+
* All errors will be gracefully swallowed.
|
|
995
|
+
*
|
|
996
|
+
* If the throttled function is called multiple times during the delay interval, only the first call will invoke the callback immediately.
|
|
997
|
+
*
|
|
998
|
+
* If `trailing` is enabled and if returned function is invoked more than once during the delay interval,
|
|
999
|
+
* the callback runs again at the end of the delay with the most recent arguments.
|
|
1000
|
+
*
|
|
1001
|
+
* @param callback function to be invoked after timeout
|
|
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`
|
|
1008
|
+
*
|
|
1009
|
+
* @example Throttle function calls
|
|
1010
|
+
* ```javascript
|
|
1011
|
+
* import { throttle } from '@superutils/core'
|
|
1012
|
+
*
|
|
1013
|
+
* const handleChange = throttle(
|
|
1014
|
+
* event => console.log('Value:', event.target.value),
|
|
1015
|
+
* 300, // throttle duration in milliseconds
|
|
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
|
|
1020
|
+
*
|
|
1021
|
+
* setTimeout(() => {
|
|
1022
|
+
* handleChange({ target: { value: 4 } }) // will be executed (after 300ms)
|
|
1023
|
+
* handleChange({ target: { value: 5 } }) // will be ignored
|
|
1024
|
+
* }, 400)
|
|
1025
|
+
* ```
|
|
1026
|
+
*
|
|
1027
|
+
* @example Throttle with trailing enabled
|
|
1028
|
+
* ```javascript
|
|
1029
|
+
* import { throttle } from '@superutils/core'
|
|
1030
|
+
*
|
|
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
|
+
*
|
|
1039
|
+
* setTimeout(() => {
|
|
1040
|
+
* handleChange({ target: { value: 4 } }) // will be executed
|
|
1041
|
+
* handleChange({ target: { value: 5 } }) // will be ignored
|
|
1042
|
+
* }, 400)
|
|
1043
|
+
* ```
|
|
1044
|
+
*/
|
|
1045
|
+
declare const throttle: {
|
|
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
|
+
};
|
|
1071
|
+
|
|
939
1072
|
/** Configuration for finding {@link IterableList} items */
|
|
940
1073
|
type FindOptions<K, V, IncludeKey extends boolean = false> = Omit<SearchOptions<K, V>, 'limit' | 'asMap'> & {
|
|
941
1074
|
/**
|
|
@@ -1053,7 +1186,7 @@ declare const filter: <K, V, AsArray extends boolean = false, Result = AsArray e
|
|
|
1053
1186
|
*
|
|
1054
1187
|
* @returns first item matched or `undefined` if not found
|
|
1055
1188
|
*
|
|
1056
|
-
* @example Find item using callback
|
|
1189
|
+
* @example Find item using predicate callback
|
|
1057
1190
|
* ```typescript
|
|
1058
1191
|
* import { find } from '@superutils/core'
|
|
1059
1192
|
*
|
|
@@ -1182,7 +1315,7 @@ type SliceMapOptions<Data, Value, Key, AsMap extends boolean = false> = {
|
|
|
1182
1315
|
start?: number;
|
|
1183
1316
|
};
|
|
1184
1317
|
/**
|
|
1185
|
-
* Slice an iterable list and map the values into an Array/Map
|
|
1318
|
+
* Slice an iterable list and map the values into an Array/Map.
|
|
1186
1319
|
*
|
|
1187
1320
|
* @param data Array, Map, Set...
|
|
1188
1321
|
* @param options One of the following is required to create a new list:
|
|
@@ -1202,6 +1335,27 @@ type SliceMapOptions<Data, Value, Key, AsMap extends boolean = false> = {
|
|
|
1202
1335
|
* - data: original list
|
|
1203
1336
|
*
|
|
1204
1337
|
* @returns Array/Map
|
|
1338
|
+
*
|
|
1339
|
+
* @example
|
|
1340
|
+
* ```javascript
|
|
1341
|
+
* const data = new Map([
|
|
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
|
+
* ```
|
|
1205
1359
|
*/
|
|
1206
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;
|
|
1207
1361
|
|
|
@@ -1317,12 +1471,30 @@ declare const mapJoin: <K, V>(...inputs: (Map<K, V> | [K, V][])[]) => Map<K, V>;
|
|
|
1317
1471
|
*/
|
|
1318
1472
|
declare const randomInt: (min?: number, max?: number) => number;
|
|
1319
1473
|
|
|
1320
|
-
/**
|
|
1474
|
+
/**
|
|
1475
|
+
* Describes a valid finite number type.
|
|
1476
|
+
*/
|
|
1477
|
+
type FiniteNumber<N> = N extends number ? number extends N ? never : N : never;
|
|
1478
|
+
/**
|
|
1479
|
+
* Describes an integer type.
|
|
1480
|
+
*/
|
|
1481
|
+
type Integer<N> = N extends number ? number extends N ? never : `${N}` extends `${string}.${string}` | `.${string}` | `${string}e-${string}` ? never : N : never;
|
|
1482
|
+
/**
|
|
1483
|
+
* Describes a number type with negative integer values.
|
|
1484
|
+
*/
|
|
1485
|
+
type NegativeInteger<N> = N extends number ? `${N}` extends `-${string}` ? `${N}` extends `-${string}.${string}` | `-${string}e-${string}` ? never : N : never : never;
|
|
1486
|
+
/**
|
|
1487
|
+
* Describes a number type with negative values.
|
|
1488
|
+
*/
|
|
1321
1489
|
type NegativeNumber<N> = N extends number ? `${N}` extends `-${string}` ? N : never : never;
|
|
1322
|
-
/**
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
type
|
|
1490
|
+
/**
|
|
1491
|
+
* Describes a number type with positive integer values.
|
|
1492
|
+
*/
|
|
1493
|
+
type PositiveInteger<N> = N extends number ? number extends N ? never : `${N}` extends `-${string}` | '0' | `${string}.${string}` | `${string}e-${string}` ? never : N : never;
|
|
1494
|
+
/**
|
|
1495
|
+
* Describes a number type with positive values.
|
|
1496
|
+
*/
|
|
1497
|
+
type PositiveNumber<N> = N extends number ? number extends N ? never : `${N}` extends `-${string}` | '0' ? never : N : never;
|
|
1326
1498
|
|
|
1327
1499
|
/**
|
|
1328
1500
|
* Clears clutter from strings
|
|
@@ -1386,4 +1558,4 @@ declare const HASH_REGEX: RegExp;
|
|
|
1386
1558
|
*/
|
|
1387
1559
|
declare const strToArr: (value: unknown, seperator?: string) => string[];
|
|
1388
1560
|
|
|
1389
|
-
export { type ArrayComparator, type AsyncFn, type CreateTuple, type CurriedArgs, type Curry, type DeferredOptions, type DropFirst, type DropFirstN, type DropLast, EMAIL_REGEX, type EntryComparator, type FindOptions, HASH_REGEX, HEX_REGEX, type IfPromiseAddValue, type IsFiniteTuple, type IsOptional, type IterableList, type KeepFirst, type KeepFirstN, type KeepOptionals, type KeepRequired, type MakeOptional, type MinLength, type NegativeNumber, type OptionalIf, type
|
|
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 OptionalIf, type PositiveInteger, type PositiveNumber, type ReadOnlyAllowAddFn, ReadOnlyArrayHelper, type ReadOnlyConfig, type SearchOptions, 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 };
|
package/dist/index.js
CHANGED
|
@@ -48,9 +48,11 @@ var isDate_default = isDate;
|
|
|
48
48
|
|
|
49
49
|
// src/is/isNumber.ts
|
|
50
50
|
var isInteger = (x) => Number.isInteger(x);
|
|
51
|
+
var isNegativeInteger = (x) => Number.isInteger(x) && x < 0;
|
|
52
|
+
var isNegativeNumber = (x) => isNumber(x) && x < 0;
|
|
53
|
+
var isNumber = (x) => typeof x === "number" && !Number.isNaN(x) && Number.isFinite(x);
|
|
51
54
|
var isPositiveInteger = (x) => isInteger(x) && x > 0;
|
|
52
55
|
var isPositiveNumber = (x) => isNumber(x) && x > 0;
|
|
53
|
-
var isNumber = (x) => typeof x == "number" && !Number.isNaN(x) && Number.isFinite(x);
|
|
54
56
|
|
|
55
57
|
// src/is/isEmpty.ts
|
|
56
58
|
var isEmpty = (x, nonNumerable = false, fallback = false) => {
|
|
@@ -149,6 +151,7 @@ var isPromise = (x) => x instanceof Promise;
|
|
|
149
151
|
var isRegExp = (x) => x instanceof RegExp;
|
|
150
152
|
var isSet = (x) => x instanceof Set;
|
|
151
153
|
var isStr = (x) => typeof x === "string";
|
|
154
|
+
var isSubjectLike = (x, withValue = false) => isObj_default(x, false) && isFn_default(x.subscribe) && isFn_default(x.next) && (!withValue || "value" in x);
|
|
152
155
|
var isSymbol = (x) => typeof x === "symbol";
|
|
153
156
|
var is = {
|
|
154
157
|
arr: isArr_default,
|
|
@@ -172,6 +175,8 @@ var is = {
|
|
|
172
175
|
integer: isInteger,
|
|
173
176
|
map: isMap,
|
|
174
177
|
mapObj: isMapObj,
|
|
178
|
+
negativeInteger: isNegativeInteger,
|
|
179
|
+
negativeNumber: isNegativeNumber,
|
|
175
180
|
number: isNumber,
|
|
176
181
|
obj: isObj_default,
|
|
177
182
|
positiveInteger: isPositiveInteger,
|
|
@@ -180,7 +185,7 @@ var is = {
|
|
|
180
185
|
regExp: isRegExp,
|
|
181
186
|
set: isSet,
|
|
182
187
|
str: isStr,
|
|
183
|
-
|
|
188
|
+
subjectLike: isSubjectLike,
|
|
184
189
|
symbol: isSymbol,
|
|
185
190
|
uint8Arr: isUint8Arr,
|
|
186
191
|
url: isUrl_default,
|
|
@@ -188,86 +193,18 @@ var is = {
|
|
|
188
193
|
};
|
|
189
194
|
|
|
190
195
|
// src/fallbackIfFails.ts
|
|
191
|
-
var fallbackIfFails = (target, args,
|
|
192
|
-
let result;
|
|
196
|
+
var fallbackIfFails = (target, args, fallback) => {
|
|
193
197
|
try {
|
|
194
|
-
result = !isFn(target) ? target : target(...isFn(args) ? args() : args);
|
|
198
|
+
const result = !isFn(target) ? target : target(...isFn(args) ? args() : args);
|
|
195
199
|
if (!isPromise(result)) return result;
|
|
196
|
-
return result.catch(
|
|
197
|
-
|
|
198
|
-
|
|
200
|
+
return result.catch(
|
|
201
|
+
(err) => isFn(fallback) ? fallback(err) : fallback
|
|
202
|
+
);
|
|
203
|
+
} catch (err) {
|
|
204
|
+
return isFn(fallback) ? fallback(err) : fallback;
|
|
199
205
|
}
|
|
200
206
|
};
|
|
201
207
|
var fallbackIfFails_default = fallbackIfFails;
|
|
202
|
-
var getAltValCb = (error, fallbackValue) => isFn(fallbackValue) ? fallbackValue(error) : fallbackValue;
|
|
203
|
-
|
|
204
|
-
// src/deferred.ts
|
|
205
|
-
var deferred = (callback, delay = 50, config = {}) => {
|
|
206
|
-
const {
|
|
207
|
-
leading = deferred.defaults.leading,
|
|
208
|
-
onError = deferred.defaults.onError,
|
|
209
|
-
thisArg
|
|
210
|
-
} = config;
|
|
211
|
-
let { tid } = config;
|
|
212
|
-
if (thisArg !== void 0) callback = callback.bind(thisArg);
|
|
213
|
-
const _callback = (...args) => fallbackIfFails_default(callback, args, onError);
|
|
214
|
-
let firstArgs = null;
|
|
215
|
-
const leadingGlobal = leading === "global";
|
|
216
|
-
return (...args) => {
|
|
217
|
-
clearTimeout(tid);
|
|
218
|
-
tid = setTimeout(() => {
|
|
219
|
-
firstArgs !== args && _callback(...args);
|
|
220
|
-
firstArgs = leadingGlobal ? true : null;
|
|
221
|
-
}, delay);
|
|
222
|
-
if (!leading || firstArgs) return;
|
|
223
|
-
firstArgs = args;
|
|
224
|
-
_callback(...args);
|
|
225
|
-
};
|
|
226
|
-
};
|
|
227
|
-
deferred.defaults = {
|
|
228
|
-
/**
|
|
229
|
-
* Set the default value of argument `leading` for the `deferred` function.
|
|
230
|
-
* This change is applicable application-wide and only applies to any new invocation of `deferred()`.
|
|
231
|
-
*/
|
|
232
|
-
leading: false,
|
|
233
|
-
/**
|
|
234
|
-
* Set the default value of argument `onError` for the `deferred` function.
|
|
235
|
-
* This change is applicable application-wide and only applies to any new invocation of `deferred()`.
|
|
236
|
-
*/
|
|
237
|
-
onError: void 0
|
|
238
|
-
};
|
|
239
|
-
var deferred_default = deferred;
|
|
240
|
-
|
|
241
|
-
// src/debounce.ts
|
|
242
|
-
var debounce = deferred_default;
|
|
243
|
-
|
|
244
|
-
// src/throttled.ts
|
|
245
|
-
var throttled = (callback, delay = 50, config = {}) => {
|
|
246
|
-
const { defaults: d } = throttled;
|
|
247
|
-
const { onError = d.onError, trailing = d.trailing, thisArg } = config;
|
|
248
|
-
let { tid } = config;
|
|
249
|
-
if (thisArg !== void 0) callback = callback.bind(thisArg);
|
|
250
|
-
const _callback = (...args) => fallbackIfFails_default(callback, args, onError);
|
|
251
|
-
let trailArgs = null;
|
|
252
|
-
return (...args) => {
|
|
253
|
-
if (tid) {
|
|
254
|
-
trailArgs = args;
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
tid = setTimeout(() => {
|
|
258
|
-
tid = void 0;
|
|
259
|
-
if (!trailing) return;
|
|
260
|
-
const cbArgs = trailArgs;
|
|
261
|
-
trailArgs = null;
|
|
262
|
-
cbArgs && cbArgs !== args && _callback(...cbArgs);
|
|
263
|
-
}, delay);
|
|
264
|
-
_callback(...args);
|
|
265
|
-
};
|
|
266
|
-
};
|
|
267
|
-
throttled.defaults = {
|
|
268
|
-
onError: void 0,
|
|
269
|
-
trailing: false
|
|
270
|
-
};
|
|
271
208
|
|
|
272
209
|
// src/toDatetimeLocal.ts
|
|
273
210
|
var toDatetimeLocal = (dateStr) => {
|
|
@@ -533,6 +470,79 @@ function arrToMap(arr, key, flatDepth = 0) {
|
|
|
533
470
|
// src/arr/arrUnique.ts
|
|
534
471
|
var arrUnique = (arr, flatDepth = 0) => !isArr(arr) ? [] : Array.from(new Set(arr.flat(flatDepth)));
|
|
535
472
|
|
|
473
|
+
// src/deferred/debounce.ts
|
|
474
|
+
var debounce = (callback, delay = 50, config = {}) => {
|
|
475
|
+
const {
|
|
476
|
+
leading = debounce.defaults.leading,
|
|
477
|
+
onError = debounce.defaults.onError,
|
|
478
|
+
thisArg
|
|
479
|
+
} = config;
|
|
480
|
+
let { tid } = config;
|
|
481
|
+
if (thisArg !== void 0) callback = callback.bind(thisArg);
|
|
482
|
+
const _callback = (...args) => fallbackIfFails_default(callback, args, onError);
|
|
483
|
+
let firstArgs = null;
|
|
484
|
+
const leadingGlobal = leading === "global";
|
|
485
|
+
return (...args) => {
|
|
486
|
+
clearTimeout(tid);
|
|
487
|
+
tid = setTimeout(() => {
|
|
488
|
+
firstArgs !== args && _callback(...args);
|
|
489
|
+
firstArgs = leadingGlobal ? true : null;
|
|
490
|
+
}, delay);
|
|
491
|
+
if (!leading || firstArgs) return;
|
|
492
|
+
firstArgs = args;
|
|
493
|
+
_callback(...args);
|
|
494
|
+
};
|
|
495
|
+
};
|
|
496
|
+
debounce.defaults = {
|
|
497
|
+
/**
|
|
498
|
+
* Set the default value of argument `leading` for the `deferred` function.
|
|
499
|
+
* This change is applicable application-wide and only applies to any new invocation of `deferred()`.
|
|
500
|
+
*/
|
|
501
|
+
leading: false,
|
|
502
|
+
/**
|
|
503
|
+
* Set the default value of argument `onError` for the `deferred` function.
|
|
504
|
+
* This change is applicable application-wide and only applies to any new invocation of `deferred()`.
|
|
505
|
+
*/
|
|
506
|
+
onError: void 0
|
|
507
|
+
};
|
|
508
|
+
var debounce_default = debounce;
|
|
509
|
+
|
|
510
|
+
// src/deferred/throttle.ts
|
|
511
|
+
var throttle = (callback, delay = 50, config = {}) => {
|
|
512
|
+
const { defaults: d } = throttle;
|
|
513
|
+
const { onError = d.onError, trailing = d.trailing, thisArg } = config;
|
|
514
|
+
let { tid } = config;
|
|
515
|
+
const handleCallback = (...args) => fallbackIfFails_default(
|
|
516
|
+
thisArg !== void 0 ? callback.bind(thisArg) : callback,
|
|
517
|
+
args,
|
|
518
|
+
!isFn(onError) ? void 0 : (err) => fallbackIfFails_default(onError, [err], void 0)
|
|
519
|
+
);
|
|
520
|
+
let trailArgs = null;
|
|
521
|
+
return (...args) => {
|
|
522
|
+
if (tid) {
|
|
523
|
+
trailArgs = args;
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
tid = setTimeout(() => {
|
|
527
|
+
tid = void 0;
|
|
528
|
+
if (!trailing) return;
|
|
529
|
+
const cbArgs = trailArgs;
|
|
530
|
+
trailArgs = null;
|
|
531
|
+
cbArgs && cbArgs !== args && handleCallback(...cbArgs);
|
|
532
|
+
}, delay);
|
|
533
|
+
handleCallback(...args);
|
|
534
|
+
};
|
|
535
|
+
};
|
|
536
|
+
throttle.defaults = {
|
|
537
|
+
onError: void 0,
|
|
538
|
+
trailing: false
|
|
539
|
+
};
|
|
540
|
+
var throttled = throttle;
|
|
541
|
+
var throttle_default = throttle;
|
|
542
|
+
|
|
543
|
+
// src/deferred/deferred.ts
|
|
544
|
+
var deferred = (callback, delay = 50, config = {}) => config.throttle ? throttle_default(callback, delay, config) : debounce_default(callback, delay, config);
|
|
545
|
+
|
|
536
546
|
// src/iterable/filter.ts
|
|
537
547
|
var filter = (data, predicate, limit, asArray, result) => {
|
|
538
548
|
var _a;
|
|
@@ -673,7 +683,7 @@ function matchObjOrProp({
|
|
|
673
683
|
""
|
|
674
684
|
);
|
|
675
685
|
if (value === keyword) return 0;
|
|
676
|
-
if (matchExact) return -1;
|
|
686
|
+
if (matchExact && !isRegExp(keyword)) return -1;
|
|
677
687
|
let valueStr = String(value);
|
|
678
688
|
if (!valueStr.trim()) return -1;
|
|
679
689
|
if (isRegExp(keyword)) return (_b = (_a = valueStr.match(keyword)) == null ? void 0 : _a.index) != null ? _b : -1;
|
|
@@ -947,6 +957,8 @@ export {
|
|
|
947
957
|
isInteger,
|
|
948
958
|
isMap,
|
|
949
959
|
isMapObj,
|
|
960
|
+
isNegativeInteger,
|
|
961
|
+
isNegativeNumber,
|
|
950
962
|
isNumber,
|
|
951
963
|
isObj,
|
|
952
964
|
isPositiveInteger,
|
|
@@ -955,6 +967,7 @@ export {
|
|
|
955
967
|
isRegExp,
|
|
956
968
|
isSet,
|
|
957
969
|
isStr,
|
|
970
|
+
isSubjectLike,
|
|
958
971
|
isSymbol,
|
|
959
972
|
isUint8Arr,
|
|
960
973
|
isUrl,
|
|
@@ -979,6 +992,7 @@ export {
|
|
|
979
992
|
sliceMap,
|
|
980
993
|
sort,
|
|
981
994
|
strToArr,
|
|
995
|
+
throttle,
|
|
982
996
|
throttled,
|
|
983
997
|
toDatetimeLocal
|
|
984
998
|
};
|
package/package.json
CHANGED