@ls-stack/utils 3.40.0 → 3.42.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/dist/arrayUtils.d.cts +70 -57
- package/dist/arrayUtils.d.ts +70 -57
- package/dist/assertions.d.cts +128 -117
- package/dist/assertions.d.ts +128 -117
- package/dist/asyncQueue.cjs +117 -112
- package/dist/asyncQueue.d.cts +217 -215
- package/dist/asyncQueue.d.ts +217 -215
- package/dist/asyncQueue.js +117 -112
- package/dist/awaitDebounce.d.cts +26 -23
- package/dist/awaitDebounce.d.ts +26 -23
- package/dist/cache.cjs +2 -1
- package/dist/cache.d.cts +15 -9
- package/dist/cache.d.ts +15 -9
- package/dist/cache.js +2 -1
- package/dist/{chunk-ADM37GSC.js → chunk-7L4KCZJJ.js} +12 -10
- package/dist/{chunk-KW55OTUG.js → chunk-B3KFV2MH.js} +2 -2
- package/dist/chunk-GMJTLFM6.js +60 -0
- package/dist/{chunk-GHAQOUA6.js → chunk-Y45CE75W.js} +22 -1
- package/dist/concurrentCalls.d.cts +8 -4
- package/dist/concurrentCalls.d.ts +8 -4
- package/dist/concurrentCalls.js +1 -1
- package/dist/debounce.d.cts +12 -20
- package/dist/debounce.d.ts +12 -20
- package/dist/dedent.d.cts +31 -31
- package/dist/dedent.d.ts +31 -31
- package/dist/deepEqual.d.cts +10 -9
- package/dist/deepEqual.d.ts +10 -9
- package/dist/filterObjectOrArrayKeys.d.cts +44 -19
- package/dist/filterObjectOrArrayKeys.d.ts +44 -19
- package/dist/getAutoIncrementId.d.cts +25 -23
- package/dist/getAutoIncrementId.d.ts +25 -23
- package/dist/getCompositeKey.d.cts +2 -1
- package/dist/getCompositeKey.d.ts +2 -1
- package/dist/getValueStableKey.d.cts +4 -3
- package/dist/getValueStableKey.d.ts +4 -3
- package/dist/mutationUtils.d.cts +2 -2
- package/dist/mutationUtils.d.ts +2 -2
- package/dist/objUtils.cjs +51 -2
- package/dist/objUtils.d.cts +8 -3
- package/dist/objUtils.d.ts +8 -3
- package/dist/objUtils.js +11 -3
- package/dist/parallelAsyncCalls.cjs +2 -1
- package/dist/parallelAsyncCalls.d.cts +5 -4
- package/dist/parallelAsyncCalls.d.ts +5 -4
- package/dist/parallelAsyncCalls.js +2 -1
- package/dist/retryOnError.d.cts +23 -20
- package/dist/retryOnError.d.ts +23 -20
- package/dist/runShellCmd.d.cts +40 -40
- package/dist/runShellCmd.d.ts +40 -40
- package/dist/safeJson.d.cts +6 -2
- package/dist/safeJson.d.ts +6 -2
- package/dist/saferTyping.d.cts +21 -14
- package/dist/saferTyping.d.ts +21 -14
- package/dist/shallowEqual.d.cts +1 -1
- package/dist/shallowEqual.d.ts +1 -1
- package/dist/stringUtils.cjs +2 -2
- package/dist/stringUtils.d.cts +6 -7
- package/dist/stringUtils.d.ts +6 -7
- package/dist/stringUtils.js +1 -1
- package/dist/testUtils.cjs +11 -9
- package/dist/testUtils.d.cts +32 -14
- package/dist/testUtils.d.ts +32 -14
- package/dist/testUtils.js +4 -3
- package/dist/throttle.d.cts +57 -48
- package/dist/throttle.d.ts +57 -48
- package/dist/timers.d.cts +68 -63
- package/dist/timers.d.ts +68 -63
- package/dist/tsResult.d.cts +7 -6
- package/dist/tsResult.d.ts +7 -6
- package/dist/typeGuards.d.cts +65 -64
- package/dist/typeGuards.d.ts +65 -64
- package/dist/typeUtils.d.cts +18 -4
- package/dist/typeUtils.d.ts +18 -4
- package/dist/typedStrings.d.cts +68 -57
- package/dist/typedStrings.d.ts +68 -57
- package/dist/typingFnUtils.cjs +15 -0
- package/dist/typingFnUtils.d.cts +34 -7
- package/dist/typingFnUtils.d.ts +34 -7
- package/dist/typingFnUtils.js +18 -35
- package/dist/typingTestUtils.cjs +7 -1
- package/dist/typingTestUtils.d.cts +52 -11
- package/dist/typingTestUtils.d.ts +52 -11
- package/dist/typingTestUtils.js +7 -1
- package/dist/typingUtils.d.cts +4 -1
- package/dist/typingUtils.d.ts +4 -1
- package/dist/yamlStringify.cjs +11 -9
- package/dist/yamlStringify.js +2 -2
- package/package.json +5 -1
- package/docs/README.md +0 -72
- package/docs/_media/modules.md +0 -58
- package/docs/arrayUtils/-internal-.md +0 -179
- package/docs/arrayUtils/README.md +0 -550
- package/docs/assertions/-internal-.md +0 -63
- package/docs/assertions/README.md +0 -565
- package/docs/asyncQueue/-internal-.md +0 -297
- package/docs/asyncQueue/README.md +0 -1485
- package/docs/awaitDebounce.md +0 -66
- package/docs/cache/-internal-.md +0 -168
- package/docs/cache/README.md +0 -360
- package/docs/castValues.md +0 -47
- package/docs/concurrentCalls/-internal-.md +0 -490
- package/docs/concurrentCalls/README.md +0 -299
- package/docs/consoleFmt.md +0 -115
- package/docs/conversions.md +0 -27
- package/docs/createThrottleController/-internal-.md +0 -73
- package/docs/createThrottleController/README.md +0 -31
- package/docs/debounce.md +0 -188
- package/docs/dedent/-internal-.md +0 -17
- package/docs/dedent/README.md +0 -204
- package/docs/deepEqual.md +0 -94
- package/docs/enhancedMap.md +0 -358
- package/docs/exhaustiveMatch/-internal-.md +0 -39
- package/docs/exhaustiveMatch/README.md +0 -146
- package/docs/filterObjectOrArrayKeys.md +0 -109
- package/docs/getAutoIncrementId.md +0 -93
- package/docs/getCompositeKey.md +0 -39
- package/docs/getValueStableKey.md +0 -57
- package/docs/hash.md +0 -31
- package/docs/interpolate/-internal-.md +0 -61
- package/docs/interpolate/README.md +0 -62
- package/docs/keepPrevIfUnchanged.md +0 -43
- package/docs/levenshtein.md +0 -93
- package/docs/main.md +0 -21
- package/docs/mathUtils.md +0 -137
- package/docs/modules.md +0 -58
- package/docs/mutationUtils.md +0 -44
- package/docs/objUtils.md +0 -237
- package/docs/parallelAsyncCalls/-internal-.md +0 -347
- package/docs/parallelAsyncCalls/README.md +0 -45
- package/docs/promiseUtils/-internal-.md +0 -69
- package/docs/promiseUtils/README.md +0 -31
- package/docs/retryOnError/-internal-.md +0 -111
- package/docs/retryOnError/README.md +0 -168
- package/docs/runShellCmd/-internal-.md +0 -111
- package/docs/runShellCmd/README.md +0 -201
- package/docs/safeJson.md +0 -51
- package/docs/saferTyping.md +0 -228
- package/docs/serializeXML.md +0 -100
- package/docs/shallowEqual.md +0 -33
- package/docs/sleep.md +0 -27
- package/docs/stringUtils/-internal-.md +0 -17
- package/docs/stringUtils/README.md +0 -270
- package/docs/testUtils.md +0 -382
- package/docs/throttle/-internal-.md +0 -47
- package/docs/throttle/README.md +0 -178
- package/docs/time.md +0 -274
- package/docs/timers.md +0 -256
- package/docs/tsResult/-internal-.md +0 -327
- package/docs/tsResult/README.md +0 -702
- package/docs/typeGuards.md +0 -399
- package/docs/typeUtils/-internal-.md +0 -99
- package/docs/typeUtils/README.md +0 -195
- package/docs/typeUtils.typesTest.md +0 -7
- package/docs/typedStrings.md +0 -458
- package/docs/typingFnUtils/-internal-.md +0 -43
- package/docs/typingFnUtils/README.md +0 -317
- package/docs/typingTestUtils.md +0 -172
- package/docs/typingUtils.md +0 -135
- package/docs/yamlStringify.md +0 -83
package/dist/testUtils.d.cts
CHANGED
|
@@ -42,26 +42,32 @@ declare function waitController(): {
|
|
|
42
42
|
stopWaitingAfter: (ms: number) => void;
|
|
43
43
|
};
|
|
44
44
|
/**
|
|
45
|
-
* Produces a more compact and readable snapshot of a value using yaml.
|
|
46
|
-
*
|
|
45
|
+
* Produces a more compact and readable snapshot of a value using yaml. By
|
|
46
|
+
* default booleans are shown as `✅` and `❌`, use `showBooleansAs` to
|
|
47
|
+
* disable/configure this.
|
|
47
48
|
*
|
|
48
49
|
* Filtering patterns in `rejectKeys` and `filterKeys`:
|
|
50
|
+
*
|
|
49
51
|
* - `'prop'` - Only root-level properties named 'prop'
|
|
50
52
|
* - `'**prop'` - Any property named exactly 'prop' at any level (root or nested)
|
|
51
|
-
* - `'*.prop'` - Any nested property named 'prop' at second level (excludes
|
|
53
|
+
* - `'*.prop'` - Any nested property named 'prop' at second level (excludes
|
|
54
|
+
* root-level matches)
|
|
52
55
|
* - `'test.*.prop'` - Any property named 'prop' at second level of 'test'
|
|
53
56
|
* - `'test.*.test.**prop'` - Any property named 'prop' inside of 'test.*.test'
|
|
54
57
|
* - `'prop.nested'` - Exact nested property paths like `obj.prop.nested`
|
|
55
|
-
* - `'prop.**nested'` - All nested properties inside root `prop` with name
|
|
58
|
+
* - `'prop.**nested'` - All nested properties inside root `prop` with name
|
|
59
|
+
* `nested`
|
|
56
60
|
* - `'prop[0]'` - The first item of the `prop` array
|
|
57
61
|
* - `'prop[*]'` - All items of the `prop` array
|
|
58
62
|
* - `'prop[0].nested'` - `nested` prop of the first item of the `prop` array
|
|
59
63
|
* - `'prop[*].nested'` - `nested` prop of all items of the `prop` array
|
|
60
64
|
* - `'prop[*]**nested'` - all `nested` props of all items of the `prop` array
|
|
61
65
|
* - `'prop[0-2]'` - The first three items of the `prop` array
|
|
62
|
-
* - `'prop[4-*]'` - All items of the `prop` array from the fourth index to the
|
|
66
|
+
* - `'prop[4-*]'` - All items of the `prop` array from the fourth index to the
|
|
67
|
+
* end
|
|
63
68
|
* - `'prop[0-2].nested.**prop'` - Combining multiple nested patterns is supported
|
|
64
69
|
* - Root array:
|
|
70
|
+
*
|
|
65
71
|
* - `'[0]'` - The first item of the root array
|
|
66
72
|
* - `'[*]'` - All items of the array
|
|
67
73
|
* - `'[0].nested'` - `nested` prop of the first item of the array
|
|
@@ -70,27 +76,39 @@ declare function waitController(): {
|
|
|
70
76
|
* - `'[0-2]'` - The first three items of the array
|
|
71
77
|
* - `'[4-*]'` - All items of the array from the fourth index to the end
|
|
72
78
|
* - Expanding the patterns with parentheses:
|
|
73
|
-
*
|
|
79
|
+
*
|
|
80
|
+
* - `'prop.test.(prop1|prop2|prop3.prop4)'` - Will produce `prop.test.prop1`,
|
|
81
|
+
* `prop.test.prop2`, and `prop.test.prop3.prop4`
|
|
74
82
|
* - Array filtering by value:
|
|
75
|
-
*
|
|
76
|
-
* - `'users[%name="John"
|
|
77
|
-
*
|
|
83
|
+
*
|
|
84
|
+
* - `'users[%name="John"]'` - Filters the `users` with the `name` property equal
|
|
85
|
+
* to `John`
|
|
86
|
+
* - `'users[%name="John" | "Jane"]'` - Filters the `users` with the `name`
|
|
87
|
+
* property equal to `John` or `Jane`
|
|
88
|
+
* - `'users[%name="John" | "Jane" && %age=20]'` - AND and OR are supported by
|
|
89
|
+
* using `&&` and `||`, nesting logical operators is not supported yet
|
|
78
90
|
* - `'users[%config.name="John" | "Jane"]'` - Dot notation is supported
|
|
79
91
|
*
|
|
80
92
|
* Check more supported patterns in {@link filterObjectOrArrayKeys} docs.
|
|
81
93
|
*
|
|
82
94
|
* @param value - The value to snapshot.
|
|
83
95
|
* @param options - The options for the snapshot.
|
|
84
|
-
* @param options.collapseObjects - Whether to collapse objects into a single
|
|
96
|
+
* @param options.collapseObjects - Whether to collapse objects into a single
|
|
97
|
+
* line.
|
|
85
98
|
* @param options.maxLineLength - The maximum length of a line.
|
|
86
99
|
* @param options.showUndefined - Whether to show undefined values.
|
|
87
|
-
* @param options.showBooleansAs - Whether to show booleans as text, by default
|
|
100
|
+
* @param options.showBooleansAs - Whether to show booleans as text, by default
|
|
101
|
+
* true is `✅` and false is `❌`
|
|
88
102
|
* @param options.rejectKeys - The keys to reject.
|
|
89
103
|
* @param options.filterKeys - The keys to filter.
|
|
90
104
|
* @param options.ignoreProps - The props to ignore.
|
|
91
|
-
* @param options.replaceValues - Function to replace values at specific paths.
|
|
92
|
-
*
|
|
93
|
-
* @param options.
|
|
105
|
+
* @param options.replaceValues - Function to replace values at specific paths.
|
|
106
|
+
* Returns `false` to keep original value or `{newValue}` to replace.
|
|
107
|
+
* @param options.sortKeys - Sort all keys by a specific order (default:
|
|
108
|
+
* `simpleValuesFirst`).
|
|
109
|
+
* @param options.sortPatterns - Sort specific keys by pattern. Use to control
|
|
110
|
+
* the order of specific properties. The same patterns as `filterKeys` are
|
|
111
|
+
* supported.
|
|
94
112
|
* @returns The compact snapshot of the value.
|
|
95
113
|
*/
|
|
96
114
|
declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLength, showUndefined, showBooleansAs, replaceValues, rejectKeys, filterKeys, sortKeys, sortPatterns, ...options }?: YamlStringifyOptions & {
|
package/dist/testUtils.d.ts
CHANGED
|
@@ -42,26 +42,32 @@ declare function waitController(): {
|
|
|
42
42
|
stopWaitingAfter: (ms: number) => void;
|
|
43
43
|
};
|
|
44
44
|
/**
|
|
45
|
-
* Produces a more compact and readable snapshot of a value using yaml.
|
|
46
|
-
*
|
|
45
|
+
* Produces a more compact and readable snapshot of a value using yaml. By
|
|
46
|
+
* default booleans are shown as `✅` and `❌`, use `showBooleansAs` to
|
|
47
|
+
* disable/configure this.
|
|
47
48
|
*
|
|
48
49
|
* Filtering patterns in `rejectKeys` and `filterKeys`:
|
|
50
|
+
*
|
|
49
51
|
* - `'prop'` - Only root-level properties named 'prop'
|
|
50
52
|
* - `'**prop'` - Any property named exactly 'prop' at any level (root or nested)
|
|
51
|
-
* - `'*.prop'` - Any nested property named 'prop' at second level (excludes
|
|
53
|
+
* - `'*.prop'` - Any nested property named 'prop' at second level (excludes
|
|
54
|
+
* root-level matches)
|
|
52
55
|
* - `'test.*.prop'` - Any property named 'prop' at second level of 'test'
|
|
53
56
|
* - `'test.*.test.**prop'` - Any property named 'prop' inside of 'test.*.test'
|
|
54
57
|
* - `'prop.nested'` - Exact nested property paths like `obj.prop.nested`
|
|
55
|
-
* - `'prop.**nested'` - All nested properties inside root `prop` with name
|
|
58
|
+
* - `'prop.**nested'` - All nested properties inside root `prop` with name
|
|
59
|
+
* `nested`
|
|
56
60
|
* - `'prop[0]'` - The first item of the `prop` array
|
|
57
61
|
* - `'prop[*]'` - All items of the `prop` array
|
|
58
62
|
* - `'prop[0].nested'` - `nested` prop of the first item of the `prop` array
|
|
59
63
|
* - `'prop[*].nested'` - `nested` prop of all items of the `prop` array
|
|
60
64
|
* - `'prop[*]**nested'` - all `nested` props of all items of the `prop` array
|
|
61
65
|
* - `'prop[0-2]'` - The first three items of the `prop` array
|
|
62
|
-
* - `'prop[4-*]'` - All items of the `prop` array from the fourth index to the
|
|
66
|
+
* - `'prop[4-*]'` - All items of the `prop` array from the fourth index to the
|
|
67
|
+
* end
|
|
63
68
|
* - `'prop[0-2].nested.**prop'` - Combining multiple nested patterns is supported
|
|
64
69
|
* - Root array:
|
|
70
|
+
*
|
|
65
71
|
* - `'[0]'` - The first item of the root array
|
|
66
72
|
* - `'[*]'` - All items of the array
|
|
67
73
|
* - `'[0].nested'` - `nested` prop of the first item of the array
|
|
@@ -70,27 +76,39 @@ declare function waitController(): {
|
|
|
70
76
|
* - `'[0-2]'` - The first three items of the array
|
|
71
77
|
* - `'[4-*]'` - All items of the array from the fourth index to the end
|
|
72
78
|
* - Expanding the patterns with parentheses:
|
|
73
|
-
*
|
|
79
|
+
*
|
|
80
|
+
* - `'prop.test.(prop1|prop2|prop3.prop4)'` - Will produce `prop.test.prop1`,
|
|
81
|
+
* `prop.test.prop2`, and `prop.test.prop3.prop4`
|
|
74
82
|
* - Array filtering by value:
|
|
75
|
-
*
|
|
76
|
-
* - `'users[%name="John"
|
|
77
|
-
*
|
|
83
|
+
*
|
|
84
|
+
* - `'users[%name="John"]'` - Filters the `users` with the `name` property equal
|
|
85
|
+
* to `John`
|
|
86
|
+
* - `'users[%name="John" | "Jane"]'` - Filters the `users` with the `name`
|
|
87
|
+
* property equal to `John` or `Jane`
|
|
88
|
+
* - `'users[%name="John" | "Jane" && %age=20]'` - AND and OR are supported by
|
|
89
|
+
* using `&&` and `||`, nesting logical operators is not supported yet
|
|
78
90
|
* - `'users[%config.name="John" | "Jane"]'` - Dot notation is supported
|
|
79
91
|
*
|
|
80
92
|
* Check more supported patterns in {@link filterObjectOrArrayKeys} docs.
|
|
81
93
|
*
|
|
82
94
|
* @param value - The value to snapshot.
|
|
83
95
|
* @param options - The options for the snapshot.
|
|
84
|
-
* @param options.collapseObjects - Whether to collapse objects into a single
|
|
96
|
+
* @param options.collapseObjects - Whether to collapse objects into a single
|
|
97
|
+
* line.
|
|
85
98
|
* @param options.maxLineLength - The maximum length of a line.
|
|
86
99
|
* @param options.showUndefined - Whether to show undefined values.
|
|
87
|
-
* @param options.showBooleansAs - Whether to show booleans as text, by default
|
|
100
|
+
* @param options.showBooleansAs - Whether to show booleans as text, by default
|
|
101
|
+
* true is `✅` and false is `❌`
|
|
88
102
|
* @param options.rejectKeys - The keys to reject.
|
|
89
103
|
* @param options.filterKeys - The keys to filter.
|
|
90
104
|
* @param options.ignoreProps - The props to ignore.
|
|
91
|
-
* @param options.replaceValues - Function to replace values at specific paths.
|
|
92
|
-
*
|
|
93
|
-
* @param options.
|
|
105
|
+
* @param options.replaceValues - Function to replace values at specific paths.
|
|
106
|
+
* Returns `false` to keep original value or `{newValue}` to replace.
|
|
107
|
+
* @param options.sortKeys - Sort all keys by a specific order (default:
|
|
108
|
+
* `simpleValuesFirst`).
|
|
109
|
+
* @param options.sortPatterns - Sort specific keys by pattern. Use to control
|
|
110
|
+
* the order of specific properties. The same patterns as `filterKeys` are
|
|
111
|
+
* supported.
|
|
94
112
|
* @returns The compact snapshot of the value.
|
|
95
113
|
*/
|
|
96
114
|
declare function compactSnapshot(value: unknown, { collapseObjects, maxLineLength, showUndefined, showBooleansAs, replaceValues, rejectKeys, filterKeys, sortKeys, sortPatterns, ...options }?: YamlStringifyOptions & {
|
package/dist/testUtils.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
yamlStringify
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-7L4KCZJJ.js";
|
|
4
4
|
import {
|
|
5
5
|
omit,
|
|
6
6
|
pick
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-Y45CE75W.js";
|
|
8
|
+
import "./chunk-GMJTLFM6.js";
|
|
8
9
|
import "./chunk-IATIXMCE.js";
|
|
9
10
|
import {
|
|
10
11
|
deepEqual
|
|
@@ -18,7 +19,7 @@ import {
|
|
|
18
19
|
import {
|
|
19
20
|
clampMin
|
|
20
21
|
} from "./chunk-HTCYUMDR.js";
|
|
21
|
-
import "./chunk-
|
|
22
|
+
import "./chunk-B3KFV2MH.js";
|
|
22
23
|
import {
|
|
23
24
|
arrayWithPrevAndIndex,
|
|
24
25
|
filterAndMap
|
package/dist/throttle.d.cts
CHANGED
|
@@ -4,54 +4,85 @@ import { __LEGIT_ANY_FUNCTION__ } from './saferTyping.cjs';
|
|
|
4
4
|
interface ThrottleSettings {
|
|
5
5
|
/**
|
|
6
6
|
* Specify invoking on the leading edge of the timeout.
|
|
7
|
+
*
|
|
7
8
|
* @default true
|
|
8
9
|
*/
|
|
9
10
|
leading?: boolean;
|
|
10
11
|
/**
|
|
11
12
|
* Specify invoking on the trailing edge of the timeout.
|
|
13
|
+
*
|
|
12
14
|
* @default true
|
|
13
15
|
*/
|
|
14
16
|
trailing?: boolean;
|
|
15
17
|
}
|
|
16
18
|
/**
|
|
17
|
-
* Creates a throttled function that only invokes the provided function at most
|
|
18
|
-
*
|
|
19
|
+
* Creates a throttled function that only invokes the provided function at most
|
|
20
|
+
* once per every `wait` milliseconds. The throttled function comes with a
|
|
21
|
+
* `cancel` method to cancel delayed invocations and a `flush` method to
|
|
22
|
+
* immediately invoke them.
|
|
19
23
|
*
|
|
20
|
-
* Throttling is useful for rate-limiting events that fire frequently, like
|
|
21
|
-
* Unlike debouncing, throttling guarantees the
|
|
24
|
+
* Throttling is useful for rate-limiting events that fire frequently, like
|
|
25
|
+
* scroll or resize handlers. Unlike debouncing, throttling guarantees the
|
|
26
|
+
* function is called at regular intervals.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const throttledSave = throttle(saveData, 1000);
|
|
31
|
+
*
|
|
32
|
+
* // Will only call saveData at most once per second
|
|
33
|
+
* throttledSave();
|
|
34
|
+
* throttledSave();
|
|
35
|
+
* throttledSave();
|
|
36
|
+
* ```;
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* // Only invoke on trailing edge
|
|
41
|
+
* const throttledHandler = throttle(handleScroll, 100, { leading: false });
|
|
42
|
+
* ```;
|
|
22
43
|
*
|
|
23
44
|
* @template T - The type of the function to throttle
|
|
24
45
|
* @param func - The function to throttle
|
|
25
46
|
* @param wait - The number of milliseconds to throttle invocations to
|
|
26
47
|
* @param options - The options object
|
|
27
|
-
* @param options.leading - Specify invoking on the leading edge of the timeout
|
|
28
|
-
*
|
|
48
|
+
* @param options.leading - Specify invoking on the leading edge of the timeout
|
|
49
|
+
* (default: true)
|
|
50
|
+
* @param options.trailing - Specify invoking on the trailing edge of the
|
|
51
|
+
* timeout (default: true)
|
|
29
52
|
* @returns Returns the new throttled function
|
|
53
|
+
*/
|
|
54
|
+
declare function throttle<T extends __LEGIT_ANY_FUNCTION__>(func: T, wait: number, options?: ThrottleSettings): DebouncedFunc<T>;
|
|
55
|
+
/**
|
|
56
|
+
* Creates a factory for throttled functions that caches throttled instances
|
|
57
|
+
* based on function arguments. Each unique set of arguments gets its own
|
|
58
|
+
* throttled function instance, allowing for fine-grained throttling control per
|
|
59
|
+
* argument combination.
|
|
60
|
+
*
|
|
61
|
+
* This is useful when you want to throttle calls to the same function but with
|
|
62
|
+
* different parameters independently. For example, throttling API calls per
|
|
63
|
+
* user ID or throttling UI updates per component.
|
|
30
64
|
*
|
|
31
65
|
* @example
|
|
32
|
-
*
|
|
33
|
-
*
|
|
66
|
+
* ```ts
|
|
67
|
+
* const apiThrottle = createThrottledFunctionFactory(
|
|
68
|
+
* 1000,
|
|
69
|
+
* (userId: string, action: string) => callAPI(userId, action)
|
|
70
|
+
* );
|
|
34
71
|
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
72
|
+
* // Each user gets their own throttled instance
|
|
73
|
+
* apiThrottle.call('user1', 'update'); // Executes immediately
|
|
74
|
+
* apiThrottle.call('user2', 'update'); // Executes immediately (different user)
|
|
75
|
+
* apiThrottle.call('user1', 'update'); // Throttled (same user)
|
|
76
|
+
* ```;
|
|
40
77
|
*
|
|
41
78
|
* @example
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
* Creates a factory for throttled functions that caches throttled instances based on function arguments.
|
|
50
|
-
* Each unique set of arguments gets its own throttled function instance, allowing for fine-grained
|
|
51
|
-
* throttling control per argument combination.
|
|
52
|
-
*
|
|
53
|
-
* This is useful when you want to throttle calls to the same function but with different parameters
|
|
54
|
-
* independently. For example, throttling API calls per user ID or throttling UI updates per component.
|
|
79
|
+
* ```ts
|
|
80
|
+
* // Throttle UI updates per component
|
|
81
|
+
* const updateThrottle = createThrottledFunctionFactory(
|
|
82
|
+
* 100,
|
|
83
|
+
* (componentId: string, data: any) => updateComponent(componentId, data)
|
|
84
|
+
* );
|
|
85
|
+
* ```;
|
|
55
86
|
*
|
|
56
87
|
* @template T - The type of arguments the callback function accepts
|
|
57
88
|
* @template R - The return type of the callback function
|
|
@@ -59,28 +90,6 @@ declare function throttle<T extends __LEGIT_ANY_FUNCTION__>(func: T, wait: numbe
|
|
|
59
90
|
* @param callback - The function to throttle
|
|
60
91
|
* @param options - The throttle options (leading/trailing behavior)
|
|
61
92
|
* @returns An object with a `call` method that accepts the callback arguments
|
|
62
|
-
*
|
|
63
|
-
* @example
|
|
64
|
-
* ```ts
|
|
65
|
-
* const apiThrottle = createThrottledFunctionFactory(
|
|
66
|
-
* 1000,
|
|
67
|
-
* (userId: string, action: string) => callAPI(userId, action)
|
|
68
|
-
* );
|
|
69
|
-
*
|
|
70
|
-
* // Each user gets their own throttled instance
|
|
71
|
-
* apiThrottle.call('user1', 'update'); // Executes immediately
|
|
72
|
-
* apiThrottle.call('user2', 'update'); // Executes immediately (different user)
|
|
73
|
-
* apiThrottle.call('user1', 'update'); // Throttled (same user)
|
|
74
|
-
* ```
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* ```ts
|
|
78
|
-
* // Throttle UI updates per component
|
|
79
|
-
* const updateThrottle = createThrottledFunctionFactory(
|
|
80
|
-
* 100,
|
|
81
|
-
* (componentId: string, data: any) => updateComponent(componentId, data)
|
|
82
|
-
* );
|
|
83
|
-
* ```
|
|
84
93
|
*/
|
|
85
94
|
declare function createThrottledFunctionFactory<T extends string | number | null | undefined | boolean, R>(wait: number, callback: (...args: T[]) => R, options?: ThrottleSettings): {
|
|
86
95
|
call: (...args: T[]) => R | undefined;
|
package/dist/throttle.d.ts
CHANGED
|
@@ -4,54 +4,85 @@ import { __LEGIT_ANY_FUNCTION__ } from './saferTyping.js';
|
|
|
4
4
|
interface ThrottleSettings {
|
|
5
5
|
/**
|
|
6
6
|
* Specify invoking on the leading edge of the timeout.
|
|
7
|
+
*
|
|
7
8
|
* @default true
|
|
8
9
|
*/
|
|
9
10
|
leading?: boolean;
|
|
10
11
|
/**
|
|
11
12
|
* Specify invoking on the trailing edge of the timeout.
|
|
13
|
+
*
|
|
12
14
|
* @default true
|
|
13
15
|
*/
|
|
14
16
|
trailing?: boolean;
|
|
15
17
|
}
|
|
16
18
|
/**
|
|
17
|
-
* Creates a throttled function that only invokes the provided function at most
|
|
18
|
-
*
|
|
19
|
+
* Creates a throttled function that only invokes the provided function at most
|
|
20
|
+
* once per every `wait` milliseconds. The throttled function comes with a
|
|
21
|
+
* `cancel` method to cancel delayed invocations and a `flush` method to
|
|
22
|
+
* immediately invoke them.
|
|
19
23
|
*
|
|
20
|
-
* Throttling is useful for rate-limiting events that fire frequently, like
|
|
21
|
-
* Unlike debouncing, throttling guarantees the
|
|
24
|
+
* Throttling is useful for rate-limiting events that fire frequently, like
|
|
25
|
+
* scroll or resize handlers. Unlike debouncing, throttling guarantees the
|
|
26
|
+
* function is called at regular intervals.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const throttledSave = throttle(saveData, 1000);
|
|
31
|
+
*
|
|
32
|
+
* // Will only call saveData at most once per second
|
|
33
|
+
* throttledSave();
|
|
34
|
+
* throttledSave();
|
|
35
|
+
* throttledSave();
|
|
36
|
+
* ```;
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* // Only invoke on trailing edge
|
|
41
|
+
* const throttledHandler = throttle(handleScroll, 100, { leading: false });
|
|
42
|
+
* ```;
|
|
22
43
|
*
|
|
23
44
|
* @template T - The type of the function to throttle
|
|
24
45
|
* @param func - The function to throttle
|
|
25
46
|
* @param wait - The number of milliseconds to throttle invocations to
|
|
26
47
|
* @param options - The options object
|
|
27
|
-
* @param options.leading - Specify invoking on the leading edge of the timeout
|
|
28
|
-
*
|
|
48
|
+
* @param options.leading - Specify invoking on the leading edge of the timeout
|
|
49
|
+
* (default: true)
|
|
50
|
+
* @param options.trailing - Specify invoking on the trailing edge of the
|
|
51
|
+
* timeout (default: true)
|
|
29
52
|
* @returns Returns the new throttled function
|
|
53
|
+
*/
|
|
54
|
+
declare function throttle<T extends __LEGIT_ANY_FUNCTION__>(func: T, wait: number, options?: ThrottleSettings): DebouncedFunc<T>;
|
|
55
|
+
/**
|
|
56
|
+
* Creates a factory for throttled functions that caches throttled instances
|
|
57
|
+
* based on function arguments. Each unique set of arguments gets its own
|
|
58
|
+
* throttled function instance, allowing for fine-grained throttling control per
|
|
59
|
+
* argument combination.
|
|
60
|
+
*
|
|
61
|
+
* This is useful when you want to throttle calls to the same function but with
|
|
62
|
+
* different parameters independently. For example, throttling API calls per
|
|
63
|
+
* user ID or throttling UI updates per component.
|
|
30
64
|
*
|
|
31
65
|
* @example
|
|
32
|
-
*
|
|
33
|
-
*
|
|
66
|
+
* ```ts
|
|
67
|
+
* const apiThrottle = createThrottledFunctionFactory(
|
|
68
|
+
* 1000,
|
|
69
|
+
* (userId: string, action: string) => callAPI(userId, action)
|
|
70
|
+
* );
|
|
34
71
|
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
72
|
+
* // Each user gets their own throttled instance
|
|
73
|
+
* apiThrottle.call('user1', 'update'); // Executes immediately
|
|
74
|
+
* apiThrottle.call('user2', 'update'); // Executes immediately (different user)
|
|
75
|
+
* apiThrottle.call('user1', 'update'); // Throttled (same user)
|
|
76
|
+
* ```;
|
|
40
77
|
*
|
|
41
78
|
* @example
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
* Creates a factory for throttled functions that caches throttled instances based on function arguments.
|
|
50
|
-
* Each unique set of arguments gets its own throttled function instance, allowing for fine-grained
|
|
51
|
-
* throttling control per argument combination.
|
|
52
|
-
*
|
|
53
|
-
* This is useful when you want to throttle calls to the same function but with different parameters
|
|
54
|
-
* independently. For example, throttling API calls per user ID or throttling UI updates per component.
|
|
79
|
+
* ```ts
|
|
80
|
+
* // Throttle UI updates per component
|
|
81
|
+
* const updateThrottle = createThrottledFunctionFactory(
|
|
82
|
+
* 100,
|
|
83
|
+
* (componentId: string, data: any) => updateComponent(componentId, data)
|
|
84
|
+
* );
|
|
85
|
+
* ```;
|
|
55
86
|
*
|
|
56
87
|
* @template T - The type of arguments the callback function accepts
|
|
57
88
|
* @template R - The return type of the callback function
|
|
@@ -59,28 +90,6 @@ declare function throttle<T extends __LEGIT_ANY_FUNCTION__>(func: T, wait: numbe
|
|
|
59
90
|
* @param callback - The function to throttle
|
|
60
91
|
* @param options - The throttle options (leading/trailing behavior)
|
|
61
92
|
* @returns An object with a `call` method that accepts the callback arguments
|
|
62
|
-
*
|
|
63
|
-
* @example
|
|
64
|
-
* ```ts
|
|
65
|
-
* const apiThrottle = createThrottledFunctionFactory(
|
|
66
|
-
* 1000,
|
|
67
|
-
* (userId: string, action: string) => callAPI(userId, action)
|
|
68
|
-
* );
|
|
69
|
-
*
|
|
70
|
-
* // Each user gets their own throttled instance
|
|
71
|
-
* apiThrottle.call('user1', 'update'); // Executes immediately
|
|
72
|
-
* apiThrottle.call('user2', 'update'); // Executes immediately (different user)
|
|
73
|
-
* apiThrottle.call('user1', 'update'); // Throttled (same user)
|
|
74
|
-
* ```
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* ```ts
|
|
78
|
-
* // Throttle UI updates per component
|
|
79
|
-
* const updateThrottle = createThrottledFunctionFactory(
|
|
80
|
-
* 100,
|
|
81
|
-
* (componentId: string, data: any) => updateComponent(componentId, data)
|
|
82
|
-
* );
|
|
83
|
-
* ```
|
|
84
93
|
*/
|
|
85
94
|
declare function createThrottledFunctionFactory<T extends string | number | null | undefined | boolean, R>(wait: number, callback: (...args: T[]) => R, options?: ThrottleSettings): {
|
|
86
95
|
call: (...args: T[]) => R | undefined;
|
package/dist/timers.d.cts
CHANGED
|
@@ -2,69 +2,70 @@ type CleanupTimer = () => void;
|
|
|
2
2
|
/**
|
|
3
3
|
* Creates a timeout with automatic cleanup capability.
|
|
4
4
|
*
|
|
5
|
-
* Returns a cleanup function that can be called to cancel the timeout.
|
|
6
|
-
*
|
|
5
|
+
* Returns a cleanup function that can be called to cancel the timeout. The
|
|
6
|
+
* cleanup function is idempotent - calling it multiple times is safe.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const cleanup = createTimeout(1000, () => {
|
|
11
|
+
* console.log('Timeout completed');
|
|
12
|
+
* });
|
|
13
|
+
*
|
|
14
|
+
* // Cancel the timeout before it completes
|
|
15
|
+
* cleanup();
|
|
16
|
+
* ```;
|
|
7
17
|
*
|
|
8
18
|
* @param ms - The timeout duration in milliseconds
|
|
9
19
|
* @param callback - The function to execute when the timeout completes
|
|
10
20
|
* @returns A cleanup function that cancels the timeout when called
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* const cleanup = createTimeout(1000, () => {
|
|
15
|
-
* console.log('Timeout completed');
|
|
16
|
-
* });
|
|
17
|
-
*
|
|
18
|
-
* // Cancel the timeout before it completes
|
|
19
|
-
* cleanup();
|
|
20
|
-
* ```
|
|
21
21
|
*/
|
|
22
22
|
declare function createTimeout(ms: number, callback: () => void): CleanupTimer;
|
|
23
23
|
/**
|
|
24
24
|
* Creates an interval with automatic cleanup capability.
|
|
25
25
|
*
|
|
26
|
-
* Returns a cleanup function that can be called to cancel the interval.
|
|
27
|
-
*
|
|
26
|
+
* Returns a cleanup function that can be called to cancel the interval. The
|
|
27
|
+
* cleanup function is idempotent - calling it multiple times is safe.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const cleanup = createInterval(1000, () => {
|
|
32
|
+
* console.log('Interval tick');
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // Stop the interval
|
|
36
|
+
* cleanup();
|
|
37
|
+
* ```;
|
|
28
38
|
*
|
|
29
39
|
* @param ms - The interval duration in milliseconds
|
|
30
40
|
* @param callback - The function to execute on each interval tick
|
|
31
41
|
* @returns A cleanup function that cancels the interval when called
|
|
32
|
-
*
|
|
33
|
-
* @example
|
|
34
|
-
* ```typescript
|
|
35
|
-
* const cleanup = createInterval(1000, () => {
|
|
36
|
-
* console.log('Interval tick');
|
|
37
|
-
* });
|
|
38
|
-
*
|
|
39
|
-
* // Stop the interval
|
|
40
|
-
* cleanup();
|
|
41
|
-
* ```
|
|
42
42
|
*/
|
|
43
43
|
declare function createInterval(ms: number, callback: () => void): CleanupTimer;
|
|
44
44
|
/**
|
|
45
45
|
* Creates a timeout that prevents concurrent executions.
|
|
46
46
|
*
|
|
47
|
-
* Each call to the `call` function will cancel any previous pending timeout
|
|
48
|
-
*
|
|
49
|
-
*
|
|
47
|
+
* Each call to the `call` function will cancel any previous pending timeout and
|
|
48
|
+
* start a new one. This is useful for debouncing or ensuring only the last call
|
|
49
|
+
* executes after a delay.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* const { call, clean } = createDebouncedTimeout(1000, () => {
|
|
54
|
+
* console.log('Only the last call executes');
|
|
55
|
+
* });
|
|
56
|
+
*
|
|
57
|
+
* call(); // This will be cancelled
|
|
58
|
+
* call(); // This will be cancelled
|
|
59
|
+
* call(); // Only this one will execute after 1000ms
|
|
60
|
+
*
|
|
61
|
+
* // Or cancel all pending timeouts
|
|
62
|
+
* clean();
|
|
63
|
+
* ```;
|
|
50
64
|
*
|
|
51
65
|
* @param ms - The timeout duration in milliseconds
|
|
52
66
|
* @param callback - The function to execute when the timeout completes
|
|
53
|
-
* @returns An object with `call` to trigger the timeout and `clean` to cancel
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```typescript
|
|
57
|
-
* const { call, clean } = createDebouncedTimeout(1000, () => {
|
|
58
|
-
* console.log('Only the last call executes');
|
|
59
|
-
* });
|
|
60
|
-
*
|
|
61
|
-
* call(); // This will be cancelled
|
|
62
|
-
* call(); // This will be cancelled
|
|
63
|
-
* call(); // Only this one will execute after 1000ms
|
|
64
|
-
*
|
|
65
|
-
* // Or cancel all pending timeouts
|
|
66
|
-
* clean();
|
|
67
|
-
* ```
|
|
67
|
+
* @returns An object with `call` to trigger the timeout and `clean` to cancel
|
|
68
|
+
* it
|
|
68
69
|
*/
|
|
69
70
|
declare function createDebouncedTimeout(ms: number, callback: () => void): {
|
|
70
71
|
call: () => void;
|
|
@@ -73,32 +74,36 @@ declare function createDebouncedTimeout(ms: number, callback: () => void): {
|
|
|
73
74
|
/**
|
|
74
75
|
* Creates a timeout that waits for a condition to become true.
|
|
75
76
|
*
|
|
76
|
-
* Polls the condition function at regular intervals until it returns a truthy
|
|
77
|
-
* then calls the callback with that value. If the condition doesn't
|
|
78
|
-
* within the maximum wait time, the timeout expires without calling
|
|
77
|
+
* Polls the condition function at regular intervals until it returns a truthy
|
|
78
|
+
* value, then calls the callback with that value. If the condition doesn't
|
|
79
|
+
* become true within the maximum wait time, the timeout expires without calling
|
|
80
|
+
* the callback.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const cleanup = createWaitUntil({
|
|
85
|
+
* condition: () => document.getElementById('myElement'),
|
|
86
|
+
* maxWaitMs: 5000,
|
|
87
|
+
* callback: (element) => {
|
|
88
|
+
* console.log('Element found:', element);
|
|
89
|
+
* },
|
|
90
|
+
* checkIntervalMs: 50
|
|
91
|
+
* });
|
|
92
|
+
*
|
|
93
|
+
* // Cancel the condition check
|
|
94
|
+
* cleanup();
|
|
95
|
+
* ```;
|
|
79
96
|
*
|
|
80
97
|
* @template T - The type of value returned by the condition function when true
|
|
81
98
|
* @param options - Configuration options
|
|
82
|
-
* @param options.condition - Function that returns false or a truthy value when
|
|
83
|
-
*
|
|
99
|
+
* @param options.condition - Function that returns false or a truthy value when
|
|
100
|
+
* the condition is met
|
|
101
|
+
* @param options.maxWaitMs - Maximum time to wait for the condition in
|
|
102
|
+
* milliseconds
|
|
84
103
|
* @param options.callback - Function to call when the condition becomes true
|
|
85
|
-
* @param options.checkIntervalMs - How often to check the condition in
|
|
104
|
+
* @param options.checkIntervalMs - How often to check the condition in
|
|
105
|
+
* milliseconds (default: 20)
|
|
86
106
|
* @returns A cleanup function that cancels the condition timeout
|
|
87
|
-
*
|
|
88
|
-
* @example
|
|
89
|
-
* ```typescript
|
|
90
|
-
* const cleanup = createWaitUntil({
|
|
91
|
-
* condition: () => document.getElementById('myElement'),
|
|
92
|
-
* maxWaitMs: 5000,
|
|
93
|
-
* callback: (element) => {
|
|
94
|
-
* console.log('Element found:', element);
|
|
95
|
-
* },
|
|
96
|
-
* checkIntervalMs: 50
|
|
97
|
-
* });
|
|
98
|
-
*
|
|
99
|
-
* // Cancel the condition check
|
|
100
|
-
* cleanup();
|
|
101
|
-
* ```
|
|
102
107
|
*/
|
|
103
108
|
declare function createWaitUntil<T extends NonNullable<unknown>>({ condition, maxWaitMs, callback, checkIntervalMs, }: {
|
|
104
109
|
condition: () => false | T;
|