es-toolkit 1.3.1 → 1.4.0-dev.82

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.
Files changed (89) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/array/index.js +9 -2
  3. package/dist/array/index.js.map +1 -1
  4. package/dist/array/index.mjs +24 -24
  5. package/dist/array/unionBy.js +1 -1
  6. package/dist/array/unionBy.js.map +1 -1
  7. package/dist/array/unionBy.mjs +1 -1
  8. package/dist/array/uniqBy.js +6 -13
  9. package/dist/array/uniqBy.js.map +1 -1
  10. package/dist/array/uniqBy.mjs +1 -2
  11. package/dist/array/xorBy.js +1 -1
  12. package/dist/array/xorBy.js.map +1 -1
  13. package/dist/array/xorBy.mjs +2 -2
  14. package/dist/chunk-AL3GJBEI.mjs +26 -0
  15. package/dist/chunk-AL3GJBEI.mjs.map +1 -0
  16. package/dist/{chunk-EW3RT4QW.mjs → chunk-AM2EGFCU.mjs} +2 -2
  17. package/dist/chunk-FZEZH22H.mjs +1 -0
  18. package/dist/{chunk-GOXQV6DX.mjs → chunk-MKETZ2RP.mjs} +2 -3
  19. package/dist/{chunk-GOXQV6DX.mjs.map → chunk-MKETZ2RP.mjs.map} +1 -1
  20. package/dist/{chunk-JBDGK6X4.mjs → chunk-NRNKP6JF.mjs} +2 -2
  21. package/dist/{chunk-JBDGK6X4.mjs.map → chunk-NRNKP6JF.mjs.map} +1 -1
  22. package/dist/chunk-QWJDITYJ.mjs +13 -0
  23. package/dist/chunk-QWJDITYJ.mjs.map +1 -0
  24. package/dist/chunk-UGFVAAVO.mjs +12 -0
  25. package/dist/chunk-UGFVAAVO.mjs.map +1 -0
  26. package/dist/chunk-VAV4XHLO.mjs +1 -0
  27. package/dist/chunk-VAV4XHLO.mjs.map +1 -0
  28. package/dist/chunk-VP2LXY75.mjs +12 -0
  29. package/dist/chunk-VP2LXY75.mjs.map +1 -0
  30. package/dist/chunk-WPDDOUQT.mjs +16 -0
  31. package/dist/chunk-WPDDOUQT.mjs.map +1 -0
  32. package/dist/error/AbortError.d.mts +5 -0
  33. package/dist/error/AbortError.d.ts +5 -0
  34. package/dist/error/AbortError.js +36 -0
  35. package/dist/error/AbortError.js.map +1 -0
  36. package/dist/error/AbortError.mjs +8 -0
  37. package/dist/error/AbortError.mjs.map +1 -0
  38. package/dist/error/index.d.mts +1 -0
  39. package/dist/error/index.d.ts +1 -0
  40. package/dist/error/index.js +38 -0
  41. package/dist/error/index.js.map +1 -0
  42. package/dist/error/index.mjs +9 -0
  43. package/dist/error/index.mjs.map +1 -0
  44. package/dist/function/debounce.js +1 -2
  45. package/dist/function/debounce.js.map +1 -1
  46. package/dist/function/debounce.mjs +1 -1
  47. package/dist/function/index.js +1 -2
  48. package/dist/function/index.js.map +1 -1
  49. package/dist/function/index.mjs +4 -4
  50. package/dist/index.d.mts +3 -0
  51. package/dist/index.d.ts +3 -0
  52. package/dist/index.js +51 -7
  53. package/dist/index.js.map +1 -1
  54. package/dist/index.mjs +67 -54
  55. package/dist/math/index.d.mts +2 -0
  56. package/dist/math/index.d.ts +2 -0
  57. package/dist/math/index.js +17 -0
  58. package/dist/math/index.js.map +1 -1
  59. package/dist/math/index.mjs +11 -3
  60. package/dist/math/random.d.mts +16 -0
  61. package/dist/math/random.d.ts +16 -0
  62. package/dist/math/random.js +36 -0
  63. package/dist/math/random.js.map +1 -0
  64. package/dist/math/random.mjs +8 -0
  65. package/dist/math/random.mjs.map +1 -0
  66. package/dist/math/randomInt.d.mts +15 -0
  67. package/dist/math/randomInt.d.ts +15 -0
  68. package/dist/math/randomInt.js +43 -0
  69. package/dist/math/randomInt.js.map +1 -0
  70. package/dist/math/randomInt.mjs +9 -0
  71. package/dist/math/randomInt.mjs.map +1 -0
  72. package/dist/object/index.mjs +6 -6
  73. package/dist/predicate/index.mjs +3 -3
  74. package/dist/promise/delay.d.mts +18 -1
  75. package/dist/promise/delay.d.ts +18 -1
  76. package/dist/promise/delay.js +24 -3
  77. package/dist/promise/delay.js.map +1 -1
  78. package/dist/promise/delay.mjs +2 -1
  79. package/dist/promise/index.js +22 -3
  80. package/dist/promise/index.js.map +1 -1
  81. package/dist/promise/index.mjs +2 -1
  82. package/package.json +1 -1
  83. package/dist/chunk-ASFOKYTY.mjs +0 -13
  84. package/dist/chunk-ASFOKYTY.mjs.map +0 -1
  85. package/dist/chunk-IJLQVGW4.mjs +0 -1
  86. package/dist/chunk-ZBG3EYM3.mjs +0 -11
  87. package/dist/chunk-ZBG3EYM3.mjs.map +0 -1
  88. /package/dist/{chunk-EW3RT4QW.mjs.map → chunk-AM2EGFCU.mjs.map} +0 -0
  89. /package/dist/{chunk-IJLQVGW4.mjs.map → chunk-FZEZH22H.mjs.map} +0 -0
