@superutils/core 1.1.5 → 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 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
- - [`deferred()`](#deferred): Debounce callbacks
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="deferred"></div>
80
-
81
- ### `deferred(fn)`: debounce callbacks
80
+ <div id="debouce"></div>
82
81
 
83
- `debounce()`, a sugar for `deferred()`, is also available.
82
+ ### `debouce(fn, delay, options)`: debounce callbacks
84
83
 
85
84
  ```javascript
86
- import { deferred } from '@superutils/core'
85
+ import { debouce } from '@superutils/core'
87
86
 
88
- const handleChange = deferred(
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 executed
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
- /** Accept value a promise resolving to value */
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.
@@ -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
  *
@@ -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);
@@ -538,6 +470,79 @@ function arrToMap(arr, key, flatDepth = 0) {
538
470
  // src/arr/arrUnique.ts
539
471
  var arrUnique = (arr, flatDepth = 0) => !isArr(arr) ? [] : Array.from(new Set(arr.flat(flatDepth)));
540
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
+
541
546
  // src/iterable/filter.ts
542
547
  var filter = (data, predicate, limit, asArray, result) => {
543
548
  var _a;
@@ -682,7 +687,6 @@ function matchObjOrProp({
682
687
  let valueStr = String(value);
683
688
  if (!valueStr.trim()) return -1;
684
689
  if (isRegExp(keyword)) return (_b = (_a = valueStr.match(keyword)) == null ? void 0 : _a.index) != null ? _b : -1;
685
- if (matchExact) return -1;
686
690
  if (ignoreCase) valueStr = valueStr.toLowerCase();
687
691
  return valueStr.indexOf(String(keyword));
688
692
  }
@@ -988,6 +992,7 @@ export {
988
992
  sliceMap,
989
993
  sort,
990
994
  strToArr,
995
+ throttle,
991
996
  throttled,
992
997
  toDatetimeLocal
993
998
  };
package/package.json CHANGED
@@ -42,5 +42,5 @@
42
42
  "sideEffects": false,
43
43
  "type": "module",
44
44
  "types": "dist/index.d.ts",
45
- "version": "1.1.5"
45
+ "version": "1.1.6"
46
46
  }