@superutils/core 1.1.5 → 1.2.0
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 +36 -10
- package/dist/index.d.ts +184 -112
- package/dist/index.js +90 -82
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,8 +13,9 @@ 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
|
|
@@ -76,27 +77,29 @@ import {
|
|
|
76
77
|
} from '@superutils/core'
|
|
77
78
|
```
|
|
78
79
|
|
|
79
|
-
<div id="
|
|
80
|
-
|
|
81
|
-
### `deferred(fn)`: debounce callbacks
|
|
80
|
+
<div id="debouce"></div>
|
|
82
81
|
|
|
83
|
-
`
|
|
82
|
+
### `debouce(fn, delay, options)`: debounce callbacks
|
|
84
83
|
|
|
85
84
|
```javascript
|
|
86
|
-
import {
|
|
85
|
+
import { debouce } from '@superutils/core'
|
|
87
86
|
|
|
88
|
-
const handleChange =
|
|
87
|
+
const handleChange = debouce(
|
|
89
88
|
event => console.log(event.target.value),
|
|
90
89
|
300, // debounce delay in milliseconds
|
|
90
|
+
{
|
|
91
|
+
leading: false, // default
|
|
92
|
+
},
|
|
91
93
|
)
|
|
92
|
-
handleChange({ target: { value: 1 } }) // will be ignored
|
|
94
|
+
handleChange({ target: { value: 1 } }) // will be ignored, unless `leading = true`
|
|
93
95
|
handleChange({ target: { value: 2 } }) // will be ignored
|
|
94
|
-
handleChange({ target: { value: 3 } }) // will be
|
|
96
|
+
handleChange({ target: { value: 3 } }) // will be ignored
|
|
97
|
+
handleChange({ target: { value: 4 } }) // will be executed
|
|
95
98
|
```
|
|
96
99
|
|
|
97
100
|
<div id="throttle"></div>
|
|
98
101
|
|
|
99
|
-
### `throttle(fn)`: throttle callbacks
|
|
102
|
+
### `throttle(fn, delay, options)`: throttle callbacks
|
|
100
103
|
|
|
101
104
|
```javascript
|
|
102
105
|
import { throttle } from '@superutils/core'
|
|
@@ -104,10 +107,33 @@ import { throttle } from '@superutils/core'
|
|
|
104
107
|
const handleChange = throttle(
|
|
105
108
|
event => console.log(event.target.value),
|
|
106
109
|
300, // throttle duration in milliseconds
|
|
110
|
+
{
|
|
111
|
+
trailing: false, // default
|
|
112
|
+
},
|
|
107
113
|
)
|
|
108
114
|
handleChange({ target: { value: 1 } }) // will be executed
|
|
109
115
|
handleChange({ target: { value: 2 } }) // will be ignored
|
|
110
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
|
|
111
137
|
```
|
|
112
138
|
|
|
113
139
|
<div id="fallback-if-fails"></div>
|
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,61 +255,6 @@ 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.
|
|
@@ -408,7 +354,7 @@ type IfPromiseAddValue<T> = T extends Promise<infer V> ? T | V : T;
|
|
|
408
354
|
* console.log({ value }) // undefined
|
|
409
355
|
* ```
|
|
410
356
|
*/
|
|
411
|
-
declare const fallbackIfFails: <T, TArgs extends unknown[] = unknown[]>(target: T | ((...args: TArgs) => T), args: TArgs | (() => TArgs), fallback: IfPromiseAddValue<
|
|
357
|
+
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;
|
|
412
358
|
|
|
413
359
|
/** Check if value is an array */
|
|
414
360
|
declare const isArr: <Item = unknown>(x: unknown) => x is Item[];
|
|
@@ -684,48 +630,6 @@ declare const is: {
|
|
|
684
630
|
declare function noop(): void;
|
|
685
631
|
declare function noopAsync(): Promise<void>;
|
|
686
632
|
|
|
687
|
-
type ThrottleOptions<ThisArg = unknown> = {
|
|
688
|
-
onError?: (err: unknown) => ValueOrPromise<unknown>;
|
|
689
|
-
thisArg?: ThisArg;
|
|
690
|
-
trailing?: boolean;
|
|
691
|
-
tid?: TimeoutId;
|
|
692
|
-
};
|
|
693
|
-
/**
|
|
694
|
-
* @function throttle
|
|
695
|
-
* @summary returns a function that invokes the `callback` maximum twice (once if `executeLast = false`) per interval
|
|
696
|
-
*
|
|
697
|
-
* @param callback function to be invoked after timeout
|
|
698
|
-
* @param delay (optional) interval duration in milliseconds. Default: 50
|
|
699
|
-
* @param config.onError (optional)
|
|
700
|
-
* @param config.tid (optional)
|
|
701
|
-
* @param config.thisArg (optional) the special `thisArgs` to be used when invoking the callback.
|
|
702
|
-
* @param config.trailing (optional) whether to enable trailing edge execution. Default: `true`
|
|
703
|
-
*
|
|
704
|
-
* @example
|
|
705
|
-
* ```javascript
|
|
706
|
-
* import { throttle } from '@superutils/core'
|
|
707
|
-
*
|
|
708
|
-
* const handleChange = throttle(
|
|
709
|
-
* event => console.log('Value:', event.target.value),
|
|
710
|
-
* 300, // throttle duration in milliseconds
|
|
711
|
-
* )
|
|
712
|
-
* handleChange({ target: { value: 1 } }) // will be executed
|
|
713
|
-
* handleChange({ target: { value: 2 } }) // will be ignored
|
|
714
|
-
* handleChange({ target: { value: 3 } }) // will be ignored
|
|
715
|
-
* ```
|
|
716
|
-
*/
|
|
717
|
-
declare const throttled: {
|
|
718
|
-
<TArgs extends unknown[], ThisArg>(callback: (this: ThisArg, ...args: TArgs) => ValueOrPromise<unknown>, delay?: number, config?: ThrottleOptions<ThisArg>): (...args: TArgs) => void;
|
|
719
|
-
/**
|
|
720
|
-
* Set the default values
|
|
721
|
-
* This change is applicable application-wide and only applies to any new invocation of `throttle()`.
|
|
722
|
-
*/
|
|
723
|
-
defaults: {
|
|
724
|
-
onError: undefined;
|
|
725
|
-
trailing: false;
|
|
726
|
-
};
|
|
727
|
-
};
|
|
728
|
-
|
|
729
633
|
/**
|
|
730
634
|
* Convert timestamp to `input["datetime-local"]` compatible format.
|
|
731
635
|
*
|
|
@@ -974,6 +878,7 @@ declare const arrReverse: <T = unknown>(arr: T[], reverse?: boolean, newArray?:
|
|
|
974
878
|
* arr,
|
|
975
879
|
* (_: Item, i: number) => item.a,
|
|
976
880
|
* )
|
|
881
|
+
* ```
|
|
977
882
|
*
|
|
978
883
|
* @example Flatten and convert Array to Map
|
|
979
884
|
* ```typescript
|
|
@@ -997,6 +902,173 @@ declare function arrToMap<T extends unknown[], FlatDepth extends number = 0, Map
|
|
|
997
902
|
*/
|
|
998
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>[];
|
|
999
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
|
+
|
|
1000
1072
|
/** Configuration for finding {@link IterableList} items */
|
|
1001
1073
|
type FindOptions<K, V, IncludeKey extends boolean = false> = Omit<SearchOptions<K, V>, 'limit' | 'asMap'> & {
|
|
1002
1074
|
/**
|
|
@@ -1114,7 +1186,7 @@ declare const filter: <K, V, AsArray extends boolean = false, Result = AsArray e
|
|
|
1114
1186
|
*
|
|
1115
1187
|
* @returns first item matched or `undefined` if not found
|
|
1116
1188
|
*
|
|
1117
|
-
* @example Find item using callback
|
|
1189
|
+
* @example Find item using predicate callback
|
|
1118
1190
|
* ```typescript
|
|
1119
1191
|
* import { find } from '@superutils/core'
|
|
1120
1192
|
*
|
|
@@ -1219,7 +1291,7 @@ declare const reverse: <K, V, T extends IterableList<K, V>>(data: T, reverse?: b
|
|
|
1219
1291
|
*/
|
|
1220
1292
|
declare const search: {
|
|
1221
1293
|
<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;
|
|
1222
|
-
|
|
1294
|
+
defaults: Pick<Required<SearchOptions<unknown, unknown, false, true>>, "matchAll" | "limit" | "asMap" | "ignoreCase" | "ranked" | "transform">;
|
|
1223
1295
|
};
|
|
1224
1296
|
/**
|
|
1225
1297
|
* Utility for use with {@link search} function
|
|
@@ -1350,7 +1422,7 @@ declare function sort<V>(set: Set<V>, comparator: ArrayComparator<V>, options?:
|
|
|
1350
1422
|
/** Sort Array/Map/Set with `string | boolean | number` values */
|
|
1351
1423
|
declare function sort<K, V extends string | boolean | number, T extends IterableList<K, V>>(data: T, options?: SortOptions): T;
|
|
1352
1424
|
declare namespace sort {
|
|
1353
|
-
var
|
|
1425
|
+
var defaults: {
|
|
1354
1426
|
ignoreCase: true;
|
|
1355
1427
|
newInstance: false;
|
|
1356
1428
|
reverse: false;
|
|
@@ -1486,4 +1558,4 @@ declare const HASH_REGEX: RegExp;
|
|
|
1486
1558
|
*/
|
|
1487
1559
|
declare const strToArr: (value: unknown, seperator?: string) => string[];
|
|
1488
1560
|
|
|
1489
|
-
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, 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, throttled, toDatetimeLocal };
|
|
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
|
@@ -206,74 +206,6 @@ var fallbackIfFails = (target, args, fallback) => {
|
|
|
206
206
|
};
|
|
207
207
|
var fallbackIfFails_default = fallbackIfFails;
|
|
208
208
|
|
|
209
|
-
// src/deferred.ts
|
|
210
|
-
var deferred = (callback, delay = 50, config = {}) => {
|
|
211
|
-
const {
|
|
212
|
-
leading = deferred.defaults.leading,
|
|
213
|
-
onError = deferred.defaults.onError,
|
|
214
|
-
thisArg
|
|
215
|
-
} = config;
|
|
216
|
-
let { tid } = config;
|
|
217
|
-
if (thisArg !== void 0) callback = callback.bind(thisArg);
|
|
218
|
-
const _callback = (...args) => fallbackIfFails_default(callback, args, onError);
|
|
219
|
-
let firstArgs = null;
|
|
220
|
-
const leadingGlobal = leading === "global";
|
|
221
|
-
return (...args) => {
|
|
222
|
-
clearTimeout(tid);
|
|
223
|
-
tid = setTimeout(() => {
|
|
224
|
-
firstArgs !== args && _callback(...args);
|
|
225
|
-
firstArgs = leadingGlobal ? true : null;
|
|
226
|
-
}, delay);
|
|
227
|
-
if (!leading || firstArgs) return;
|
|
228
|
-
firstArgs = args;
|
|
229
|
-
_callback(...args);
|
|
230
|
-
};
|
|
231
|
-
};
|
|
232
|
-
deferred.defaults = {
|
|
233
|
-
/**
|
|
234
|
-
* Set the default value of argument `leading` for the `deferred` function.
|
|
235
|
-
* This change is applicable application-wide and only applies to any new invocation of `deferred()`.
|
|
236
|
-
*/
|
|
237
|
-
leading: false,
|
|
238
|
-
/**
|
|
239
|
-
* Set the default value of argument `onError` for the `deferred` function.
|
|
240
|
-
* This change is applicable application-wide and only applies to any new invocation of `deferred()`.
|
|
241
|
-
*/
|
|
242
|
-
onError: void 0
|
|
243
|
-
};
|
|
244
|
-
var deferred_default = deferred;
|
|
245
|
-
|
|
246
|
-
// src/debounce.ts
|
|
247
|
-
var debounce = deferred_default;
|
|
248
|
-
|
|
249
|
-
// src/throttled.ts
|
|
250
|
-
var throttled = (callback, delay = 50, config = {}) => {
|
|
251
|
-
const { defaults: d } = throttled;
|
|
252
|
-
const { onError = d.onError, trailing = d.trailing, thisArg } = config;
|
|
253
|
-
let { tid } = config;
|
|
254
|
-
if (thisArg !== void 0) callback = callback.bind(thisArg);
|
|
255
|
-
const _callback = (...args) => fallbackIfFails_default(callback, args, onError);
|
|
256
|
-
let trailArgs = null;
|
|
257
|
-
return (...args) => {
|
|
258
|
-
if (tid) {
|
|
259
|
-
trailArgs = args;
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
tid = setTimeout(() => {
|
|
263
|
-
tid = void 0;
|
|
264
|
-
if (!trailing) return;
|
|
265
|
-
const cbArgs = trailArgs;
|
|
266
|
-
trailArgs = null;
|
|
267
|
-
cbArgs && cbArgs !== args && _callback(...cbArgs);
|
|
268
|
-
}, delay);
|
|
269
|
-
_callback(...args);
|
|
270
|
-
};
|
|
271
|
-
};
|
|
272
|
-
throttled.defaults = {
|
|
273
|
-
onError: void 0,
|
|
274
|
-
trailing: false
|
|
275
|
-
};
|
|
276
|
-
|
|
277
209
|
// src/toDatetimeLocal.ts
|
|
278
210
|
var toDatetimeLocal = (dateStr) => {
|
|
279
211
|
const date = new Date(dateStr);
|
|
@@ -320,13 +252,16 @@ var objClean = (obj, keys, ignoreIfNotExist = true) => {
|
|
|
320
252
|
};
|
|
321
253
|
|
|
322
254
|
// src/obj/objKeys.ts
|
|
323
|
-
var objKeys = (obj, sorted = true, includeSymbols = true) =>
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
255
|
+
var objKeys = (obj, sorted = true, includeSymbols = true) => (
|
|
256
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
257
|
+
fallbackIfFails_default(
|
|
258
|
+
() => [
|
|
259
|
+
...includeSymbols && Object.getOwnPropertySymbols(obj) || [],
|
|
260
|
+
...sorted ? Object.keys(obj).sort() : Object.keys(obj)
|
|
261
|
+
],
|
|
262
|
+
[],
|
|
263
|
+
[]
|
|
264
|
+
)
|
|
330
265
|
);
|
|
331
266
|
var objKeys_default = objKeys;
|
|
332
267
|
|
|
@@ -538,6 +473,79 @@ function arrToMap(arr, key, flatDepth = 0) {
|
|
|
538
473
|
// src/arr/arrUnique.ts
|
|
539
474
|
var arrUnique = (arr, flatDepth = 0) => !isArr(arr) ? [] : Array.from(new Set(arr.flat(flatDepth)));
|
|
540
475
|
|
|
476
|
+
// src/deferred/debounce.ts
|
|
477
|
+
var debounce = (callback, delay = 50, config = {}) => {
|
|
478
|
+
const {
|
|
479
|
+
leading = debounce.defaults.leading,
|
|
480
|
+
onError = debounce.defaults.onError,
|
|
481
|
+
thisArg
|
|
482
|
+
} = config;
|
|
483
|
+
let { tid } = config;
|
|
484
|
+
if (thisArg !== void 0) callback = callback.bind(thisArg);
|
|
485
|
+
const _callback = (...args) => fallbackIfFails_default(callback, args, onError);
|
|
486
|
+
let firstArgs = null;
|
|
487
|
+
const leadingGlobal = leading === "global";
|
|
488
|
+
return (...args) => {
|
|
489
|
+
clearTimeout(tid);
|
|
490
|
+
tid = setTimeout(() => {
|
|
491
|
+
firstArgs !== args && _callback(...args);
|
|
492
|
+
firstArgs = leadingGlobal ? true : null;
|
|
493
|
+
}, delay);
|
|
494
|
+
if (!leading || firstArgs) return;
|
|
495
|
+
firstArgs = args;
|
|
496
|
+
_callback(...args);
|
|
497
|
+
};
|
|
498
|
+
};
|
|
499
|
+
debounce.defaults = {
|
|
500
|
+
/**
|
|
501
|
+
* Set the default value of argument `leading` for the `deferred` function.
|
|
502
|
+
* This change is applicable application-wide and only applies to any new invocation of `deferred()`.
|
|
503
|
+
*/
|
|
504
|
+
leading: false,
|
|
505
|
+
/**
|
|
506
|
+
* Set the default value of argument `onError` for the `deferred` function.
|
|
507
|
+
* This change is applicable application-wide and only applies to any new invocation of `deferred()`.
|
|
508
|
+
*/
|
|
509
|
+
onError: void 0
|
|
510
|
+
};
|
|
511
|
+
var debounce_default = debounce;
|
|
512
|
+
|
|
513
|
+
// src/deferred/throttle.ts
|
|
514
|
+
var throttle = (callback, delay = 50, config = {}) => {
|
|
515
|
+
const { defaults: d } = throttle;
|
|
516
|
+
const { onError = d.onError, trailing = d.trailing, thisArg } = config;
|
|
517
|
+
let { tid } = config;
|
|
518
|
+
const handleCallback = (...args) => fallbackIfFails_default(
|
|
519
|
+
thisArg !== void 0 ? callback.bind(thisArg) : callback,
|
|
520
|
+
args,
|
|
521
|
+
!isFn(onError) ? void 0 : (err) => fallbackIfFails_default(onError, [err], void 0)
|
|
522
|
+
);
|
|
523
|
+
let trailArgs = null;
|
|
524
|
+
return (...args) => {
|
|
525
|
+
if (tid) {
|
|
526
|
+
trailArgs = args;
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
tid = setTimeout(() => {
|
|
530
|
+
tid = void 0;
|
|
531
|
+
if (!trailing) return;
|
|
532
|
+
const cbArgs = trailArgs;
|
|
533
|
+
trailArgs = null;
|
|
534
|
+
cbArgs && cbArgs !== args && handleCallback(...cbArgs);
|
|
535
|
+
}, delay);
|
|
536
|
+
handleCallback(...args);
|
|
537
|
+
};
|
|
538
|
+
};
|
|
539
|
+
throttle.defaults = {
|
|
540
|
+
onError: void 0,
|
|
541
|
+
trailing: false
|
|
542
|
+
};
|
|
543
|
+
var throttled = throttle;
|
|
544
|
+
var throttle_default = throttle;
|
|
545
|
+
|
|
546
|
+
// src/deferred/deferred.ts
|
|
547
|
+
var deferred = (callback, delay = 50, config = {}) => config.throttle ? throttle_default(callback, delay, config) : debounce_default(callback, delay, config);
|
|
548
|
+
|
|
541
549
|
// src/iterable/filter.ts
|
|
542
550
|
var filter = (data, predicate, limit, asArray, result) => {
|
|
543
551
|
var _a;
|
|
@@ -577,10 +585,10 @@ var search = (data, options) => {
|
|
|
577
585
|
var _a;
|
|
578
586
|
const ignore = !getSize_default(data) || isEmpty(options == null ? void 0 : options.query);
|
|
579
587
|
const result = isMap(options == null ? void 0 : options.result) ? options.result : /* @__PURE__ */ new Map();
|
|
580
|
-
const asMap = (_a = options == null ? void 0 : options.asMap) != null ? _a : search.
|
|
588
|
+
const asMap = (_a = options == null ? void 0 : options.asMap) != null ? _a : search.defaults.asMap;
|
|
581
589
|
if (ignore) return asMap ? result : getValues_default(result);
|
|
582
590
|
options = objCopy(
|
|
583
|
-
search.
|
|
591
|
+
search.defaults,
|
|
584
592
|
options,
|
|
585
593
|
[],
|
|
586
594
|
"empty"
|
|
@@ -648,7 +656,7 @@ var search = (data, options) => {
|
|
|
648
656
|
}
|
|
649
657
|
return asMap ? result : getValues_default(result);
|
|
650
658
|
};
|
|
651
|
-
search.
|
|
659
|
+
search.defaults = {
|
|
652
660
|
asMap: true,
|
|
653
661
|
ignoreCase: true,
|
|
654
662
|
limit: Infinity,
|
|
@@ -682,7 +690,6 @@ function matchObjOrProp({
|
|
|
682
690
|
let valueStr = String(value);
|
|
683
691
|
if (!valueStr.trim()) return -1;
|
|
684
692
|
if (isRegExp(keyword)) return (_b = (_a = valueStr.match(keyword)) == null ? void 0 : _a.index) != null ? _b : -1;
|
|
685
|
-
if (matchExact) return -1;
|
|
686
693
|
if (ignoreCase) valueStr = valueStr.toLowerCase();
|
|
687
694
|
return valueStr.indexOf(String(keyword));
|
|
688
695
|
}
|
|
@@ -763,7 +770,7 @@ function sort(data, keyOrFn, options) {
|
|
|
763
770
|
const dataType = isArr(data) ? 1 : isMap(data) ? 2 : isSet(data) ? 3 : 0;
|
|
764
771
|
if (!dataType) return data;
|
|
765
772
|
const { ignoreCase, newInstance, reverse: reverse2, undefinedFirst } = {
|
|
766
|
-
...sort.
|
|
773
|
+
...sort.defaults,
|
|
767
774
|
...isObj(options) ? options : isObj(keyOrFn) ? keyOrFn : {}
|
|
768
775
|
};
|
|
769
776
|
if (dataType === 1 && isFn(keyOrFn)) {
|
|
@@ -807,7 +814,7 @@ function sort(data, keyOrFn, options) {
|
|
|
807
814
|
dataType === 2 ? data.set(...entry) : data.add(entry);
|
|
808
815
|
return data;
|
|
809
816
|
}
|
|
810
|
-
sort.
|
|
817
|
+
sort.defaults = {
|
|
811
818
|
ignoreCase: true,
|
|
812
819
|
newInstance: false,
|
|
813
820
|
reverse: false,
|
|
@@ -887,7 +894,7 @@ var getUrlParamRegex = (name, url, asArray = []) => {
|
|
|
887
894
|
if (isUrl(url)) url = url.toString();
|
|
888
895
|
const params = {};
|
|
889
896
|
const regex = /[?&]+([^=&]+)=([^&]*)/gi;
|
|
890
|
-
url.replace(regex, (_, name2, value) => {
|
|
897
|
+
url == null ? void 0 : url.replace(regex, (_, name2, value) => {
|
|
891
898
|
value = decodeURIComponent(value);
|
|
892
899
|
if (asArray.includes(name2) || params[name2] !== void 0) {
|
|
893
900
|
params[name2] = isArr(params[name2]) ? params[name2] : [params[name2]];
|
|
@@ -988,6 +995,7 @@ export {
|
|
|
988
995
|
sliceMap,
|
|
989
996
|
sort,
|
|
990
997
|
strToArr,
|
|
998
|
+
throttle,
|
|
991
999
|
throttled,
|
|
992
1000
|
toDatetimeLocal
|
|
993
1001
|
};
|
package/package.json
CHANGED