@@ -0,0 +1,12 @@
1
+ // src/math/random.ts
2
+ function random(minimum, maximum) {
3
+ if (minimum >= maximum) {
4
+ throw new Error("Invalid input: The maximum value must be greater than the minimum value.");
5
+ }
6
+ return Math.random() * (maximum - minimum) + minimum;
7
+ }
8
+
9
+ export {
10
+ random
11
+ };
12
+ //# sourceMappingURL=chunk-UGFVAAVO.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/math/random.ts"],"sourcesContent":["/**\n * Generates a random floating-point number between minimum (inclusive) and maximum (exclusive).\n * If min is greater than maximum, the values are swapped.\n *\n * @param {number} minimum - The lower bound (inclusive).\n * @param {number} maximum - The upper bound (exclusive).\n * @returns {number} A random integer between minimum (inclusive) and maximum (exclusive).\n * @throws {Error} Throws an error if `maximum` is not greater than `minimum`.\n *\n * @example\n * const result = random(0, 5); // result will be a random floating-point number between 0 (inclusive) and 5 (exclusive)\n * const result2 = random(5, 0); // This will throw an error\n */\nexport function random(minimum: number, maximum: number): number {\n if (minimum >= maximum) {\n throw new Error('Invalid input: The maximum value must be greater than the minimum value.');\n }\n\n return Math.random() * (maximum - minimum) + minimum;\n}\n"],"mappings":";AAaO,SAAS,OAAO,SAAiB,SAAyB;AAC/D,MAAI,WAAW,SAAS;AACtB,UAAM,IAAI,MAAM,0EAA0E;AAAA,EAC5F;AAEA,SAAO,KAAK,OAAO,KAAK,UAAU,WAAW;AAC/C;","names":[]}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-VAV4XHLO.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,12 @@
1
+ // src/error/AbortError.ts
2
+ var AbortError = class extends Error {
3
+ constructor(message = "The operation was aborted") {
4
+ super(message);
5
+ this.name = "AbortError";
6
+ }
7
+ };
8
+
9
+ export {
10
+ AbortError
11
+ };
12
+ //# sourceMappingURL=chunk-VP2LXY75.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/error/AbortError.ts"],"sourcesContent":["export class AbortError extends Error {\n constructor(message = 'The operation was aborted') {\n super(message);\n this.name = 'AbortError';\n }\n}\n"],"mappings":";AAAO,IAAM,aAAN,cAAyB,MAAM;AAAA,EACpC,YAAY,UAAU,6BAA6B;AACjD,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;","names":[]}
@@ -0,0 +1,16 @@
1
+ // src/array/uniqBy.ts
2
+ function uniqBy(arr, mapper) {
3
+ const map = /* @__PURE__ */ new Map();
4
+ for (const item of arr) {
5
+ const key = mapper(item);
6
+ if (!map.has(key)) {
7
+ map.set(key, item);
8
+ }
9
+ }
10
+ return Array.from(map.values());
11
+ }
12
+
13
+ export {
14
+ uniqBy
15
+ };
16
+ //# sourceMappingURL=chunk-WPDDOUQT.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/array/uniqBy.ts"],"sourcesContent":["/**\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 const map = new Map<U, T>();\n\n for (const item of arr) {\n const key = mapper(item);\n\n if (!map.has(key)) {\n map.set(key, item);\n }\n }\n\n return Array.from(map.values());\n}\n"],"mappings":";AAcO,SAAS,OAAa,KAAmB,QAA6B;AAC3E,QAAM,MAAM,oBAAI,IAAU;AAE1B,aAAW,QAAQ,KAAK;AACtB,UAAM,MAAM,OAAO,IAAI;AAEvB,QAAI,CAAC,IAAI,IAAI,GAAG,GAAG;AACjB,UAAI,IAAI,KAAK,IAAI;AAAA,IACnB;AAAA,EACF;AAEA,SAAO,MAAM,KAAK,IAAI,OAAO,CAAC;AAChC;","names":[]}
@@ -0,0 +1,5 @@
1
+ declare class AbortError extends Error {
2
+ constructor(message?: string);
3
+ }
4
+
5
+ export { AbortError };
@@ -0,0 +1,5 @@
1
+ declare class AbortError extends Error {
2
+ constructor(message?: string);
3
+ }
4
+
5
+ export { AbortError };
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/error/AbortError.ts
21
+ var AbortError_exports = {};
22
+ __export(AbortError_exports, {
23
+ AbortError: () => AbortError
24
+ });
25
+ module.exports = __toCommonJS(AbortError_exports);
26
+ var AbortError = class extends Error {
27
+ constructor(message = "The operation was aborted") {
28
+ super(message);
29
+ this.name = "AbortError";
30
+ }
31
+ };
32
+ // Annotate the CommonJS export names for ESM import in node:
33
+ 0 && (module.exports = {
34
+ AbortError
35
+ });
36
+ //# sourceMappingURL=AbortError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/error/AbortError.ts"],"sourcesContent":["export class AbortError extends Error {\n constructor(message = 'The operation was aborted') {\n super(message);\n this.name = 'AbortError';\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAO,IAAM,aAAN,cAAyB,MAAM;AAAA,EACpC,YAAY,UAAU,6BAA6B;AACjD,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;","names":[]}
@@ -0,0 +1,8 @@
1
+ import {
2
+ AbortError
3
+ } from "../chunk-VP2LXY75.mjs";
4
+ import "../chunk-2MM5EJJX.mjs";
5
+ export {
6
+ AbortError
7
+ };
8
+ //# sourceMappingURL=AbortError.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1 @@
1
+ export { AbortError } from './AbortError.mjs';
@@ -0,0 +1 @@
1
+ export { AbortError } from './AbortError.js';
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/error/index.ts
21
+ var error_exports = {};
22
+ __export(error_exports, {
23
+ AbortError: () => AbortError
24
+ });
25
+ module.exports = __toCommonJS(error_exports);
26
+
27
+ // src/error/AbortError.ts
28
+ var AbortError = class extends Error {
29
+ constructor(message = "The operation was aborted") {
30
+ super(message);
31
+ this.name = "AbortError";
32
+ }
33
+ };
34
+ // Annotate the CommonJS export names for ESM import in node:
35
+ 0 && (module.exports = {
36
+ AbortError
37
+ });
38
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/error/index.ts","../../src/error/AbortError.ts"],"sourcesContent":["export { AbortError } from './AbortError';\n","export class AbortError extends Error {\n constructor(message = 'The operation was aborted') {\n super(message);\n this.name = 'AbortError';\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,aAAN,cAAyB,MAAM;AAAA,EACpC,YAAY,UAAU,6BAA6B;AACjD,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;","names":[]}
@@ -0,0 +1,9 @@
1
+ import "../chunk-FZEZH22H.mjs";
2
+ import {
3
+ AbortError
4
+ } from "../chunk-VP2LXY75.mjs";
5
+ import "../chunk-2MM5EJJX.mjs";
6
+ export {
7
+ AbortError
8
+ };
9
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -45,9 +45,8 @@ function debounce(func, debounceMs, { signal } = {}) {
45
45
  clearTimeout(timeoutId);
46
46
  timeoutId = null;
47
47
  }
48
- signal == null ? void 0 : signal.removeEventListener("abort", onAbort);
49
48
  };
50
- signal == null ? void 0 : signal.addEventListener("abort", onAbort);
49
+ signal == null ? void 0 : signal.addEventListener("abort", onAbort, { once: true });
51
50
  return debounced;
52
51
  }
