es-toolkit 1.19.0 → 1.20.0-dev.625
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/CHANGELOG.md +8 -0
- package/dist/_chunk/{curry-BmwJrK.js → rest-pUyjvl.js} +47 -46
- package/dist/_chunk/{zipWith-B-5AMf.js → zipWith-CDtN9Y.js} +11 -16
- package/dist/array/at.mjs +1 -1
- package/dist/array/index.js +7 -2
- package/dist/array/initial.d.mts +41 -13
- package/dist/array/initial.d.ts +41 -13
- package/dist/array/zip.mjs +8 -7
- package/dist/array/zipWith.mjs +2 -2
- package/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/compat/array/drop.d.mts +19 -0
- package/dist/compat/array/drop.d.ts +19 -0
- package/dist/compat/array/drop.mjs +9 -0
- package/dist/compat/array/findLastIndex.mjs +8 -8
- package/dist/compat/array/some.d.mts +0 -1
- package/dist/compat/array/some.d.ts +0 -1
- package/dist/compat/function/curry.d.mts +48 -0
- package/dist/compat/function/curry.d.ts +48 -0
- package/dist/compat/function/curry.mjs +65 -0
- package/dist/compat/function/debounce.d.mts +73 -0
- package/dist/compat/function/debounce.d.ts +73 -0
- package/dist/compat/function/debounce.mjs +48 -0
- package/dist/compat/function/throttle.d.mts +52 -0
- package/dist/compat/function/throttle.d.ts +52 -0
- package/dist/compat/function/throttle.mjs +11 -0
- package/dist/compat/index.d.mts +8 -6
- package/dist/compat/index.d.ts +8 -6
- package/dist/compat/index.js +180 -28
- package/dist/compat/index.mjs +8 -6
- package/dist/compat/math/clamp.d.mts +32 -0
- package/dist/compat/math/clamp.d.ts +32 -0
- package/dist/compat/math/clamp.mjs +13 -0
- package/dist/compat/predicate/isNaN.d.mts +15 -0
- package/dist/compat/predicate/isNaN.d.ts +15 -0
- package/dist/compat/predicate/isNaN.mjs +5 -0
- package/dist/compat/predicate/isNumber.d.mts +20 -0
- package/dist/compat/predicate/isNumber.d.ts +20 -0
- package/dist/compat/predicate/isNumber.mjs +10 -0
- package/dist/compat/string/camelCase.d.mts +1 -1
- package/dist/compat/string/camelCase.d.ts +1 -1
- package/dist/compat/string/kebabCase.d.mts +2 -2
- package/dist/compat/string/kebabCase.d.ts +2 -2
- package/dist/compat/string/kebabCase.mjs +0 -1
- package/dist/compat/string/lowerCase.d.mts +2 -2
- package/dist/compat/string/lowerCase.d.ts +2 -2
- package/dist/compat/string/snakeCase.d.mts +2 -2
- package/dist/compat/string/snakeCase.d.ts +2 -2
- package/dist/compat/string/startCase.d.mts +2 -2
- package/dist/compat/string/startCase.d.ts +2 -2
- package/dist/compat/string/trim.d.mts +0 -1
- package/dist/compat/string/trim.d.ts +0 -1
- package/dist/compat/string/trimEnd.d.mts +0 -1
- package/dist/compat/string/trimEnd.d.ts +0 -1
- package/dist/compat/string/trimStart.d.mts +0 -1
- package/dist/compat/string/trimStart.d.ts +0 -1
- package/dist/compat/string/upperCase.d.mts +17 -0
- package/dist/compat/string/upperCase.d.ts +17 -0
- package/dist/compat/string/upperCase.mjs +9 -0
- package/dist/function/debounce.d.mts +30 -1
- package/dist/function/debounce.d.ts +30 -1
- package/dist/function/debounce.mjs +47 -12
- package/dist/function/index.js +55 -15
- package/dist/function/throttle.d.mts +18 -1
- package/dist/function/throttle.d.ts +18 -1
- package/dist/function/throttle.mjs +18 -8
- package/dist/index.js +17 -17
- package/dist/math/inRange.d.mts +23 -0
- package/dist/math/inRange.d.ts +23 -0
- package/package.json +1 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes a specified number of elements from the beginning of an array and returns the rest.
|
|
3
|
+
*
|
|
4
|
+
* This function takes an array and a number, and returns a new array with the specified number
|
|
5
|
+
* of elements removed from the start.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The type of elements in the array.
|
|
8
|
+
* @param { T[] | null | undefined} collection - - The array from which to drop elements.
|
|
9
|
+
* @param {number} itemsCount - The number of elements to drop from the beginning of the array.
|
|
10
|
+
* @returns {T[]} A new array with the specified number of elements removed from the start.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const array = [1, 2, 3, 4, 5];
|
|
14
|
+
* const result = drop(array, 2);
|
|
15
|
+
* result will be [3, 4, 5] since the first two elements are dropped.
|
|
16
|
+
*/
|
|
17
|
+
declare function drop<T>(collection: readonly T[] | null | undefined, itemsCount: number): T[];
|
|
18
|
+
|
|
19
|
+
export { drop };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes a specified number of elements from the beginning of an array and returns the rest.
|
|
3
|
+
*
|
|
4
|
+
* This function takes an array and a number, and returns a new array with the specified number
|
|
5
|
+
* of elements removed from the start.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The type of elements in the array.
|
|
8
|
+
* @param { T[] | null | undefined} collection - - The array from which to drop elements.
|
|
9
|
+
* @param {number} itemsCount - The number of elements to drop from the beginning of the array.
|
|
10
|
+
* @returns {T[]} A new array with the specified number of elements removed from the start.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const array = [1, 2, 3, 4, 5];
|
|
14
|
+
* const result = drop(array, 2);
|
|
15
|
+
* result will be [3, 4, 5] since the first two elements are dropped.
|
|
16
|
+
*/
|
|
17
|
+
declare function drop<T>(collection: readonly T[] | null | undefined, itemsCount: number): T[];
|
|
18
|
+
|
|
19
|
+
export { drop };
|
|
@@ -2,30 +2,30 @@ import { property } from '../object/property.mjs';
|
|
|
2
2
|
import { matches } from '../predicate/matches.mjs';
|
|
3
3
|
import { matchesProperty } from '../predicate/matchesProperty.mjs';
|
|
4
4
|
|
|
5
|
-
function findLastIndex(
|
|
5
|
+
function findLastIndex(arr, doesMatch, fromIndex = arr.length - 1) {
|
|
6
6
|
if (fromIndex < 0) {
|
|
7
|
-
fromIndex = Math.max(
|
|
7
|
+
fromIndex = Math.max(arr.length + fromIndex, 0);
|
|
8
8
|
}
|
|
9
9
|
else {
|
|
10
|
-
fromIndex = Math.min(fromIndex,
|
|
10
|
+
fromIndex = Math.min(fromIndex, arr.length - 1);
|
|
11
11
|
}
|
|
12
|
-
|
|
12
|
+
arr = arr.slice(0, fromIndex + 1);
|
|
13
13
|
switch (typeof doesMatch) {
|
|
14
14
|
case 'function': {
|
|
15
|
-
return
|
|
15
|
+
return arr.findLastIndex(doesMatch);
|
|
16
16
|
}
|
|
17
17
|
case 'object': {
|
|
18
18
|
if (Array.isArray(doesMatch) && doesMatch.length === 2) {
|
|
19
19
|
const key = doesMatch[0];
|
|
20
20
|
const value = doesMatch[1];
|
|
21
|
-
return
|
|
21
|
+
return arr.findLastIndex(matchesProperty(key, value));
|
|
22
22
|
}
|
|
23
23
|
else {
|
|
24
|
-
return
|
|
24
|
+
return arr.findLastIndex(matches(doesMatch));
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
case 'string': {
|
|
28
|
-
return
|
|
28
|
+
return arr.findLastIndex(property(doesMatch));
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
}
|
|
@@ -65,7 +65,6 @@ declare function some<T>(arr: readonly T[], doesMatch: Partial<T>): boolean;
|
|
|
65
65
|
* @param {T[]} arr The array to iterate over.
|
|
66
66
|
* @param {((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | string} [predicate=identity] The function invoked per iteration.
|
|
67
67
|
* If a property name or an object is provided it will be used to create a predicate function.
|
|
68
|
-
* @param guard
|
|
69
68
|
* @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.
|
|
70
69
|
*
|
|
71
70
|
* @example
|
|
@@ -65,7 +65,6 @@ declare function some<T>(arr: readonly T[], doesMatch: Partial<T>): boolean;
|
|
|
65
65
|
* @param {T[]} arr The array to iterate over.
|
|
66
66
|
* @param {((item: T, index: number, arr: any) => unknown) | Partial<T> | [keyof T, unknown] | string} [predicate=identity] The function invoked per iteration.
|
|
67
67
|
* If a property name or an object is provided it will be used to create a predicate function.
|
|
68
|
-
* @param guard
|
|
69
68
|
* @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.
|
|
70
69
|
*
|
|
71
70
|
* @example
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a function that accepts arguments of `func` and either invokes `func` returning its result, if at least `arity` number of arguments have been provided, or returns a function that accepts the remaining `func` arguments, and so on.
|
|
3
|
+
* The arity of `func` may be specified if `func.length` is not sufficient.
|
|
4
|
+
*
|
|
5
|
+
* The `curry.placeholder` value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
|
|
6
|
+
*
|
|
7
|
+
* Note: This method doesn't set the `length` property of curried functions.
|
|
8
|
+
*
|
|
9
|
+
* @param {(...args: any[]) => any} func - The function to curry.
|
|
10
|
+
* @param {number=func.length} arity - The arity of func.
|
|
11
|
+
* @param {unknown} guard - Enables use as an iteratee for methods like `Array#map`.
|
|
12
|
+
* @returns {((...args: any[]) => any) & { placeholder: typeof curry.placeholder }} - Returns the new curried function.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const abc = function(a, b, c) {
|
|
16
|
+
* return Array.from(arguments);
|
|
17
|
+
* };
|
|
18
|
+
*
|
|
19
|
+
* let curried = curry(abc);
|
|
20
|
+
*
|
|
21
|
+
* curried(1)(2)(3);
|
|
22
|
+
* // => [1, 2, 3]
|
|
23
|
+
*
|
|
24
|
+
* curried(1, 2)(3);
|
|
25
|
+
* // => [1, 2, 3]
|
|
26
|
+
*
|
|
27
|
+
* curried(1, 2, 3);
|
|
28
|
+
* // => [1, 2, 3]
|
|
29
|
+
*
|
|
30
|
+
* // Curried with placeholders.
|
|
31
|
+
* curried(1)(curry.placeholder, 3)(2);
|
|
32
|
+
* // => [1, 2, 3]
|
|
33
|
+
*
|
|
34
|
+
* // Curried with arity.
|
|
35
|
+
* curried = curry(abc, 2);
|
|
36
|
+
*
|
|
37
|
+
* curried(1)(2);
|
|
38
|
+
* // => [1, 2]
|
|
39
|
+
*/
|
|
40
|
+
declare function curry(func: (...args: any[]) => any, arity?: number, guard?: unknown): ((...args: any[]) => any) & {
|
|
41
|
+
placeholder: typeof curry.placeholder;
|
|
42
|
+
};
|
|
43
|
+
declare namespace curry {
|
|
44
|
+
var placeholder: typeof curryPlaceholder;
|
|
45
|
+
}
|
|
46
|
+
declare const curryPlaceholder: unique symbol;
|
|
47
|
+
|
|
48
|
+
export { curry };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a function that accepts arguments of `func` and either invokes `func` returning its result, if at least `arity` number of arguments have been provided, or returns a function that accepts the remaining `func` arguments, and so on.
|
|
3
|
+
* The arity of `func` may be specified if `func.length` is not sufficient.
|
|
4
|
+
*
|
|
5
|
+
* The `curry.placeholder` value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
|
|
6
|
+
*
|
|
7
|
+
* Note: This method doesn't set the `length` property of curried functions.
|
|
8
|
+
*
|
|
9
|
+
* @param {(...args: any[]) => any} func - The function to curry.
|
|
10
|
+
* @param {number=func.length} arity - The arity of func.
|
|
11
|
+
* @param {unknown} guard - Enables use as an iteratee for methods like `Array#map`.
|
|
12
|
+
* @returns {((...args: any[]) => any) & { placeholder: typeof curry.placeholder }} - Returns the new curried function.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const abc = function(a, b, c) {
|
|
16
|
+
* return Array.from(arguments);
|
|
17
|
+
* };
|
|
18
|
+
*
|
|
19
|
+
* let curried = curry(abc);
|
|
20
|
+
*
|
|
21
|
+
* curried(1)(2)(3);
|
|
22
|
+
* // => [1, 2, 3]
|
|
23
|
+
*
|
|
24
|
+
* curried(1, 2)(3);
|
|
25
|
+
* // => [1, 2, 3]
|
|
26
|
+
*
|
|
27
|
+
* curried(1, 2, 3);
|
|
28
|
+
* // => [1, 2, 3]
|
|
29
|
+
*
|
|
30
|
+
* // Curried with placeholders.
|
|
31
|
+
* curried(1)(curry.placeholder, 3)(2);
|
|
32
|
+
* // => [1, 2, 3]
|
|
33
|
+
*
|
|
34
|
+
* // Curried with arity.
|
|
35
|
+
* curried = curry(abc, 2);
|
|
36
|
+
*
|
|
37
|
+
* curried(1)(2);
|
|
38
|
+
* // => [1, 2]
|
|
39
|
+
*/
|
|
40
|
+
declare function curry(func: (...args: any[]) => any, arity?: number, guard?: unknown): ((...args: any[]) => any) & {
|
|
41
|
+
placeholder: typeof curry.placeholder;
|
|
42
|
+
};
|
|
43
|
+
declare namespace curry {
|
|
44
|
+
var placeholder: typeof curryPlaceholder;
|
|
45
|
+
}
|
|
46
|
+
declare const curryPlaceholder: unique symbol;
|
|
47
|
+
|
|
48
|
+
export { curry };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
function curry(func, arity = func.length, guard) {
|
|
2
|
+
arity = guard ? func.length : arity;
|
|
3
|
+
arity = Number.parseInt(arity, 10);
|
|
4
|
+
if (Number.isNaN(arity) || arity < 1) {
|
|
5
|
+
arity = 0;
|
|
6
|
+
}
|
|
7
|
+
const wrapper = function (...partials) {
|
|
8
|
+
const holders = replaceHolders(partials);
|
|
9
|
+
const length = partials.length - holders.length;
|
|
10
|
+
if (length < arity) {
|
|
11
|
+
return makeCurry(func, holders, arity - length, partials);
|
|
12
|
+
}
|
|
13
|
+
if (this instanceof wrapper) {
|
|
14
|
+
return new func(...partials);
|
|
15
|
+
}
|
|
16
|
+
return func.apply(this, partials);
|
|
17
|
+
};
|
|
18
|
+
wrapper.placeholder = curryPlaceholder;
|
|
19
|
+
return wrapper;
|
|
20
|
+
}
|
|
21
|
+
function makeCurry(func, holders, arity, partials) {
|
|
22
|
+
function wrapper(...args) {
|
|
23
|
+
const holdersCount = args.filter(item => item === curry.placeholder).length;
|
|
24
|
+
const length = args.length - holdersCount;
|
|
25
|
+
args = composeArgs(args, partials, holders);
|
|
26
|
+
if (length < arity) {
|
|
27
|
+
const newHolders = replaceHolders(args);
|
|
28
|
+
return makeCurry(func, newHolders, arity - length, args);
|
|
29
|
+
}
|
|
30
|
+
if (this instanceof wrapper) {
|
|
31
|
+
return new func(...args);
|
|
32
|
+
}
|
|
33
|
+
return func.apply(this, args);
|
|
34
|
+
}
|
|
35
|
+
wrapper.placeholder = curryPlaceholder;
|
|
36
|
+
return wrapper;
|
|
37
|
+
}
|
|
38
|
+
function replaceHolders(args) {
|
|
39
|
+
const result = [];
|
|
40
|
+
for (let i = 0; i < args.length; i++) {
|
|
41
|
+
if (args[i] === curry.placeholder) {
|
|
42
|
+
result.push(i);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
function composeArgs(args, partials, holders) {
|
|
48
|
+
const result = [...partials];
|
|
49
|
+
const argsLength = args.length;
|
|
50
|
+
const holdersLength = holders.length;
|
|
51
|
+
let argsIndex = -1, leftIndex = partials.length, rangeLength = Math.max(argsLength - holdersLength, 0);
|
|
52
|
+
while (++argsIndex < holdersLength) {
|
|
53
|
+
if (argsIndex < argsLength) {
|
|
54
|
+
result[holders[argsIndex]] = args[argsIndex];
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
while (rangeLength--) {
|
|
58
|
+
result[leftIndex++] = args[argsIndex++];
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
const curryPlaceholder = Symbol('curry.placeholder');
|
|
63
|
+
curry.placeholder = curryPlaceholder;
|
|
64
|
+
|
|
65
|
+
export { curry };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
interface DebounceOptions {
|
|
2
|
+
/**
|
|
3
|
+
* An optional AbortSignal to cancel the debounced function.
|
|
4
|
+
*/
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
/**
|
|
7
|
+
* If `true`, the function will be invoked on the leading edge of the timeout.
|
|
8
|
+
* @default false
|
|
9
|
+
*/
|
|
10
|
+
leading?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* If `true`, the function will be invoked on the trailing edge of the timeout.
|
|
13
|
+
* @default true
|
|
14
|
+
*/
|
|
15
|
+
trailing?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* The maximum time `func` is allowed to be delayed before it's invoked.
|
|
18
|
+
* @default Infinity
|
|
19
|
+
*/
|
|
20
|
+
maxWait?: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
|
|
24
|
+
* have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
|
|
25
|
+
* method to cancel any pending execution.
|
|
26
|
+
*
|
|
27
|
+
* You can set the debounced function to run at the start (`leading`) or end (`trailing`) of the delay period.
|
|
28
|
+
* If `leading` is true, the function runs immediately on the first call.
|
|
29
|
+
* If `trailing` is true, the function runs after `debounceMs` milliseconds have passed since the last call.
|
|
30
|
+
* If both `leading` and `trailing` are true, the function runs at both the start and end, but it must be called at least twice within `debounceMs` milliseconds for this to happen
|
|
31
|
+
* (since one debounced function call cannot trigger the function twice).
|
|
32
|
+
*
|
|
33
|
+
* You can also set a `maxWait` time, which is the maximum time the function is allowed to be delayed before it is called.
|
|
34
|
+
*
|
|
35
|
+
* @template F - The type of function.
|
|
36
|
+
* @param {F} func - The function to debounce.
|
|
37
|
+
* @param {number} debounceMs - The number of milliseconds to delay.
|
|
38
|
+
* @param {DebounceOptions} options - The options object
|
|
39
|
+
* @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.
|
|
40
|
+
* @param {boolean} options.leading - If `true`, the function will be invoked on the leading edge of the timeout.
|
|
41
|
+
* @param {boolean} options.trailing - If `true`, the function will be invoked on the trailing edge of the timeout.
|
|
42
|
+
* @param {number} options.maxWait - The maximum time `func` is allowed to be delayed before it's invoked.
|
|
43
|
+
* @returns A new debounced function with a `cancel` method.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* const debouncedFunction = debounce(() => {
|
|
47
|
+
* console.log('Function executed');
|
|
48
|
+
* }, 1000);
|
|
49
|
+
*
|
|
50
|
+
* // Will log 'Function executed' after 1 second if not called again in that time
|
|
51
|
+
* debouncedFunction();
|
|
52
|
+
*
|
|
53
|
+
* // Will not log anything as the previous call is canceled
|
|
54
|
+
* debouncedFunction.cancel();
|
|
55
|
+
*
|
|
56
|
+
* // With AbortSignal
|
|
57
|
+
* const controller = new AbortController();
|
|
58
|
+
* const signal = controller.signal;
|
|
59
|
+
* const debouncedWithSignal = debounce(() => {
|
|
60
|
+
* console.log('Function executed');
|
|
61
|
+
* }, 1000, { signal });
|
|
62
|
+
*
|
|
63
|
+
* debouncedWithSignal();
|
|
64
|
+
*
|
|
65
|
+
* // Will cancel the debounced function call
|
|
66
|
+
* controller.abort();
|
|
67
|
+
*/
|
|
68
|
+
declare function debounce<F extends (...args: any[]) => any>(func: F, debounceMs?: number, options?: DebounceOptions): ((...args: Parameters<F>) => ReturnType<F> | undefined) & {
|
|
69
|
+
cancel: () => void;
|
|
70
|
+
flush: () => void;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export { debounce };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
interface DebounceOptions {
|
|
2
|
+
/**
|
|
3
|
+
* An optional AbortSignal to cancel the debounced function.
|
|
4
|
+
*/
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
/**
|
|
7
|
+
* If `true`, the function will be invoked on the leading edge of the timeout.
|
|
8
|
+
* @default false
|
|
9
|
+
*/
|
|
10
|
+
leading?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* If `true`, the function will be invoked on the trailing edge of the timeout.
|
|
13
|
+
* @default true
|
|
14
|
+
*/
|
|
15
|
+
trailing?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* The maximum time `func` is allowed to be delayed before it's invoked.
|
|
18
|
+
* @default Infinity
|
|
19
|
+
*/
|
|
20
|
+
maxWait?: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
|
|
24
|
+
* have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
|
|
25
|
+
* method to cancel any pending execution.
|
|
26
|
+
*
|
|
27
|
+
* You can set the debounced function to run at the start (`leading`) or end (`trailing`) of the delay period.
|
|
28
|
+
* If `leading` is true, the function runs immediately on the first call.
|
|
29
|
+
* If `trailing` is true, the function runs after `debounceMs` milliseconds have passed since the last call.
|
|
30
|
+
* If both `leading` and `trailing` are true, the function runs at both the start and end, but it must be called at least twice within `debounceMs` milliseconds for this to happen
|
|
31
|
+
* (since one debounced function call cannot trigger the function twice).
|
|
32
|
+
*
|
|
33
|
+
* You can also set a `maxWait` time, which is the maximum time the function is allowed to be delayed before it is called.
|
|
34
|
+
*
|
|
35
|
+
* @template F - The type of function.
|
|
36
|
+
* @param {F} func - The function to debounce.
|
|
37
|
+
* @param {number} debounceMs - The number of milliseconds to delay.
|
|
38
|
+
* @param {DebounceOptions} options - The options object
|
|
39
|
+
* @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.
|
|
40
|
+
* @param {boolean} options.leading - If `true`, the function will be invoked on the leading edge of the timeout.
|
|
41
|
+
* @param {boolean} options.trailing - If `true`, the function will be invoked on the trailing edge of the timeout.
|
|
42
|
+
* @param {number} options.maxWait - The maximum time `func` is allowed to be delayed before it's invoked.
|
|
43
|
+
* @returns A new debounced function with a `cancel` method.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* const debouncedFunction = debounce(() => {
|
|
47
|
+
* console.log('Function executed');
|
|
48
|
+
* }, 1000);
|
|
49
|
+
*
|
|
50
|
+
* // Will log 'Function executed' after 1 second if not called again in that time
|
|
51
|
+
* debouncedFunction();
|
|
52
|
+
*
|
|
53
|
+
* // Will not log anything as the previous call is canceled
|
|
54
|
+
* debouncedFunction.cancel();
|
|
55
|
+
*
|
|
56
|
+
* // With AbortSignal
|
|
57
|
+
* const controller = new AbortController();
|
|
58
|
+
* const signal = controller.signal;
|
|
59
|
+
* const debouncedWithSignal = debounce(() => {
|
|
60
|
+
* console.log('Function executed');
|
|
61
|
+
* }, 1000, { signal });
|
|
62
|
+
*
|
|
63
|
+
* debouncedWithSignal();
|
|
64
|
+
*
|
|
65
|
+
* // Will cancel the debounced function call
|
|
66
|
+
* controller.abort();
|
|
67
|
+
*/
|
|
68
|
+
declare function debounce<F extends (...args: any[]) => any>(func: F, debounceMs?: number, options?: DebounceOptions): ((...args: Parameters<F>) => ReturnType<F> | undefined) & {
|
|
69
|
+
cancel: () => void;
|
|
70
|
+
flush: () => void;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export { debounce };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { debounce as debounce$1 } from '../../function/debounce.mjs';
|
|
2
|
+
|
|
3
|
+
function debounce(func, debounceMs = 0, options = {}) {
|
|
4
|
+
if (typeof options !== 'object') {
|
|
5
|
+
options = {};
|
|
6
|
+
}
|
|
7
|
+
const { signal, leading = false, trailing = true, maxWait } = options;
|
|
8
|
+
const edges = Array(2);
|
|
9
|
+
if (leading) {
|
|
10
|
+
edges[0] = 'leading';
|
|
11
|
+
}
|
|
12
|
+
if (trailing) {
|
|
13
|
+
edges[1] = 'trailing';
|
|
14
|
+
}
|
|
15
|
+
let result = undefined;
|
|
16
|
+
let pendingAt = null;
|
|
17
|
+
const _debounced = debounce$1(function (...args) {
|
|
18
|
+
result = func.apply(this, args);
|
|
19
|
+
pendingAt = null;
|
|
20
|
+
}, debounceMs, { signal, edges });
|
|
21
|
+
const debounced = function (...args) {
|
|
22
|
+
if (maxWait != null) {
|
|
23
|
+
if (pendingAt === null) {
|
|
24
|
+
pendingAt = Date.now();
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
if (Date.now() - pendingAt >= maxWait) {
|
|
28
|
+
result = func.apply(this, args);
|
|
29
|
+
pendingAt = Date.now();
|
|
30
|
+
_debounced.cancel();
|
|
31
|
+
_debounced.schedule();
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
_debounced.apply(this, args);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
const flush = () => {
|
|
40
|
+
_debounced.flush();
|
|
41
|
+
return result;
|
|
42
|
+
};
|
|
43
|
+
debounced.cancel = _debounced.cancel;
|
|
44
|
+
debounced.flush = flush;
|
|
45
|
+
return debounced;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { debounce };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
interface ThrottleOptions {
|
|
2
|
+
/**
|
|
3
|
+
* An optional AbortSignal to cancel the function invocation on the trailing edge.
|
|
4
|
+
*/
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
/**
|
|
7
|
+
* If `true`, the function will be invoked on the leading edge of the timeout.
|
|
8
|
+
* @default true
|
|
9
|
+
*/
|
|
10
|
+
leading?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* If `true`, the function will be invoked on the trailing edge of the timeout.
|
|
13
|
+
* @default true
|
|
14
|
+
*/
|
|
15
|
+
trailing?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Creates a throttled function that only invokes the provided function at most once
|
|
19
|
+
* per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
|
20
|
+
* within the wait time will not trigger the execution of the original function.
|
|
21
|
+
*
|
|
22
|
+
* @template F - The type of function.
|
|
23
|
+
* @param {F} func - The function to throttle.
|
|
24
|
+
* @param {number} throttleMs - The number of milliseconds to throttle executions to.
|
|
25
|
+
* @param {ThrottleOptions} options - The options object
|
|
26
|
+
* @param {AbortSignal} options.signal - An optional AbortSignal to cancel the throttled function.
|
|
27
|
+
* @param {boolean} options.leading - If `true`, the function will be invoked on the leading edge of the timeout.
|
|
28
|
+
* @param {boolean} options.trailing - If `true`, the function will be invoked on the trailing edge of the timeout.
|
|
29
|
+
* @returns {(...args: Parameters<F>) => void} A new throttled function that accepts the same parameters as the original function.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* const throttledFunction = throttle(() => {
|
|
33
|
+
* console.log('Function executed');
|
|
34
|
+
* }, 1000);
|
|
35
|
+
*
|
|
36
|
+
* // Will log 'Function executed' immediately
|
|
37
|
+
* throttledFunction();
|
|
38
|
+
*
|
|
39
|
+
* // Will not log anything as it is within the throttle time
|
|
40
|
+
* throttledFunction();
|
|
41
|
+
*
|
|
42
|
+
* // After 1 second
|
|
43
|
+
* setTimeout(() => {
|
|
44
|
+
* throttledFunction(); // Will log 'Function executed'
|
|
45
|
+
* }, 1000);
|
|
46
|
+
*/
|
|
47
|
+
declare function throttle<F extends (...args: any[]) => any>(func: F, throttleMs?: number, options?: ThrottleOptions): ((...args: Parameters<F>) => ReturnType<F> | undefined) & {
|
|
48
|
+
cancel: () => void;
|
|
49
|
+
flush: () => void;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export { throttle };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
interface ThrottleOptions {
|
|
2
|
+
/**
|
|
3
|
+
* An optional AbortSignal to cancel the function invocation on the trailing edge.
|
|
4
|
+
*/
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
/**
|
|
7
|
+
* If `true`, the function will be invoked on the leading edge of the timeout.
|
|
8
|
+
* @default true
|
|
9
|
+
*/
|
|
10
|
+
leading?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* If `true`, the function will be invoked on the trailing edge of the timeout.
|
|
13
|
+
* @default true
|
|
14
|
+
*/
|
|
15
|
+
trailing?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Creates a throttled function that only invokes the provided function at most once
|
|
19
|
+
* per every `throttleMs` milliseconds. Subsequent calls to the throttled function
|
|
20
|
+
* within the wait time will not trigger the execution of the original function.
|
|
21
|
+
*
|
|
22
|
+
* @template F - The type of function.
|
|
23
|
+
* @param {F} func - The function to throttle.
|
|
24
|
+
* @param {number} throttleMs - The number of milliseconds to throttle executions to.
|
|
25
|
+
* @param {ThrottleOptions} options - The options object
|
|
26
|
+
* @param {AbortSignal} options.signal - An optional AbortSignal to cancel the throttled function.
|
|
27
|
+
* @param {boolean} options.leading - If `true`, the function will be invoked on the leading edge of the timeout.
|
|
28
|
+
* @param {boolean} options.trailing - If `true`, the function will be invoked on the trailing edge of the timeout.
|
|
29
|
+
* @returns {(...args: Parameters<F>) => void} A new throttled function that accepts the same parameters as the original function.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* const throttledFunction = throttle(() => {
|
|
33
|
+
* console.log('Function executed');
|
|
34
|
+
* }, 1000);
|
|
35
|
+
*
|
|
36
|
+
* // Will log 'Function executed' immediately
|
|
37
|
+
* throttledFunction();
|
|
38
|
+
*
|
|
39
|
+
* // Will not log anything as it is within the throttle time
|
|
40
|
+
* throttledFunction();
|
|
41
|
+
*
|
|
42
|
+
* // After 1 second
|
|
43
|
+
* setTimeout(() => {
|
|
44
|
+
* throttledFunction(); // Will log 'Function executed'
|
|
45
|
+
* }, 1000);
|
|
46
|
+
*/
|
|
47
|
+
declare function throttle<F extends (...args: any[]) => any>(func: F, throttleMs?: number, options?: ThrottleOptions): ((...args: Parameters<F>) => ReturnType<F> | undefined) & {
|
|
48
|
+
cancel: () => void;
|
|
49
|
+
flush: () => void;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export { throttle };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { debounce } from './debounce.mjs';
|
|
2
|
+
|
|
3
|
+
function throttle(func, throttleMs = 0, options = {}) {
|
|
4
|
+
if (typeof options !== 'object') {
|
|
5
|
+
options = {};
|
|
6
|
+
}
|
|
7
|
+
const { leading = true, trailing = true, signal } = options;
|
|
8
|
+
return debounce(func, throttleMs, { leading, trailing, signal, maxWait: throttleMs });
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { throttle };
|
package/dist/compat/index.d.mts
CHANGED
|
@@ -3,7 +3,6 @@ export { compact } from '../array/compact.mjs';
|
|
|
3
3
|
export { countBy } from '../array/countBy.mjs';
|
|
4
4
|
export { differenceBy } from '../array/differenceBy.mjs';
|
|
5
5
|
export { differenceWith } from '../array/differenceWith.mjs';
|
|
6
|
-
export { drop } from '../array/drop.mjs';
|
|
7
6
|
export { dropRight } from '../array/dropRight.mjs';
|
|
8
7
|
export { dropRightWhile } from '../array/dropRightWhile.mjs';
|
|
9
8
|
export { dropWhile } from '../array/dropWhile.mjs';
|
|
@@ -51,17 +50,13 @@ export { AbortError } from '../error/AbortError.mjs';
|
|
|
51
50
|
export { TimeoutError } from '../error/TimeoutError.mjs';
|
|
52
51
|
export { before } from '../function/before.mjs';
|
|
53
52
|
export { after } from '../function/after.mjs';
|
|
54
|
-
export { debounce } from '../function/debounce.mjs';
|
|
55
53
|
export { noop } from '../function/noop.mjs';
|
|
56
54
|
export { once } from '../function/once.mjs';
|
|
57
|
-
export { throttle } from '../function/throttle.mjs';
|
|
58
55
|
export { negate } from '../function/negate.mjs';
|
|
59
56
|
export { MemoizeCache, memoize } from '../function/memoize.mjs';
|
|
60
57
|
export { unary } from '../function/unary.mjs';
|
|
61
58
|
export { partial } from '../function/partial.mjs';
|
|
62
59
|
export { partialRight } from '../function/partialRight.mjs';
|
|
63
|
-
export { curry } from '../function/curry.mjs';
|
|
64
|
-
export { clamp } from '../math/clamp.mjs';
|
|
65
60
|
export { inRange } from '../math/inRange.mjs';
|
|
66
61
|
export { mean } from '../math/mean.mjs';
|
|
67
62
|
export { meanBy } from '../math/meanBy.mjs';
|
|
@@ -89,7 +84,6 @@ export { isPrimitive } from '../predicate/isPrimitive.mjs';
|
|
|
89
84
|
export { delay } from '../promise/delay.mjs';
|
|
90
85
|
export { withTimeout } from '../promise/withTimeout.mjs';
|
|
91
86
|
export { timeout } from '../promise/timeout.mjs';
|
|
92
|
-
export { upperCase } from '../string/upperCase.mjs';
|
|
93
87
|
export { capitalize } from '../string/capitalize.mjs';
|
|
94
88
|
export { pascalCase } from '../string/pascalCase.mjs';
|
|
95
89
|
export { upperFirst } from '../string/upperFirst.mjs';
|
|
@@ -103,6 +97,7 @@ export { castArray } from './array/castArray.mjs';
|
|
|
103
97
|
export { chunk } from './array/chunk.mjs';
|
|
104
98
|
export { concat } from './array/concat.mjs';
|
|
105
99
|
export { difference } from './array/difference.mjs';
|
|
100
|
+
export { drop } from './array/drop.mjs';
|
|
106
101
|
export { fill } from './array/fill.mjs';
|
|
107
102
|
export { find } from './array/find.mjs';
|
|
108
103
|
export { findIndex } from './array/findIndex.mjs';
|
|
@@ -124,6 +119,9 @@ export { rest } from './function/rest.mjs';
|
|
|
124
119
|
export { spread } from './function/spread.mjs';
|
|
125
120
|
export { attempt } from './function/attempt.mjs';
|
|
126
121
|
export { rearg } from './function/rearg.mjs';
|
|
122
|
+
export { curry } from './function/curry.mjs';
|
|
123
|
+
export { debounce } from './function/debounce.mjs';
|
|
124
|
+
export { throttle } from './function/throttle.mjs';
|
|
127
125
|
export { get } from './object/get.mjs';
|
|
128
126
|
export { set } from './object/set.mjs';
|
|
129
127
|
export { pick } from './object/pick.mjs';
|
|
@@ -156,11 +154,14 @@ export { conforms } from './predicate/conforms.mjs';
|
|
|
156
154
|
export { conformsTo } from './predicate/conformsTo.mjs';
|
|
157
155
|
export { isInteger } from './predicate/isInteger.mjs';
|
|
158
156
|
export { isSafeInteger } from './predicate/isSafeInteger.mjs';
|
|
157
|
+
export { isNumber } from './predicate/isNumber.mjs';
|
|
158
|
+
export { isNaN } from './predicate/isNaN.mjs';
|
|
159
159
|
export { camelCase } from './string/camelCase.mjs';
|
|
160
160
|
export { kebabCase } from './string/kebabCase.mjs';
|
|
161
161
|
export { snakeCase } from './string/snakeCase.mjs';
|
|
162
162
|
export { startCase } from './string/startCase.mjs';
|
|
163
163
|
export { lowerCase } from './string/lowerCase.mjs';
|
|
164
|
+
export { upperCase } from './string/upperCase.mjs';
|
|
164
165
|
export { startsWith } from './string/startsWith.mjs';
|
|
165
166
|
export { endsWith } from './string/endsWith.mjs';
|
|
166
167
|
export { padStart } from './string/padStart.mjs';
|
|
@@ -169,6 +170,7 @@ export { repeat } from './string/repeat.mjs';
|
|
|
169
170
|
export { trim } from './string/trim.mjs';
|
|
170
171
|
export { trimStart } from './string/trimStart.mjs';
|
|
171
172
|
export { trimEnd } from './string/trimEnd.mjs';
|
|
173
|
+
export { clamp } from './math/clamp.mjs';
|
|
172
174
|
export { max } from './math/max.mjs';
|
|
173
175
|
export { min } from './math/min.mjs';
|
|
174
176
|
export { ceil } from './math/ceil.mjs';
|