@ls-stack/utils 3.56.0 → 3.58.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.cjs +24 -0
- package/dist/arrayUtils.d.cts +45 -20
- package/dist/arrayUtils.d.ts +45 -20
- package/dist/arrayUtils.js +5 -1
- package/dist/asyncQueue.js +2 -2
- package/dist/cache.js +3 -3
- package/dist/{chunk-5MNYPLZI.js → chunk-6FBIEPWU.js} +1 -1
- package/dist/{chunk-27AL66CH.js → chunk-BT3UMATU.js} +23 -1
- package/dist/{chunk-NW5H5EW7.js → chunk-CCUPDGSZ.js} +44 -12
- package/dist/chunk-DBOWTYR4.js +49 -0
- package/dist/{chunk-AULH7VMS.js → chunk-DX2524CZ.js} +1 -1
- package/dist/{chunk-6CG6JZKB.js → chunk-LBBC55GE.js} +1 -1
- package/dist/{chunk-Y76LZUOB.js → chunk-WNFRB7P6.js} +1 -1
- package/dist/concurrentCalls.js +4 -4
- package/dist/createThrottleController.js +2 -2
- package/dist/debounce.cjs +44 -12
- package/dist/debounce.d.cts +9 -0
- package/dist/debounce.d.ts +9 -0
- package/dist/debounce.js +1 -1
- package/dist/filterObjectOrArrayKeys.js +2 -2
- package/dist/interpolate.js +1 -1
- package/dist/iteratorUtils.cjs +73 -0
- package/dist/iteratorUtils.d.cts +10 -0
- package/dist/iteratorUtils.d.ts +10 -0
- package/dist/iteratorUtils.js +44 -0
- package/dist/matchPath.js +3 -3
- package/dist/mathUtils.cjs +28 -2
- package/dist/mathUtils.d.cts +48 -1
- package/dist/mathUtils.d.ts +48 -1
- package/dist/mathUtils.js +11 -3
- package/dist/objUtils.js +2 -2
- package/dist/serializeXML.js +1 -1
- package/dist/testUtils.js +4 -4
- package/dist/throttle.cjs +43 -11
- package/dist/throttle.js +1 -1
- package/dist/time.js +2 -2
- package/package.json +5 -1
- package/dist/chunk-HTCYUMDR.js +0 -27
package/dist/arrayUtils.cjs
CHANGED
|
@@ -29,9 +29,11 @@ __export(arrayUtils_exports, {
|
|
|
29
29
|
findBeforeIndex: () => findBeforeIndex,
|
|
30
30
|
getAscIndexOrder: () => getAscIndexOrder,
|
|
31
31
|
hasDuplicates: () => hasDuplicates,
|
|
32
|
+
intersperse: () => intersperse,
|
|
32
33
|
isInArray: () => isInArray,
|
|
33
34
|
rejectArrayUndefinedValues: () => rejectArrayUndefinedValues,
|
|
34
35
|
rejectDuplicates: () => rejectDuplicates,
|
|
36
|
+
repeat: () => repeat,
|
|
35
37
|
sortBy: () => sortBy,
|
|
36
38
|
truncateArray: () => truncateArray
|
|
37
39
|
});
|
|
@@ -176,6 +178,26 @@ function arrayOps(array) {
|
|
|
176
178
|
findAndMap: (predicate) => findAndMap(array, predicate)
|
|
177
179
|
};
|
|
178
180
|
}
|
|
181
|
+
function intersperse(array, separator) {
|
|
182
|
+
const result = [];
|
|
183
|
+
for (let i = 0; i < array.length; i++) {
|
|
184
|
+
result.push(array[i]);
|
|
185
|
+
if (i < array.length - 1) {
|
|
186
|
+
result.push(separator);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
function repeat(value, count, separator) {
|
|
192
|
+
const result = [];
|
|
193
|
+
for (let i = 0; i < count; i++) {
|
|
194
|
+
result.push(value);
|
|
195
|
+
if (separator !== void 0 && i < count - 1) {
|
|
196
|
+
result.push(separator);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
179
201
|
// Annotate the CommonJS export names for ESM import in node:
|
|
180
202
|
0 && (module.exports = {
|
|
181
203
|
arrayOps,
|
|
@@ -187,9 +209,11 @@ function arrayOps(array) {
|
|
|
187
209
|
findBeforeIndex,
|
|
188
210
|
getAscIndexOrder,
|
|
189
211
|
hasDuplicates,
|
|
212
|
+
intersperse,
|
|
190
213
|
isInArray,
|
|
191
214
|
rejectArrayUndefinedValues,
|
|
192
215
|
rejectDuplicates,
|
|
216
|
+
repeat,
|
|
193
217
|
sortBy,
|
|
194
218
|
truncateArray
|
|
195
219
|
});
|
package/dist/arrayUtils.d.cts
CHANGED
|
@@ -82,25 +82,6 @@ declare function rejectArrayUndefinedValues<T extends unknown[]>(array: T): T;
|
|
|
82
82
|
declare function hasDuplicates<T>(array: T[], getKey?: (item: T) => unknown): boolean;
|
|
83
83
|
declare function rejectDuplicates<T>(array: T[], getKey?: (item: T) => unknown): T[];
|
|
84
84
|
declare function truncateArray<T>(array: T[], maxLength: number, appendIfTruncated?: T | ((truncatedCount: number) => T)): T[];
|
|
85
|
-
type ArrayOps<T> = {
|
|
86
|
-
/**
|
|
87
|
-
* Filter and map an array
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
* const items = [1, 2, 3];
|
|
91
|
-
*
|
|
92
|
-
* const enhancedItems = arrayOps(items);
|
|
93
|
-
*
|
|
94
|
-
* enhancedItems.filterAndMap((item) => (item === 2 ? false : item));
|
|
95
|
-
*
|
|
96
|
-
* @param mapFilter - A function that takes an item and returns a value or
|
|
97
|
-
* `false` to reject the item.
|
|
98
|
-
*/
|
|
99
|
-
filterAndMap: <R>(mapFilter: (item: T, index: number) => false | R) => R[];
|
|
100
|
-
sortBy: (sortByValue: SortByValueFn<T>, props: SortByProps) => T[];
|
|
101
|
-
rejectDuplicates: (getKey: (item: T) => unknown) => T[];
|
|
102
|
-
findAndMap: <R>(predicate: (value: T) => R | false) => R | undefined;
|
|
103
|
-
};
|
|
104
85
|
/**
|
|
105
86
|
* Finds the first item in an array where the predicate returns a non-false
|
|
106
87
|
* value and returns that mapped value.
|
|
@@ -127,6 +108,25 @@ type ArrayOps<T> = {
|
|
|
127
108
|
* item matches
|
|
128
109
|
*/
|
|
129
110
|
declare function findAndMap<T, R>(array: T[], predicate: (value: T) => R | false): R | undefined;
|
|
111
|
+
type ArrayOps<T> = {
|
|
112
|
+
/**
|
|
113
|
+
* Filter and map an array
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* const items = [1, 2, 3];
|
|
117
|
+
*
|
|
118
|
+
* const enhancedItems = arrayOps(items);
|
|
119
|
+
*
|
|
120
|
+
* enhancedItems.filterAndMap((item) => (item === 2 ? false : item));
|
|
121
|
+
*
|
|
122
|
+
* @param mapFilter - A function that takes an item and returns a value or
|
|
123
|
+
* `false` to reject the item.
|
|
124
|
+
*/
|
|
125
|
+
filterAndMap: <R>(mapFilter: (item: T, index: number) => false | R) => R[];
|
|
126
|
+
sortBy: (sortByValue: SortByValueFn<T>, props: SortByProps) => T[];
|
|
127
|
+
rejectDuplicates: (getKey: (item: T) => unknown) => T[];
|
|
128
|
+
findAndMap: <R>(predicate: (value: T) => R | false) => R | undefined;
|
|
129
|
+
};
|
|
130
130
|
/**
|
|
131
131
|
* Enhance an array with extra methods
|
|
132
132
|
*
|
|
@@ -140,5 +140,30 @@ declare function findAndMap<T, R>(array: T[], predicate: (value: T) => R | false
|
|
|
140
140
|
* @param array
|
|
141
141
|
*/
|
|
142
142
|
declare function arrayOps<T>(array: T[]): ArrayOps<T>;
|
|
143
|
+
/**
|
|
144
|
+
* Inserts a separator value between each element in an array.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* intersperse([1, 2, 3], 0); // [1, 0, 2, 0, 3]
|
|
148
|
+
*
|
|
149
|
+
* @param array - The array to intersperse
|
|
150
|
+
* @param separator - The value to insert between elements
|
|
151
|
+
* @returns A new array with separator values inserted between elements
|
|
152
|
+
*/
|
|
153
|
+
declare function intersperse<T, I>(array: T[], separator: I): (T | I)[];
|
|
154
|
+
/**
|
|
155
|
+
* Creates an array by repeating a value a specified number of times,
|
|
156
|
+
* optionally with a separator between each repetition.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* repeat('x', 3); // ['x', 'x', 'x']
|
|
160
|
+
* repeat('x', 3, '-'); // ['x', '-', 'x', '-', 'x']
|
|
161
|
+
*
|
|
162
|
+
* @param value - The value to repeat
|
|
163
|
+
* @param count - Number of times to repeat the value
|
|
164
|
+
* @param separator - Optional separator to insert between repetitions
|
|
165
|
+
* @returns A new array with the repeated values
|
|
166
|
+
*/
|
|
167
|
+
declare function repeat<T>(value: T, count: number, separator?: T): T[];
|
|
143
168
|
|
|
144
|
-
export { type FilterAndMapReturn, type SortByProps, type SortByValueFn, arrayOps, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findAndMap, findBeforeIndex, getAscIndexOrder, hasDuplicates, isInArray, rejectArrayUndefinedValues, rejectDuplicates, sortBy, truncateArray };
|
|
169
|
+
export { type FilterAndMapReturn, type SortByProps, type SortByValueFn, arrayOps, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findAndMap, findBeforeIndex, getAscIndexOrder, hasDuplicates, intersperse, isInArray, rejectArrayUndefinedValues, rejectDuplicates, repeat, sortBy, truncateArray };
|
package/dist/arrayUtils.d.ts
CHANGED
|
@@ -82,25 +82,6 @@ declare function rejectArrayUndefinedValues<T extends unknown[]>(array: T): T;
|
|
|
82
82
|
declare function hasDuplicates<T>(array: T[], getKey?: (item: T) => unknown): boolean;
|
|
83
83
|
declare function rejectDuplicates<T>(array: T[], getKey?: (item: T) => unknown): T[];
|
|
84
84
|
declare function truncateArray<T>(array: T[], maxLength: number, appendIfTruncated?: T | ((truncatedCount: number) => T)): T[];
|
|
85
|
-
type ArrayOps<T> = {
|
|
86
|
-
/**
|
|
87
|
-
* Filter and map an array
|
|
88
|
-
*
|
|
89
|
-
* @example
|
|
90
|
-
* const items = [1, 2, 3];
|
|
91
|
-
*
|
|
92
|
-
* const enhancedItems = arrayOps(items);
|
|
93
|
-
*
|
|
94
|
-
* enhancedItems.filterAndMap((item) => (item === 2 ? false : item));
|
|
95
|
-
*
|
|
96
|
-
* @param mapFilter - A function that takes an item and returns a value or
|
|
97
|
-
* `false` to reject the item.
|
|
98
|
-
*/
|
|
99
|
-
filterAndMap: <R>(mapFilter: (item: T, index: number) => false | R) => R[];
|
|
100
|
-
sortBy: (sortByValue: SortByValueFn<T>, props: SortByProps) => T[];
|
|
101
|
-
rejectDuplicates: (getKey: (item: T) => unknown) => T[];
|
|
102
|
-
findAndMap: <R>(predicate: (value: T) => R | false) => R | undefined;
|
|
103
|
-
};
|
|
104
85
|
/**
|
|
105
86
|
* Finds the first item in an array where the predicate returns a non-false
|
|
106
87
|
* value and returns that mapped value.
|
|
@@ -127,6 +108,25 @@ type ArrayOps<T> = {
|
|
|
127
108
|
* item matches
|
|
128
109
|
*/
|
|
129
110
|
declare function findAndMap<T, R>(array: T[], predicate: (value: T) => R | false): R | undefined;
|
|
111
|
+
type ArrayOps<T> = {
|
|
112
|
+
/**
|
|
113
|
+
* Filter and map an array
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* const items = [1, 2, 3];
|
|
117
|
+
*
|
|
118
|
+
* const enhancedItems = arrayOps(items);
|
|
119
|
+
*
|
|
120
|
+
* enhancedItems.filterAndMap((item) => (item === 2 ? false : item));
|
|
121
|
+
*
|
|
122
|
+
* @param mapFilter - A function that takes an item and returns a value or
|
|
123
|
+
* `false` to reject the item.
|
|
124
|
+
*/
|
|
125
|
+
filterAndMap: <R>(mapFilter: (item: T, index: number) => false | R) => R[];
|
|
126
|
+
sortBy: (sortByValue: SortByValueFn<T>, props: SortByProps) => T[];
|
|
127
|
+
rejectDuplicates: (getKey: (item: T) => unknown) => T[];
|
|
128
|
+
findAndMap: <R>(predicate: (value: T) => R | false) => R | undefined;
|
|
129
|
+
};
|
|
130
130
|
/**
|
|
131
131
|
* Enhance an array with extra methods
|
|
132
132
|
*
|
|
@@ -140,5 +140,30 @@ declare function findAndMap<T, R>(array: T[], predicate: (value: T) => R | false
|
|
|
140
140
|
* @param array
|
|
141
141
|
*/
|
|
142
142
|
declare function arrayOps<T>(array: T[]): ArrayOps<T>;
|
|
143
|
+
/**
|
|
144
|
+
* Inserts a separator value between each element in an array.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* intersperse([1, 2, 3], 0); // [1, 0, 2, 0, 3]
|
|
148
|
+
*
|
|
149
|
+
* @param array - The array to intersperse
|
|
150
|
+
* @param separator - The value to insert between elements
|
|
151
|
+
* @returns A new array with separator values inserted between elements
|
|
152
|
+
*/
|
|
153
|
+
declare function intersperse<T, I>(array: T[], separator: I): (T | I)[];
|
|
154
|
+
/**
|
|
155
|
+
* Creates an array by repeating a value a specified number of times,
|
|
156
|
+
* optionally with a separator between each repetition.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* repeat('x', 3); // ['x', 'x', 'x']
|
|
160
|
+
* repeat('x', 3, '-'); // ['x', '-', 'x', '-', 'x']
|
|
161
|
+
*
|
|
162
|
+
* @param value - The value to repeat
|
|
163
|
+
* @param count - Number of times to repeat the value
|
|
164
|
+
* @param separator - Optional separator to insert between repetitions
|
|
165
|
+
* @returns A new array with the repeated values
|
|
166
|
+
*/
|
|
167
|
+
declare function repeat<T>(value: T, count: number, separator?: T): T[];
|
|
143
168
|
|
|
144
|
-
export { type FilterAndMapReturn, type SortByProps, type SortByValueFn, arrayOps, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findAndMap, findBeforeIndex, getAscIndexOrder, hasDuplicates, isInArray, rejectArrayUndefinedValues, rejectDuplicates, sortBy, truncateArray };
|
|
169
|
+
export { type FilterAndMapReturn, type SortByProps, type SortByValueFn, arrayOps, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findAndMap, findBeforeIndex, getAscIndexOrder, hasDuplicates, intersperse, isInArray, rejectArrayUndefinedValues, rejectDuplicates, repeat, sortBy, truncateArray };
|
package/dist/arrayUtils.js
CHANGED
|
@@ -8,12 +8,14 @@ import {
|
|
|
8
8
|
findBeforeIndex,
|
|
9
9
|
getAscIndexOrder,
|
|
10
10
|
hasDuplicates,
|
|
11
|
+
intersperse,
|
|
11
12
|
isInArray,
|
|
12
13
|
rejectArrayUndefinedValues,
|
|
13
14
|
rejectDuplicates,
|
|
15
|
+
repeat,
|
|
14
16
|
sortBy,
|
|
15
17
|
truncateArray
|
|
16
|
-
} from "./chunk-
|
|
18
|
+
} from "./chunk-BT3UMATU.js";
|
|
17
19
|
import "./chunk-C2SVCIWE.js";
|
|
18
20
|
import "./chunk-JF2MDHOJ.js";
|
|
19
21
|
export {
|
|
@@ -26,9 +28,11 @@ export {
|
|
|
26
28
|
findBeforeIndex,
|
|
27
29
|
getAscIndexOrder,
|
|
28
30
|
hasDuplicates,
|
|
31
|
+
intersperse,
|
|
29
32
|
isInArray,
|
|
30
33
|
rejectArrayUndefinedValues,
|
|
31
34
|
rejectDuplicates,
|
|
35
|
+
repeat,
|
|
32
36
|
sortBy,
|
|
33
37
|
truncateArray
|
|
34
38
|
};
|
package/dist/asyncQueue.js
CHANGED
package/dist/cache.js
CHANGED
|
@@ -4,9 +4,9 @@ import {
|
|
|
4
4
|
cachedGetter,
|
|
5
5
|
createCache,
|
|
6
6
|
fastCache
|
|
7
|
-
} from "./chunk-
|
|
8
|
-
import "./chunk-
|
|
9
|
-
import "./chunk-
|
|
7
|
+
} from "./chunk-DX2524CZ.js";
|
|
8
|
+
import "./chunk-6FBIEPWU.js";
|
|
9
|
+
import "./chunk-DBOWTYR4.js";
|
|
10
10
|
import "./chunk-II4R3VVX.js";
|
|
11
11
|
import "./chunk-C2SVCIWE.js";
|
|
12
12
|
import "./chunk-JF2MDHOJ.js";
|
|
@@ -133,6 +133,26 @@ function arrayOps(array) {
|
|
|
133
133
|
findAndMap: (predicate) => findAndMap(array, predicate)
|
|
134
134
|
};
|
|
135
135
|
}
|
|
136
|
+
function intersperse(array, separator) {
|
|
137
|
+
const result = [];
|
|
138
|
+
for (let i = 0; i < array.length; i++) {
|
|
139
|
+
result.push(array[i]);
|
|
140
|
+
if (i < array.length - 1) {
|
|
141
|
+
result.push(separator);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
function repeat(value, count, separator) {
|
|
147
|
+
const result = [];
|
|
148
|
+
for (let i = 0; i < count; i++) {
|
|
149
|
+
result.push(value);
|
|
150
|
+
if (separator !== void 0 && i < count - 1) {
|
|
151
|
+
result.push(separator);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
136
156
|
|
|
137
157
|
export {
|
|
138
158
|
filterAndMap,
|
|
@@ -148,5 +168,7 @@ export {
|
|
|
148
168
|
rejectDuplicates,
|
|
149
169
|
truncateArray,
|
|
150
170
|
findAndMap,
|
|
151
|
-
arrayOps
|
|
171
|
+
arrayOps,
|
|
172
|
+
intersperse,
|
|
173
|
+
repeat
|
|
152
174
|
};
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
// src/debounce.ts
|
|
2
2
|
function debounce(func, wait, options) {
|
|
3
|
+
let currentCallback = func;
|
|
4
|
+
let waitMs = wait;
|
|
5
|
+
let currentOptions = options;
|
|
3
6
|
let lastArgs;
|
|
4
7
|
let lastThis;
|
|
5
8
|
let maxWait;
|
|
@@ -10,35 +13,37 @@ function debounce(func, wait, options) {
|
|
|
10
13
|
let leading = false;
|
|
11
14
|
let maxing = false;
|
|
12
15
|
let trailing = true;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
function applyOptions() {
|
|
17
|
+
const opts = currentOptions;
|
|
18
|
+
leading = !!opts?.leading;
|
|
19
|
+
trailing = opts && "trailing" in opts ? !!opts.trailing : true;
|
|
20
|
+
maxing = !!(opts && "maxWait" in opts);
|
|
21
|
+
maxWait = maxing ? Math.max(opts?.maxWait ?? 0, waitMs) : void 0;
|
|
18
22
|
}
|
|
23
|
+
applyOptions();
|
|
19
24
|
function invokeFunc(time) {
|
|
20
25
|
const args = lastArgs;
|
|
21
26
|
const thisArg = lastThis;
|
|
22
27
|
lastArgs = lastThis = void 0;
|
|
23
28
|
lastInvokeTime = time;
|
|
24
|
-
result =
|
|
29
|
+
result = currentCallback.apply(thisArg, args);
|
|
25
30
|
return result;
|
|
26
31
|
}
|
|
27
32
|
function leadingEdge(time) {
|
|
28
33
|
lastInvokeTime = time;
|
|
29
|
-
timerId = setTimeout(timerExpired,
|
|
34
|
+
timerId = setTimeout(timerExpired, waitMs);
|
|
30
35
|
return leading ? invokeFunc(time) : result;
|
|
31
36
|
}
|
|
32
37
|
function remainingWait(time) {
|
|
33
38
|
const timeSinceLastCall = time - (lastCallTime ?? 0);
|
|
34
39
|
const timeSinceLastInvoke = time - lastInvokeTime;
|
|
35
|
-
const timeWaiting =
|
|
40
|
+
const timeWaiting = waitMs - timeSinceLastCall;
|
|
36
41
|
return maxing ? Math.min(timeWaiting, (maxWait ?? 0) - timeSinceLastInvoke) : timeWaiting;
|
|
37
42
|
}
|
|
38
43
|
function shouldInvoke(time) {
|
|
39
44
|
const timeSinceLastCall = time - (lastCallTime ?? 0);
|
|
40
45
|
const timeSinceLastInvoke = time - lastInvokeTime;
|
|
41
|
-
return lastCallTime === void 0 || timeSinceLastCall >=
|
|
46
|
+
return lastCallTime === void 0 || timeSinceLastCall >= waitMs || timeSinceLastCall < 0 || maxing && timeSinceLastInvoke >= (maxWait ?? 0);
|
|
42
47
|
}
|
|
43
48
|
function timerExpired() {
|
|
44
49
|
const time = Date.now();
|
|
@@ -65,6 +70,30 @@ function debounce(func, wait, options) {
|
|
|
65
70
|
function flush() {
|
|
66
71
|
return timerId === void 0 ? result : trailingEdge(Date.now());
|
|
67
72
|
}
|
|
73
|
+
function pending() {
|
|
74
|
+
return timerId !== void 0;
|
|
75
|
+
}
|
|
76
|
+
function updateCb(callback) {
|
|
77
|
+
currentCallback = callback;
|
|
78
|
+
}
|
|
79
|
+
function updateParams(newWait, newOptions) {
|
|
80
|
+
waitMs = newWait;
|
|
81
|
+
if (newOptions !== void 0) {
|
|
82
|
+
currentOptions = newOptions;
|
|
83
|
+
}
|
|
84
|
+
applyOptions();
|
|
85
|
+
if (timerId !== void 0) {
|
|
86
|
+
const time = Date.now();
|
|
87
|
+
const shouldRun = shouldInvoke(time);
|
|
88
|
+
clearTimeout(timerId);
|
|
89
|
+
if (shouldRun) {
|
|
90
|
+
timerId = setTimeout(timerExpired, 0);
|
|
91
|
+
} else {
|
|
92
|
+
const delay = remainingWait(time);
|
|
93
|
+
timerId = setTimeout(timerExpired, delay > 0 ? delay : 0);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
68
97
|
function debounced() {
|
|
69
98
|
const time = Date.now();
|
|
70
99
|
const isInvoking = shouldInvoke(time);
|
|
@@ -77,21 +106,24 @@ function debounce(func, wait, options) {
|
|
|
77
106
|
}
|
|
78
107
|
if (maxing) {
|
|
79
108
|
clearTimeout(timerId);
|
|
80
|
-
timerId = setTimeout(timerExpired,
|
|
109
|
+
timerId = setTimeout(timerExpired, waitMs);
|
|
81
110
|
return invokeFunc(lastCallTime);
|
|
82
111
|
}
|
|
83
112
|
}
|
|
84
113
|
if (timerId === void 0) {
|
|
85
|
-
timerId = setTimeout(timerExpired,
|
|
114
|
+
timerId = setTimeout(timerExpired, waitMs);
|
|
86
115
|
}
|
|
87
116
|
return result;
|
|
88
117
|
}
|
|
89
118
|
debounced.cancel = cancel;
|
|
90
119
|
debounced.flush = flush;
|
|
120
|
+
debounced.pending = pending;
|
|
121
|
+
debounced.updateCb = updateCb;
|
|
122
|
+
debounced.updateParams = updateParams;
|
|
91
123
|
return debounced;
|
|
92
124
|
}
|
|
93
125
|
function isDebouncedFn(fn) {
|
|
94
|
-
return typeof fn === "function" && "cancel" in fn && "flush" in fn;
|
|
126
|
+
return typeof fn === "function" && "cancel" in fn && typeof fn.cancel === "function" && "flush" in fn && typeof fn.flush === "function" && "pending" in fn && typeof fn.pending === "function" && "updateCb" in fn && typeof fn.updateCb === "function" && "updateParams" in fn && typeof fn.updateParams === "function";
|
|
95
127
|
}
|
|
96
128
|
|
|
97
129
|
export {
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// src/mathUtils.ts
|
|
2
|
+
function clampMax(value, max) {
|
|
3
|
+
return value > max ? max : value;
|
|
4
|
+
}
|
|
5
|
+
function clampMin(value, min) {
|
|
6
|
+
return value < min ? min : value;
|
|
7
|
+
}
|
|
8
|
+
function clampRange(num, v1, v2) {
|
|
9
|
+
if (v2 > v1) {
|
|
10
|
+
return clamp(num, v1, v2);
|
|
11
|
+
}
|
|
12
|
+
return clamp(num, v2, v1);
|
|
13
|
+
}
|
|
14
|
+
function clamp(num, min, max) {
|
|
15
|
+
return num > max ? max : num < min ? min : num;
|
|
16
|
+
}
|
|
17
|
+
function fixFloatingPointNumber(value) {
|
|
18
|
+
return Number(value.toPrecision(15));
|
|
19
|
+
}
|
|
20
|
+
function roundToStep(value, step, offset = 0) {
|
|
21
|
+
const inv = 1 / step;
|
|
22
|
+
const snapped = Math.round((value - offset) * inv) / inv + offset;
|
|
23
|
+
return Number(snapped.toFixed(12));
|
|
24
|
+
}
|
|
25
|
+
function floorToStep(value, step, offset = 0) {
|
|
26
|
+
const inv = 1 / step;
|
|
27
|
+
const snapped = Math.floor((value - offset) * inv) / inv + offset;
|
|
28
|
+
return Number(snapped.toFixed(12));
|
|
29
|
+
}
|
|
30
|
+
function ceilToStep(value, step, offset = 0) {
|
|
31
|
+
const inv = 1 / step;
|
|
32
|
+
const snapped = Math.ceil((value - offset) * inv) / inv + offset;
|
|
33
|
+
return Number(snapped.toFixed(12));
|
|
34
|
+
}
|
|
35
|
+
function round(num, precision) {
|
|
36
|
+
return Math.round(num * 10 ** precision) / 10 ** precision;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export {
|
|
40
|
+
clampMax,
|
|
41
|
+
clampMin,
|
|
42
|
+
clampRange,
|
|
43
|
+
clamp,
|
|
44
|
+
fixFloatingPointNumber,
|
|
45
|
+
roundToStep,
|
|
46
|
+
floorToStep,
|
|
47
|
+
ceilToStep,
|
|
48
|
+
round
|
|
49
|
+
};
|
package/dist/concurrentCalls.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
safeJsonStringify
|
|
3
|
-
} from "./chunk-VAAMRG4K.js";
|
|
4
1
|
import {
|
|
5
2
|
truncateString
|
|
6
3
|
} from "./chunk-BM4PYVOX.js";
|
|
4
|
+
import {
|
|
5
|
+
safeJsonStringify
|
|
6
|
+
} from "./chunk-VAAMRG4K.js";
|
|
7
7
|
import {
|
|
8
8
|
sleep
|
|
9
9
|
} from "./chunk-5DZT3Z5Z.js";
|
|
10
10
|
import {
|
|
11
11
|
truncateArray
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-BT3UMATU.js";
|
|
13
13
|
import {
|
|
14
14
|
invariant
|
|
15
15
|
} from "./chunk-C2SVCIWE.js";
|
|
@@ -3,8 +3,8 @@ import {
|
|
|
3
3
|
} from "./chunk-7CQPOM5I.js";
|
|
4
4
|
import {
|
|
5
5
|
durationObjToMs
|
|
6
|
-
} from "./chunk-
|
|
7
|
-
import "./chunk-
|
|
6
|
+
} from "./chunk-6FBIEPWU.js";
|
|
7
|
+
import "./chunk-DBOWTYR4.js";
|
|
8
8
|
import "./chunk-II4R3VVX.js";
|
|
9
9
|
import "./chunk-C2SVCIWE.js";
|
|
10
10
|
import "./chunk-JF2MDHOJ.js";
|
package/dist/debounce.cjs
CHANGED
|
@@ -25,6 +25,9 @@ __export(debounce_exports, {
|
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(debounce_exports);
|
|
27
27
|
function debounce(func, wait, options) {
|
|
28
|
+
let currentCallback = func;
|
|
29
|
+
let waitMs = wait;
|
|
30
|
+
let currentOptions = options;
|
|
28
31
|
let lastArgs;
|
|
29
32
|
let lastThis;
|
|
30
33
|
let maxWait;
|
|
@@ -35,35 +38,37 @@ function debounce(func, wait, options) {
|
|
|
35
38
|
let leading = false;
|
|
36
39
|
let maxing = false;
|
|
37
40
|
let trailing = true;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
function applyOptions() {
|
|
42
|
+
const opts = currentOptions;
|
|
43
|
+
leading = !!opts?.leading;
|
|
44
|
+
trailing = opts && "trailing" in opts ? !!opts.trailing : true;
|
|
45
|
+
maxing = !!(opts && "maxWait" in opts);
|
|
46
|
+
maxWait = maxing ? Math.max(opts?.maxWait ?? 0, waitMs) : void 0;
|
|
43
47
|
}
|
|
48
|
+
applyOptions();
|
|
44
49
|
function invokeFunc(time) {
|
|
45
50
|
const args = lastArgs;
|
|
46
51
|
const thisArg = lastThis;
|
|
47
52
|
lastArgs = lastThis = void 0;
|
|
48
53
|
lastInvokeTime = time;
|
|
49
|
-
result =
|
|
54
|
+
result = currentCallback.apply(thisArg, args);
|
|
50
55
|
return result;
|
|
51
56
|
}
|
|
52
57
|
function leadingEdge(time) {
|
|
53
58
|
lastInvokeTime = time;
|
|
54
|
-
timerId = setTimeout(timerExpired,
|
|
59
|
+
timerId = setTimeout(timerExpired, waitMs);
|
|
55
60
|
return leading ? invokeFunc(time) : result;
|
|
56
61
|
}
|
|
57
62
|
function remainingWait(time) {
|
|
58
63
|
const timeSinceLastCall = time - (lastCallTime ?? 0);
|
|
59
64
|
const timeSinceLastInvoke = time - lastInvokeTime;
|
|
60
|
-
const timeWaiting =
|
|
65
|
+
const timeWaiting = waitMs - timeSinceLastCall;
|
|
61
66
|
return maxing ? Math.min(timeWaiting, (maxWait ?? 0) - timeSinceLastInvoke) : timeWaiting;
|
|
62
67
|
}
|
|
63
68
|
function shouldInvoke(time) {
|
|
64
69
|
const timeSinceLastCall = time - (lastCallTime ?? 0);
|
|
65
70
|
const timeSinceLastInvoke = time - lastInvokeTime;
|
|
66
|
-
return lastCallTime === void 0 || timeSinceLastCall >=
|
|
71
|
+
return lastCallTime === void 0 || timeSinceLastCall >= waitMs || timeSinceLastCall < 0 || maxing && timeSinceLastInvoke >= (maxWait ?? 0);
|
|
67
72
|
}
|
|
68
73
|
function timerExpired() {
|
|
69
74
|
const time = Date.now();
|
|
@@ -90,6 +95,30 @@ function debounce(func, wait, options) {
|
|
|
90
95
|
function flush() {
|
|
91
96
|
return timerId === void 0 ? result : trailingEdge(Date.now());
|
|
92
97
|
}
|
|
98
|
+
function pending() {
|
|
99
|
+
return timerId !== void 0;
|
|
100
|
+
}
|
|
101
|
+
function updateCb(callback) {
|
|
102
|
+
currentCallback = callback;
|
|
103
|
+
}
|
|
104
|
+
function updateParams(newWait, newOptions) {
|
|
105
|
+
waitMs = newWait;
|
|
106
|
+
if (newOptions !== void 0) {
|
|
107
|
+
currentOptions = newOptions;
|
|
108
|
+
}
|
|
109
|
+
applyOptions();
|
|
110
|
+
if (timerId !== void 0) {
|
|
111
|
+
const time = Date.now();
|
|
112
|
+
const shouldRun = shouldInvoke(time);
|
|
113
|
+
clearTimeout(timerId);
|
|
114
|
+
if (shouldRun) {
|
|
115
|
+
timerId = setTimeout(timerExpired, 0);
|
|
116
|
+
} else {
|
|
117
|
+
const delay = remainingWait(time);
|
|
118
|
+
timerId = setTimeout(timerExpired, delay > 0 ? delay : 0);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
93
122
|
function debounced() {
|
|
94
123
|
const time = Date.now();
|
|
95
124
|
const isInvoking = shouldInvoke(time);
|
|
@@ -102,21 +131,24 @@ function debounce(func, wait, options) {
|
|
|
102
131
|
}
|
|
103
132
|
if (maxing) {
|
|
104
133
|
clearTimeout(timerId);
|
|
105
|
-
timerId = setTimeout(timerExpired,
|
|
134
|
+
timerId = setTimeout(timerExpired, waitMs);
|
|
106
135
|
return invokeFunc(lastCallTime);
|
|
107
136
|
}
|
|
108
137
|
}
|
|
109
138
|
if (timerId === void 0) {
|
|
110
|
-
timerId = setTimeout(timerExpired,
|
|
139
|
+
timerId = setTimeout(timerExpired, waitMs);
|
|
111
140
|
}
|
|
112
141
|
return result;
|
|
113
142
|
}
|
|
114
143
|
debounced.cancel = cancel;
|
|
115
144
|
debounced.flush = flush;
|
|
145
|
+
debounced.pending = pending;
|
|
146
|
+
debounced.updateCb = updateCb;
|
|
147
|
+
debounced.updateParams = updateParams;
|
|
116
148
|
return debounced;
|
|
117
149
|
}
|
|
118
150
|
function isDebouncedFn(fn) {
|
|
119
|
-
return typeof fn === "function" && "cancel" in fn && "flush" in fn;
|
|
151
|
+
return typeof fn === "function" && "cancel" in fn && typeof fn.cancel === "function" && "flush" in fn && typeof fn.flush === "function" && "pending" in fn && typeof fn.pending === "function" && "updateCb" in fn && typeof fn.updateCb === "function" && "updateParams" in fn && typeof fn.updateParams === "function";
|
|
120
152
|
}
|
|
121
153
|
// Annotate the CommonJS export names for ESM import in node:
|
|
122
154
|
0 && (module.exports = {
|
package/dist/debounce.d.cts
CHANGED
|
@@ -27,11 +27,20 @@ interface DebouncedFunc<T extends (...args: any[]) => void> {
|
|
|
27
27
|
* debounced function was never invoked.
|
|
28
28
|
*/
|
|
29
29
|
flush: () => ReturnType<T> | undefined;
|
|
30
|
+
/** Return true if the debounced function still has a scheduled run. */
|
|
31
|
+
pending: () => boolean;
|
|
32
|
+
/** Update the debounced function with a new callback. */
|
|
33
|
+
updateCb: (callback: T) => void;
|
|
34
|
+
/** Update the debounce wait and options while keeping scheduled runs. */
|
|
35
|
+
updateParams: (wait: number, options?: DebounceOptions) => void;
|
|
30
36
|
}
|
|
31
37
|
declare function debounce<T extends (...args: any[]) => void>(func: T, wait: number, options?: DebounceOptions): DebouncedFunc<T>;
|
|
32
38
|
declare function isDebouncedFn<T extends (...args: any[]) => void>(fn: T): fn is T & {
|
|
33
39
|
cancel: () => void;
|
|
34
40
|
flush: () => ReturnType<T> | undefined;
|
|
41
|
+
pending: () => boolean;
|
|
42
|
+
updateCb: (callback: T) => void;
|
|
43
|
+
updateParams: (wait: number, options?: DebounceOptions) => void;
|
|
35
44
|
};
|
|
36
45
|
|
|
37
46
|
export { type DebounceOptions, type DebouncedFunc, debounce, isDebouncedFn };
|