53
52
  // Annotate the CommonJS export names for ESM import in node:
@@ -1 +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":";;;;;;;;;;;;;;;;;;;;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":[]}
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\n signal?.addEventListener('abort', onAbort, { once: true });\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;AAAA,EACF;AAEA,mCAAQ,iBAAiB,SAAS,SAAS,EAAE,MAAM,KAAK;AAExD,SAAO;AACT;","names":[]}
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  debounce
3
- } from "../chunk-GOXQV6DX.mjs";
3
+ } from "../chunk-MKETZ2RP.mjs";
4
4
  import "../chunk-2MM5EJJX.mjs";
5
5
  export {
6
6
  debounce
@@ -50,9 +50,8 @@ function debounce(func, debounceMs, { signal } = {}) {
50
50
  clearTimeout(timeoutId);
51
51
  timeoutId = null;
52
52
  }
53
- signal == null ? void 0 : signal.removeEventListener("abort", onAbort);
54
53
  };
55
- signal == null ? void 0 : signal.addEventListener("abort", onAbort);
54
+ signal == null ? void 0 : signal.addEventListener("abort", onAbort, { once: true });
56
55
  return debounced;
57
56
  }
58
57
 
@@ -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","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":[]}
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\n signal?.addEventListener('abort', onAbort, { once: true });\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;AAAA,EACF;AAEA,mCAAQ,iBAAiB,SAAS,SAAS,EAAE,MAAM,KAAK;AAExD,SAAO;AACT;;;ACjEO,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":[]}
@@ -1,16 +1,16 @@
1
1
  import "../chunk-N6EM6ETS.mjs";
