es-toolkit 1.2.2 → 1.3.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/CHANGELOG.md +12 -0
- package/dist/array/index.js +4 -3
- package/dist/array/index.js.map +1 -1
- package/dist/array/index.mjs +6 -6
- package/dist/array/intersection.js +2 -1
- package/dist/array/intersection.js.map +1 -1
- package/dist/array/intersection.mjs +1 -1
- package/dist/array/unionWith.js.map +1 -1
- package/dist/array/unionWith.mjs +2 -2
- package/dist/array/uniqBy.d.mts +5 -6
- package/dist/array/uniqBy.d.ts +5 -6
- package/dist/array/uniqBy.js +2 -2
- package/dist/array/uniqBy.js.map +1 -1
- package/dist/array/uniqBy.mjs +2 -2
- package/dist/array/uniqWith.d.mts +2 -3
- package/dist/array/uniqWith.d.ts +2 -3
- package/dist/array/uniqWith.js.map +1 -1
- package/dist/array/uniqWith.mjs +1 -1
- package/dist/array/xor.js +2 -1
- package/dist/array/xor.js.map +1 -1
- package/dist/array/xor.mjs +2 -2
- package/dist/array/xorWith.js.map +1 -1
- package/dist/array/xorWith.mjs +3 -3
- package/dist/{chunk-NXPEUTDI.mjs → chunk-2PFDLYQC.mjs} +1 -1
- package/dist/chunk-2PFDLYQC.mjs.map +1 -0
- package/dist/{chunk-5P2QE2VI.mjs → chunk-3KHI4I2X.mjs} +2 -2
- package/dist/chunk-ASFOKYTY.mjs +13 -0
- package/dist/chunk-ASFOKYTY.mjs.map +1 -0
- package/dist/{chunk-LVB7BYUY.mjs → chunk-FCF2RHPW.mjs} +2 -2
- package/dist/chunk-GOXQV6DX.mjs +33 -0
- package/dist/chunk-GOXQV6DX.mjs.map +1 -0
- package/dist/{chunk-7XTQCUMW.mjs → chunk-PYLGHB3F.mjs} +3 -2
- package/dist/{chunk-7XTQCUMW.mjs.map → chunk-PYLGHB3F.mjs.map} +1 -1
- package/dist/{chunk-62EFHAYX.mjs → chunk-WXIBCOES.mjs} +2 -2
- package/dist/function/debounce.d.mts +18 -1
- package/dist/function/debounce.d.ts +18 -1
- package/dist/function/debounce.js +13 -3
- package/dist/function/debounce.js.map +1 -1
- package/dist/function/debounce.mjs +1 -1
- package/dist/function/index.js +13 -3
- package/dist/function/index.js.map +1 -1
- package/dist/function/index.mjs +4 -4
- package/dist/index.js +17 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +20 -20
- package/dist/math/index.mjs +3 -3
- package/package.json +1 -1
- package/dist/chunk-GELVVASW.mjs +0 -23
- package/dist/chunk-GELVVASW.mjs.map +0 -1
- package/dist/chunk-LCYPY5SA.mjs +0 -13
- package/dist/chunk-LCYPY5SA.mjs.map +0 -1
- package/dist/chunk-NXPEUTDI.mjs.map +0 -1
- /package/dist/{chunk-5P2QE2VI.mjs.map → chunk-3KHI4I2X.mjs.map} +0 -0
- /package/dist/{chunk-LVB7BYUY.mjs.map → chunk-FCF2RHPW.mjs.map} +0 -0
- /package/dist/{chunk-62EFHAYX.mjs.map → chunk-WXIBCOES.mjs.map} +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
uniqWith
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-2PFDLYQC.mjs";
|
|
4
4
|
|
|
5
5
|
// src/array/unionWith.ts
|
|
6
6
|
function unionWith(arr1, arr2, areItemsEqual) {
|
|
@@ -10,4 +10,4 @@ function unionWith(arr1, arr2, areItemsEqual) {
|
|
|
10
10
|
export {
|
|
11
11
|
unionWith
|
|
12
12
|
};
|
|
13
|
-
//# sourceMappingURL=chunk-
|
|
13
|
+
//# sourceMappingURL=chunk-3KHI4I2X.mjs.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {
|
|
2
|
+
uniqWith
|
|
3
|
+
} from "./chunk-2PFDLYQC.mjs";
|
|
4
|
+
|
|
5
|
+
// src/array/uniqBy.ts
|
|
6
|
+
function uniqBy(arr, mapper) {
|
|
7
|
+
return uniqWith(arr, (item1, item2) => mapper(item1) === mapper(item2));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
uniqBy
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=chunk-ASFOKYTY.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/array/uniqBy.ts"],"sourcesContent":["import { uniqWith } from './uniqWith';\n\n/**\n * Returns a new array containing only the unique elements from the original array,\n * based on the values returned by the mapper function.\n *\n * @param {T[]} arr - The array to process.\n * @param {(item: T) => U} mapper - The function used to convert the array elements.\n * @returns {T[]} A new array containing only the unique elements from the original array, based on the values returned by the mapper function.\n *\n * @example\n * ```ts\n * uniqBy([1.2, 1.5, 2.1, 3.2, 5.7, 5.3, 7.19], Math.floor);\n * // [1.2, 2.1, 3.3, 5.7, 7.19]\n * ```\n */\nexport function uniqBy<T, U>(arr: readonly T[], mapper: (item: T) => U): T[] {\n return uniqWith(arr, (item1, item2) => mapper(item1) === mapper(item2));\n}\n"],"mappings":";;;;;AAgBO,SAAS,OAAa,KAAmB,QAA6B;AAC3E,SAAO,SAAS,KAAK,CAAC,OAAO,UAAU,OAAO,KAAK,MAAM,OAAO,KAAK,CAAC;AACxE;","names":[]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
unionWith
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-3KHI4I2X.mjs";
|
|
4
4
|
import {
|
|
5
5
|
intersectionWith
|
|
6
6
|
} from "./chunk-KLJRZJOR.mjs";
|
|
@@ -18,4 +18,4 @@ function xorWith(arr1, arr2, areElementsEqual) {
|
|
|
18
18
|
export {
|
|
19
19
|
xorWith
|
|
20
20
|
};
|
|
21
|
-
//# sourceMappingURL=chunk-
|
|
21
|
+
//# sourceMappingURL=chunk-FCF2RHPW.mjs.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// src/function/debounce.ts
|
|
2
|
+
function debounce(func, debounceMs, { signal } = {}) {
|
|
3
|
+
let timeoutId = null;
|
|
4
|
+
const debounced = function(...args) {
|
|
5
|
+
if (timeoutId !== null) {
|
|
6
|
+
clearTimeout(timeoutId);
|
|
7
|
+
}
|
|
8
|
+
if (signal == null ? void 0 : signal.aborted) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
timeoutId = setTimeout(() => {
|
|
12
|
+
func(...args);
|
|
13
|
+
timeoutId = null;
|
|
14
|
+
}, debounceMs);
|
|
15
|
+
};
|
|
16
|
+
const onAbort = function() {
|
|
17
|
+
debounced.cancel();
|
|
18
|
+
};
|
|
19
|
+
debounced.cancel = function() {
|
|
20
|
+
if (timeoutId !== null) {
|
|
21
|
+
clearTimeout(timeoutId);
|
|
22
|
+
timeoutId = null;
|
|
23
|
+
}
|
|
24
|
+
signal == null ? void 0 : signal.removeEventListener("abort", onAbort);
|
|
25
|
+
};
|
|
26
|
+
signal == null ? void 0 : signal.addEventListener("abort", onAbort);
|
|
27
|
+
return debounced;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export {
|
|
31
|
+
debounce
|
|
32
|
+
};
|
|
33
|
+
//# sourceMappingURL=chunk-GOXQV6DX.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/function/debounce.ts"],"sourcesContent":["interface DebounceOptions {\n signal?: AbortSignal;\n}\n\n/**\n * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds\n * have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`\n * method to cancel any pending execution.\n *\n * @param {F} func - The function to debounce.\n * @param {number} debounceMs - The number of milliseconds to delay.\n * @param {DebounceOptions} options - The options object.\n * @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.\n * @returns {F & { cancel: () => void }} A new debounced function with a `cancel` method.\n *\n * @example\n * const debouncedFunction = debounce(() => {\n * console.log('Function executed');\n * }, 1000);\n *\n * // Will log 'Function executed' after 1 second if not called again in that time\n * debouncedFunction();\n *\n * // Will not log anything as the previous call is canceled\n * debouncedFunction.cancel();\n *\n * // With AbortSignal\n * const controller = new AbortController();\n * const signal = controller.signal;\n * const debouncedWithSignal = debounce(() => {\n * console.log('Function executed');\n * }, 1000, { signal });\n *\n * debouncedWithSignal();\n *\n * // Will cancel the debounced function call\n * controller.abort();\n */\nexport function debounce<F extends (...args: any[]) => void>(\n func: F,\n debounceMs: number,\n { signal }: DebounceOptions = {}\n): F & { cancel: () => void } {\n let timeoutId: number | NodeJS.Timeout | null = null;\n\n const debounced = function (...args: Parameters<F>) {\n if (timeoutId !== null) {\n clearTimeout(timeoutId);\n }\n\n if (signal?.aborted) {\n return;\n }\n\n timeoutId = setTimeout(() => {\n func(...args);\n timeoutId = null;\n }, debounceMs);\n } as F & { cancel: () => void };\n\n const onAbort = function () {\n debounced.cancel();\n };\n\n debounced.cancel = function () {\n if (timeoutId !== null) {\n clearTimeout(timeoutId);\n timeoutId = null;\n }\n\n signal?.removeEventListener('abort', onAbort);\n };\n\n signal?.addEventListener('abort', onAbort);\n\n return debounced;\n}\n"],"mappings":";AAsCO,SAAS,SACd,MACA,YACA,EAAE,OAAO,IAAqB,CAAC,GACH;AAC5B,MAAI,YAA4C;AAEhD,QAAM,YAAY,YAAa,MAAqB;AAClD,QAAI,cAAc,MAAM;AACtB,mBAAa,SAAS;AAAA,IACxB;AAEA,QAAI,iCAAQ,SAAS;AACnB;AAAA,IACF;AAEA,gBAAY,WAAW,MAAM;AAC3B,WAAK,GAAG,IAAI;AACZ,kBAAY;AAAA,IACd,GAAG,UAAU;AAAA,EACf;AAEA,QAAM,UAAU,WAAY;AAC1B,cAAU,OAAO;AAAA,EACnB;AAEA,YAAU,SAAS,WAAY;AAC7B,QAAI,cAAc,MAAM;AACtB,mBAAa,SAAS;AACtB,kBAAY;AAAA,IACd;AAEA,qCAAQ,oBAAoB,SAAS;AAAA,EACvC;AAEA,mCAAQ,iBAAiB,SAAS;AAElC,SAAO;AACT;","names":[]}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// src/array/intersection.ts
|
|
2
2
|
function intersection(firstArr, secondArr) {
|
|
3
|
+
const secondSet = new Set(secondArr);
|
|
3
4
|
return firstArr.filter((item) => {
|
|
4
|
-
return
|
|
5
|
+
return secondSet.has(item);
|
|
5
6
|
});
|
|
6
7
|
}
|
|
7
8
|
|
|
8
9
|
export {
|
|
9
10
|
intersection
|
|
10
11
|
};
|
|
11
|
-
//# sourceMappingURL=chunk-
|
|
12
|
+
//# sourceMappingURL=chunk-PYLGHB3F.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/array/intersection.ts"],"sourcesContent":["/**\n * Returns the intersection of two arrays.\n *\n * This function takes two arrays and returns a new array containing the elements that are\n * present in both arrays. It effectively filters out any elements from the first array that\n * are not found in the second array.\n *\n * @param {T[]} firstArr - The first array to compare.\n * @param {T[]} secondArr - The second array to compare.\n * @returns {T[]} A new array containing the elements that are present in both arrays.\n *\n * @example\n * const array1 = [1, 2, 3, 4, 5];\n * const array2 = [3, 4, 5, 6, 7];\n * const result = intersection(array1, array2);\n * // result will be [3, 4, 5] since these elements are in both arrays.\n */\nexport function intersection<T>(firstArr: readonly T[], secondArr: readonly T[]): T[] {\n return firstArr.filter(item => {\n return
|
|
1
|
+
{"version":3,"sources":["../src/array/intersection.ts"],"sourcesContent":["/**\n * Returns the intersection of two arrays.\n *\n * This function takes two arrays and returns a new array containing the elements that are\n * present in both arrays. It effectively filters out any elements from the first array that\n * are not found in the second array.\n *\n * @param {T[]} firstArr - The first array to compare.\n * @param {T[]} secondArr - The second array to compare.\n * @returns {T[]} A new array containing the elements that are present in both arrays.\n *\n * @example\n * const array1 = [1, 2, 3, 4, 5];\n * const array2 = [3, 4, 5, 6, 7];\n * const result = intersection(array1, array2);\n * // result will be [3, 4, 5] since these elements are in both arrays.\n */\nexport function intersection<T>(firstArr: readonly T[], secondArr: readonly T[]): T[] {\n const secondSet = new Set(secondArr);\n\n return firstArr.filter(item => {\n return secondSet.has(item);\n });\n}\n"],"mappings":";AAiBO,SAAS,aAAgB,UAAwB,WAA8B;AACpF,QAAM,YAAY,IAAI,IAAI,SAAS;AAEnC,SAAO,SAAS,OAAO,UAAQ;AAC7B,WAAO,UAAU,IAAI,IAAI;AAAA,EAC3B,CAAC;AACH;","names":[]}
|
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
} from "./chunk-DAJZ3JCI.mjs";
|
|
4
4
|
import {
|
|
5
5
|
intersection
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-PYLGHB3F.mjs";
|
|
7
7
|
import {
|
|
8
8
|
difference
|
|
9
9
|
} from "./chunk-M7UI5ETO.mjs";
|
|
@@ -16,4 +16,4 @@ function xor(arr1, arr2) {
|
|
|
16
16
|
export {
|
|
17
17
|
xor
|
|
18
18
|
};
|
|
19
|
-
//# sourceMappingURL=chunk-
|
|
19
|
+
//# sourceMappingURL=chunk-WXIBCOES.mjs.map
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
interface DebounceOptions {
|
|
2
|
+
signal?: AbortSignal;
|
|
3
|
+
}
|
|
1
4
|
/**
|
|
2
5
|
* Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
|
|
3
6
|
* have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
|
|
@@ -5,6 +8,8 @@
|
|
|
5
8
|
*
|
|
6
9
|
* @param {F} func - The function to debounce.
|
|
7
10
|
* @param {number} debounceMs - The number of milliseconds to delay.
|
|
11
|
+
* @param {DebounceOptions} options - The options object.
|
|
12
|
+
* @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.
|
|
8
13
|
* @returns {F & { cancel: () => void }} A new debounced function with a `cancel` method.
|
|
9
14
|
*
|
|
10
15
|
* @example
|
|
@@ -17,8 +22,20 @@
|
|
|
17
22
|
*
|
|
18
23
|
* // Will not log anything as the previous call is canceled
|
|
19
24
|
* debouncedFunction.cancel();
|
|
25
|
+
*
|
|
26
|
+
* // With AbortSignal
|
|
27
|
+
* const controller = new AbortController();
|
|
28
|
+
* const signal = controller.signal;
|
|
29
|
+
* const debouncedWithSignal = debounce(() => {
|
|
30
|
+
* console.log('Function executed');
|
|
31
|
+
* }, 1000, { signal });
|
|
32
|
+
*
|
|
33
|
+
* debouncedWithSignal();
|
|
34
|
+
*
|
|
35
|
+
* // Will cancel the debounced function call
|
|
36
|
+
* controller.abort();
|
|
20
37
|
*/
|
|
21
|
-
declare function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number): F & {
|
|
38
|
+
declare function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number, { signal }?: DebounceOptions): F & {
|
|
22
39
|
cancel: () => void;
|
|
23
40
|
};
|
|
24
41
|
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
interface DebounceOptions {
|
|
2
|
+
signal?: AbortSignal;
|
|
3
|
+
}
|
|
1
4
|
/**
|
|
2
5
|
* Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
|
|
3
6
|
* have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`
|
|
@@ -5,6 +8,8 @@
|
|
|
5
8
|
*
|
|
6
9
|
* @param {F} func - The function to debounce.
|
|
7
10
|
* @param {number} debounceMs - The number of milliseconds to delay.
|
|
11
|
+
* @param {DebounceOptions} options - The options object.
|
|
12
|
+
* @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.
|
|
8
13
|
* @returns {F & { cancel: () => void }} A new debounced function with a `cancel` method.
|
|
9
14
|
*
|
|
10
15
|
* @example
|
|
@@ -17,8 +22,20 @@
|
|
|
17
22
|
*
|
|
18
23
|
* // Will not log anything as the previous call is canceled
|
|
19
24
|
* debouncedFunction.cancel();
|
|
25
|
+
*
|
|
26
|
+
* // With AbortSignal
|
|
27
|
+
* const controller = new AbortController();
|
|
28
|
+
* const signal = controller.signal;
|
|
29
|
+
* const debouncedWithSignal = debounce(() => {
|
|
30
|
+
* console.log('Function executed');
|
|
31
|
+
* }, 1000, { signal });
|
|
32
|
+
*
|
|
33
|
+
* debouncedWithSignal();
|
|
34
|
+
*
|
|
35
|
+
* // Will cancel the debounced function call
|
|
36
|
+
* controller.abort();
|
|
20
37
|
*/
|
|
21
|
-
declare function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number): F & {
|
|
38
|
+
declare function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number, { signal }?: DebounceOptions): F & {
|
|
22
39
|
cancel: () => void;
|
|
23
40
|
};
|
|
24
41
|
|
|
@@ -23,21 +23,31 @@ __export(debounce_exports, {
|
|
|
23
23
|
debounce: () => debounce
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(debounce_exports);
|
|
26
|
-
function debounce(func, debounceMs) {
|
|
26
|
+
function debounce(func, debounceMs, { signal } = {}) {
|
|
27
27
|
let timeoutId = null;
|
|
28
28
|
const debounced = function(...args) {
|
|
29
|
-
if (timeoutId
|
|
29
|
+
if (timeoutId !== null) {
|
|
30
30
|
clearTimeout(timeoutId);
|
|
31
31
|
}
|
|
32
|
+
if (signal == null ? void 0 : signal.aborted) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
32
35
|
timeoutId = setTimeout(() => {
|
|
33
36
|
func(...args);
|
|
37
|
+
timeoutId = null;
|
|
34
38
|
}, debounceMs);
|
|
35
39
|
};
|
|
40
|
+
const onAbort = function() {
|
|
41
|
+
debounced.cancel();
|
|
42
|
+
};
|
|
36
43
|
debounced.cancel = function() {
|
|
37
|
-
if (timeoutId
|
|
44
|
+
if (timeoutId !== null) {
|
|
38
45
|
clearTimeout(timeoutId);
|
|
46
|
+
timeoutId = null;
|
|
39
47
|
}
|
|
48
|
+
signal == null ? void 0 : signal.removeEventListener("abort", onAbort);
|
|
40
49
|
};
|
|
50
|
+
signal == null ? void 0 : signal.addEventListener("abort", onAbort);
|
|
41
51
|
return debounced;
|
|
42
52
|
}
|
|
43
53
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/function/debounce.ts"],"sourcesContent":["/**\n * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds\n * have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`\n * method to cancel any pending execution.\n *\n * @param {F} func - The function to debounce.\n * @param {number} debounceMs - The number of milliseconds to delay.\n * @returns {F & { cancel: () => void }} A new debounced function with a `cancel` method.\n *\n * @example\n * const debouncedFunction = debounce(() => {\n * console.log('Function executed');\n * }, 1000);\n *\n * // Will log 'Function executed' after 1 second if not called again in that time\n * debouncedFunction();\n *\n * // Will not log anything as the previous call is canceled\n * debouncedFunction.cancel();\n */\nexport function debounce<F extends (...args: any[]) => void>(func: F
|
|
1
|
+
{"version":3,"sources":["../../src/function/debounce.ts"],"sourcesContent":["interface DebounceOptions {\n signal?: AbortSignal;\n}\n\n/**\n * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds\n * have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`\n * method to cancel any pending execution.\n *\n * @param {F} func - The function to debounce.\n * @param {number} debounceMs - The number of milliseconds to delay.\n * @param {DebounceOptions} options - The options object.\n * @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.\n * @returns {F & { cancel: () => void }} A new debounced function with a `cancel` method.\n *\n * @example\n * const debouncedFunction = debounce(() => {\n * console.log('Function executed');\n * }, 1000);\n *\n * // Will log 'Function executed' after 1 second if not called again in that time\n * debouncedFunction();\n *\n * // Will not log anything as the previous call is canceled\n * debouncedFunction.cancel();\n *\n * // With AbortSignal\n * const controller = new AbortController();\n * const signal = controller.signal;\n * const debouncedWithSignal = debounce(() => {\n * console.log('Function executed');\n * }, 1000, { signal });\n *\n * debouncedWithSignal();\n *\n * // Will cancel the debounced function call\n * controller.abort();\n */\nexport function debounce<F extends (...args: any[]) => void>(\n func: F,\n debounceMs: number,\n { signal }: DebounceOptions = {}\n): F & { cancel: () => void } {\n let timeoutId: number | NodeJS.Timeout | null = null;\n\n const debounced = function (...args: Parameters<F>) {\n if (timeoutId !== null) {\n clearTimeout(timeoutId);\n }\n\n if (signal?.aborted) {\n return;\n }\n\n timeoutId = setTimeout(() => {\n func(...args);\n timeoutId = null;\n }, debounceMs);\n } as F & { cancel: () => void };\n\n const onAbort = function () {\n debounced.cancel();\n };\n\n debounced.cancel = function () {\n if (timeoutId !== null) {\n clearTimeout(timeoutId);\n timeoutId = null;\n }\n\n signal?.removeEventListener('abort', onAbort);\n };\n\n signal?.addEventListener('abort', onAbort);\n\n return debounced;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAsCO,SAAS,SACd,MACA,YACA,EAAE,OAAO,IAAqB,CAAC,GACH;AAC5B,MAAI,YAA4C;AAEhD,QAAM,YAAY,YAAa,MAAqB;AAClD,QAAI,cAAc,MAAM;AACtB,mBAAa,SAAS;AAAA,IACxB;AAEA,QAAI,iCAAQ,SAAS;AACnB;AAAA,IACF;AAEA,gBAAY,WAAW,MAAM;AAC3B,WAAK,GAAG,IAAI;AACZ,kBAAY;AAAA,IACd,GAAG,UAAU;AAAA,EACf;AAEA,QAAM,UAAU,WAAY;AAC1B,cAAU,OAAO;AAAA,EACnB;AAEA,YAAU,SAAS,WAAY;AAC7B,QAAI,cAAc,MAAM;AACtB,mBAAa,SAAS;AACtB,kBAAY;AAAA,IACd;AAEA,qCAAQ,oBAAoB,SAAS;AAAA,EACvC;AAEA,mCAAQ,iBAAiB,SAAS;AAElC,SAAO;AACT;","names":[]}
|
package/dist/function/index.js
CHANGED
|
@@ -28,21 +28,31 @@ __export(function_exports, {
|
|
|
28
28
|
module.exports = __toCommonJS(function_exports);
|
|
29
29
|
|
|
30
30
|
// src/function/debounce.ts
|
|
31
|
-
function debounce(func, debounceMs) {
|
|
31
|
+
function debounce(func, debounceMs, { signal } = {}) {
|
|
32
32
|
let timeoutId = null;
|
|
33
33
|
const debounced = function(...args) {
|
|
34
|
-
if (timeoutId
|
|
34
|
+
if (timeoutId !== null) {
|
|
35
35
|
clearTimeout(timeoutId);
|
|
36
36
|
}
|
|
37
|
+
if (signal == null ? void 0 : signal.aborted) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
37
40
|
timeoutId = setTimeout(() => {
|
|
38
41
|
func(...args);
|
|
42
|
+
timeoutId = null;
|
|
39
43
|
}, debounceMs);
|
|
40
44
|
};
|
|
45
|
+
const onAbort = function() {
|
|
46
|
+
debounced.cancel();
|
|
47
|
+
};
|
|
41
48
|
debounced.cancel = function() {
|
|
42
|
-
if (timeoutId
|
|
49
|
+
if (timeoutId !== null) {
|
|
43
50
|
clearTimeout(timeoutId);
|
|
51
|
+
timeoutId = null;
|
|
44
52
|
}
|
|
53
|
+
signal == null ? void 0 : signal.removeEventListener("abort", onAbort);
|
|
45
54
|
};
|
|
55
|
+
signal == null ? void 0 : signal.addEventListener("abort", onAbort);
|
|
46
56
|
return debounced;
|
|
47
57
|
}
|
|
48
58
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/function/index.ts","../../src/function/debounce.ts","../../src/function/noop.ts","../../src/function/once.ts","../../src/function/throttle.ts"],"sourcesContent":["export { debounce } from './debounce';\nexport { noop } from './noop';\nexport { once } from './once';\nexport { throttle } from './throttle';\n","/**\n * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds\n * have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`\n * method to cancel any pending execution.\n *\n * @param {F} func - The function to debounce.\n * @param {number} debounceMs - The number of milliseconds to delay.\n * @returns {F & { cancel: () => void }} A new debounced function with a `cancel` method.\n *\n * @example\n * const debouncedFunction = debounce(() => {\n * console.log('Function executed');\n * }, 1000);\n *\n * // Will log 'Function executed' after 1 second if not called again in that time\n * debouncedFunction();\n *\n * // Will not log anything as the previous call is canceled\n * debouncedFunction.cancel();\n */\nexport function debounce<F extends (...args: any[]) => void>(func: F
|
|
1
|
+
{"version":3,"sources":["../../src/function/index.ts","../../src/function/debounce.ts","../../src/function/noop.ts","../../src/function/once.ts","../../src/function/throttle.ts"],"sourcesContent":["export { debounce } from './debounce';\nexport { noop } from './noop';\nexport { once } from './once';\nexport { throttle } from './throttle';\n","interface DebounceOptions {\n signal?: AbortSignal;\n}\n\n/**\n * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds\n * have elapsed since the last time the debounced function was invoked. The debounced function also has a `cancel`\n * method to cancel any pending execution.\n *\n * @param {F} func - The function to debounce.\n * @param {number} debounceMs - The number of milliseconds to delay.\n * @param {DebounceOptions} options - The options object.\n * @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.\n * @returns {F & { cancel: () => void }} A new debounced function with a `cancel` method.\n *\n * @example\n * const debouncedFunction = debounce(() => {\n * console.log('Function executed');\n * }, 1000);\n *\n * // Will log 'Function executed' after 1 second if not called again in that time\n * debouncedFunction();\n *\n * // Will not log anything as the previous call is canceled\n * debouncedFunction.cancel();\n *\n * // With AbortSignal\n * const controller = new AbortController();\n * const signal = controller.signal;\n * const debouncedWithSignal = debounce(() => {\n * console.log('Function executed');\n * }, 1000, { signal });\n *\n * debouncedWithSignal();\n *\n * // Will cancel the debounced function call\n * controller.abort();\n */\nexport function debounce<F extends (...args: any[]) => void>(\n func: F,\n debounceMs: number,\n { signal }: DebounceOptions = {}\n): F & { cancel: () => void } {\n let timeoutId: number | NodeJS.Timeout | null = null;\n\n const debounced = function (...args: Parameters<F>) {\n if (timeoutId !== null) {\n clearTimeout(timeoutId);\n }\n\n if (signal?.aborted) {\n return;\n }\n\n timeoutId = setTimeout(() => {\n func(...args);\n timeoutId = null;\n }, debounceMs);\n } as F & { cancel: () => void };\n\n const onAbort = function () {\n debounced.cancel();\n };\n\n debounced.cancel = function () {\n if (timeoutId !== null) {\n clearTimeout(timeoutId);\n timeoutId = null;\n }\n\n signal?.removeEventListener('abort', onAbort);\n };\n\n signal?.addEventListener('abort', onAbort);\n\n return debounced;\n}\n","/**\n * A no-operation function that does nothing.\n * This can be used as a placeholder or default function.\n *\n * @example\n * noop(); // Does nothing\n *\n * @returns {void} This function does not return anything.\n */\nexport function noop(): void {}\n","/**\n * Creates a function that is restricted to invoking the provided function `func` once.\n * Repeated calls to the function will return the value from the first invocation.\n *\n * @param {F} func - The function to restrict.\n * @returns {F} A new function that invokes `func` once and caches the result.\n *\n * @example\n * const initialize = once(() => {\n * console.log('Initialized!');\n * return true;\n * });\n *\n * initialize(); // Logs: 'Initialized!' and returns true\n * initialize(); // Returns true without logging\n */\nexport function once<F extends () => any>(func: F): F {\n let called = false;\n let cache: ReturnType<F> | undefined;\n\n return function () {\n if (called) {\n return cache;\n }\n\n const result = func();\n\n called = true;\n cache = result;\n\n return result;\n } as F;\n}\n","/**\n * Creates a throttled function that only invokes the provided function at most once\n * per every `throttleMs` milliseconds. Subsequent calls to the throttled function\n * within the wait time will not trigger the execution of the original function.\n *\n * @param {F} func - The function to throttle.\n * @param {number} throttleMs - The number of milliseconds to throttle executions to.\n * @returns {F} A new throttled function that accepts the same parameters as the original function.\n *\n * @example\n * const throttledFunction = throttle(() => {\n * console.log('Function executed');\n * }, 1000);\n *\n * // Will log 'Function executed' immediately\n * throttledFunction();\n *\n * // Will not log anything as it is within the throttle time\n * throttledFunction();\n *\n * // After 1 second\n * setTimeout(() => {\n * throttledFunction(); // Will log 'Function executed'\n * }, 1000);\n */\nexport function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number) {\n let lastCallTime: number | null;\n\n const throttledFunction = function (...args: Parameters<F>) {\n const now = Date.now();\n\n if (lastCallTime == null || now - lastCallTime >= throttleMs) {\n lastCallTime = now;\n func(...args);\n }\n } as F;\n\n return throttledFunction;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACsCO,SAAS,SACd,MACA,YACA,EAAE,OAAO,IAAqB,CAAC,GACH;AAC5B,MAAI,YAA4C;AAEhD,QAAM,YAAY,YAAa,MAAqB;AAClD,QAAI,cAAc,MAAM;AACtB,mBAAa,SAAS;AAAA,IACxB;AAEA,QAAI,iCAAQ,SAAS;AACnB;AAAA,IACF;AAEA,gBAAY,WAAW,MAAM;AAC3B,WAAK,GAAG,IAAI;AACZ,kBAAY;AAAA,IACd,GAAG,UAAU;AAAA,EACf;AAEA,QAAM,UAAU,WAAY;AAC1B,cAAU,OAAO;AAAA,EACnB;AAEA,YAAU,SAAS,WAAY;AAC7B,QAAI,cAAc,MAAM;AACtB,mBAAa,SAAS;AACtB,kBAAY;AAAA,IACd;AAEA,qCAAQ,oBAAoB,SAAS;AAAA,EACvC;AAEA,mCAAQ,iBAAiB,SAAS;AAElC,SAAO;AACT;;;ACnEO,SAAS,OAAa;AAAC;;;ACOvB,SAAS,KAA0B,MAAY;AACpD,MAAI,SAAS;AACb,MAAI;AAEJ,SAAO,WAAY;AACjB,QAAI,QAAQ;AACV,aAAO;AAAA,IACT;AAEA,UAAM,SAAS,KAAK;AAEpB,aAAS;AACT,YAAQ;AAER,WAAO;AAAA,EACT;AACF;;;ACPO,SAAS,SAA6C,MAAS,YAAoB;AACxF,MAAI;AAEJ,QAAM,oBAAoB,YAAa,MAAqB;AAC1D,UAAM,MAAM,KAAK,IAAI;AAErB,QAAI,gBAAgB,QAAQ,MAAM,gBAAgB,YAAY;AAC5D,qBAAe;AACf,WAAK,GAAG,IAAI;AAAA,IACd;AAAA,EACF;AAEA,SAAO;AACT;","names":[]}
|
package/dist/function/index.mjs
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import "../chunk-N6EM6ETS.mjs";
|
|
2
|
+
import {
|
|
3
|
+
throttle
|
|
4
|
+
} from "../chunk-XLO3QZVY.mjs";
|
|
2
5
|
import {
|
|
3
6
|
debounce
|
|
4
|
-
} from "../chunk-
|
|
7
|
+
} from "../chunk-GOXQV6DX.mjs";
|
|
5
8
|
import {
|
|
6
9
|
noop
|
|
7
10
|
} from "../chunk-LKXCOLNY.mjs";
|
|
8
11
|
import {
|
|
9
12
|
once
|
|
10
13
|
} from "../chunk-UFAA5WKR.mjs";
|
|
11
|
-
import {
|
|
12
|
-
throttle
|
|
13
|
-
} from "../chunk-XLO3QZVY.mjs";
|
|
14
14
|
import "../chunk-2MM5EJJX.mjs";
|
|
15
15
|
export {
|
|
16
16
|
debounce,
|
package/dist/index.js
CHANGED
|
@@ -159,8 +159,9 @@ function groupBy(arr, getKeyFromItem) {
|
|
|
159
159
|
|
|
160
160
|
// src/array/intersection.ts
|
|
161
161
|
function intersection(firstArr, secondArr) {
|
|
162
|
+
const secondSet = new Set(secondArr);
|
|
162
163
|
return firstArr.filter((item) => {
|
|
163
|
-
return
|
|
164
|
+
return secondSet.has(item);
|
|
164
165
|
});
|
|
165
166
|
}
|
|
166
167
|
|
|
@@ -284,8 +285,8 @@ function unionWith(arr1, arr2, areItemsEqual) {
|
|
|
284
285
|
}
|
|
285
286
|
|
|
286
287
|
// src/array/uniqBy.ts
|
|
287
|
-
function uniqBy(arr,
|
|
288
|
-
return uniqWith(arr, (item1, item2) =>
|
|
288
|
+
function uniqBy(arr, mapper) {
|
|
289
|
+
return uniqWith(arr, (item1, item2) => mapper(item1) === mapper(item2));
|
|
289
290
|
}
|
|
290
291
|
|
|
291
292
|
// src/array/xor.ts
|
|
@@ -335,21 +336,31 @@ function zipWith(arr1, ...rest) {
|
|
|
335
336
|
}
|
|
336
337
|
|
|
337
338
|
// src/function/debounce.ts
|
|
338
|
-
function debounce(func, debounceMs) {
|
|
339
|
+
function debounce(func, debounceMs, { signal } = {}) {
|
|
339
340
|
let timeoutId = null;
|
|
340
341
|
const debounced = function(...args) {
|
|
341
|
-
if (timeoutId
|
|
342
|
+
if (timeoutId !== null) {
|
|
342
343
|
clearTimeout(timeoutId);
|
|
343
344
|
}
|
|
345
|
+
if (signal == null ? void 0 : signal.aborted) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
344
348
|
timeoutId = setTimeout(() => {
|
|
345
349
|
func(...args);
|
|
350
|
+
timeoutId = null;
|
|
346
351
|
}, debounceMs);
|
|
347
352
|
};
|
|
353
|
+
const onAbort = function() {
|
|
354
|
+
debounced.cancel();
|
|
355
|
+
};
|
|
348
356
|
debounced.cancel = function() {
|
|
349
|
-
if (timeoutId
|
|
357
|
+
if (timeoutId !== null) {
|
|
350
358
|
clearTimeout(timeoutId);
|
|
359
|
+
timeoutId = null;
|
|
351
360
|
}
|
|
361
|
+
signal == null ? void 0 : signal.removeEventListener("abort", onAbort);
|
|
352
362
|
};
|
|
363
|
+
signal == null ? void 0 : signal.addEventListener("abort", onAbort);
|
|
353
364
|
return debounced;
|
|
354
365
|
}
|
|
355
366
|
|