2
- import {
3
- throttle
4
- } from "../chunk-XLO3QZVY.mjs";
5
2
  import {
6
3
  debounce
7
- } from "../chunk-GOXQV6DX.mjs";
4
+ } from "../chunk-MKETZ2RP.mjs";
8
5
  import {
9
6
  noop
10
7
  } from "../chunk-LKXCOLNY.mjs";
11
8
  import {
12
9
  once
13
10
  } 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.d.mts CHANGED
@@ -28,11 +28,14 @@ export { xorBy } from './array/xorBy.mjs';
28
28
  export { xorWith } from './array/xorWith.mjs';
29
29
  export { zip } from './array/zip.mjs';
30
30
  export { zipWith } from './array/zipWith.mjs';
31
+ export { AbortError } from './error/AbortError.mjs';
31
32
  export { debounce } from './function/debounce.mjs';
32
33
  export { noop } from './function/noop.mjs';
33
34
  export { once } from './function/once.mjs';
34
35
  export { throttle } from './function/throttle.mjs';
35
36
  export { clamp } from './math/clamp.mjs';
37
+ export { random } from './math/random.mjs';
38
+ export { randomInt } from './math/randomInt.mjs';
36
39
  export { round } from './math/round.mjs';
37
40
  export { sum } from './math/sum.mjs';
38
41
  export { omit } from './object/omit.mjs';
package/dist/index.d.ts CHANGED
@@ -28,11 +28,14 @@ export { xorBy } from './array/xorBy.js';
28
28
  export { xorWith } from './array/xorWith.js';
29
29
  export { zip } from './array/zip.js';
30
30
  export { zipWith } from './array/zipWith.js';
31
+ export { AbortError } from './error/AbortError.js';
31
32
  export { debounce } from './function/debounce.js';
32
33
  export { noop } from './function/noop.js';
33
34
  export { once } from './function/once.js';
34
35
  export { throttle } from './function/throttle.js';
35
36
  export { clamp } from './math/clamp.js';
37
+ export { random } from './math/random.js';
38
+ export { randomInt } from './math/randomInt.js';
36
39
  export { round } from './math/round.js';
37
40
  export { sum } from './math/sum.js';
38
41
  export { omit } from './object/omit.js';
package/dist/index.js CHANGED
@@ -34,6 +34,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
34
34
  // src/index.ts
35
35
  var src_exports = {};
36
36
  __export(src_exports, {
37
+ AbortError: () => AbortError,
37
38
  chunk: () => chunk,
38
39
  clamp: () => clamp,
39
40
  debounce: () => debounce,
@@ -60,6 +61,8 @@ __export(src_exports, {
60
61
  partition: () => partition,
61
62
  pick: () => pick,
62
63
  pickBy: () => pickBy,
64
+ random: () => random,
65
+ randomInt: () => randomInt,
63
66
  round: () => round,
64
67
  sample: () => sample,
65
68
  shuffle: () => shuffle,
@@ -267,7 +270,7 @@ function unionBy(arr1, arr2, mapper) {
267
270
  map.set(key, item);
268
271
  }
269
272
  }
270
- return [...map.values()];
273
+ return Array.from(map.values());
271
274
  }
272
275
 
273
276
  // src/array/uniqWith.ts
@@ -289,7 +292,14 @@ function unionWith(arr1, arr2, areItemsEqual) {
289
292
 
290
293
  // src/array/uniqBy.ts
291
294
  function uniqBy(arr, mapper) {
292
- return uniqWith(arr, (item1, item2) => mapper(item1) === mapper(item2));
295
+ const map = /* @__PURE__ */ new Map();
296
+ for (const item of arr) {
297
+ const key = mapper(item);
298
+ if (!map.has(key)) {
299
+ map.set(key, item);
300
+ }
301
+ }
302
+ return Array.from(map.values());
293
303
  }
294
304
 
295
305
  // src/array/xor.ts
@@ -338,6 +348,14 @@ function zipWith(arr1, ...rest) {
338
348
  return result;
339
349
  }
340
350
 
351
+ // src/error/AbortError.ts
352
+ var AbortError = class extends Error {
353
+ constructor(message = "The operation was aborted") {
354
+ super(message);
355
+ this.name = "AbortError";
356
+ }
357
+ };
358
+
341
359
  // src/function/debounce.ts
342
360
  function debounce(func, debounceMs, { signal } = {}) {
343
361
  let timeoutId = null;
@@ -361,9 +379,8 @@ function debounce(func, debounceMs, { signal } = {}) {
361
379
  clearTimeout(timeoutId);
362
380
  timeoutId = null;
363
381
  }
364
- signal == null ? void 0 : signal.removeEventListener("abort", onAbort);
365
382
  };
366
- signal == null ? void 0 : signal.addEventListener("abort", onAbort);
383
+ signal == null ? void 0 : signal.addEventListener("abort", onAbort, { once: true });
367
384
  return debounced;
368
385
  }
369
386
 
@@ -407,6 +424,19 @@ function clamp(value, bound1, bound2) {
407
424
  return Math.min(Math.max(value, bound1), bound2);
408
425
  }
409
426
 
427
+ // src/math/random.ts
428
+ function random(minimum, maximum) {
429
+ if (minimum >= maximum) {
430
+ throw new Error("Invalid input: The maximum value must be greater than the minimum value.");
431
+ }
432
+ return Math.random() * (maximum - minimum) + minimum;
433
+ }
434
+
435
+ // src/math/randomInt.ts
436
+ function randomInt(minimum, maximum) {
437
+ return Math.floor(random(minimum, maximum));
438
+ }
439
+
410
440
  // src/math/round.ts
411
441
  function round(value, precision = 0) {
412
442
  const multiplier = Math.pow(10, precision);
@@ -485,13 +515,25 @@ function isUndefined(x) {
485
515
  }
486
516
 
487
517
  // src/promise/delay.ts
488
- function delay(ms) {
489
- return new Promise((resolve) => {
490
- setTimeout(resolve, ms);
518
+ function delay(ms, { signal } = {}) {
519
+ return new Promise((resolve, reject) => {
520
+ const abortError = () => {
521
+ reject(new AbortError());
522
+ };
523
+ const abortHandler = () => {
524
+ clearTimeout(timeoutId);
525
+ abortError();
526
+ };
527
+ if (signal == null ? void 0 : signal.aborted) {
528
+ return abortError();
529
+ }
530
+ const timeoutId = setTimeout(resolve, ms);
531
+ signal == null ? void 0 : signal.addEventListener("abort", abortHandler, { once: true });
491
532
  });
492
533
  }
493
534
  // Annotate the CommonJS export names for ESM import in node:
494
535
  0 && (module.exports = {
536
+ AbortError,
495
537
  chunk,
496
538
  clamp,
497
539
  debounce,
@@ -518,6 +560,8 @@ function delay(ms) {
518
560
  partition,
519
561
  pick,
520
562
  pickBy,
563
+ random,
564
+ randomInt,
521
565
  round,
522
566
  sample,
523
567
  shuffle,