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.
Files changed (70) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/_chunk/{curry-BmwJrK.js → rest-pUyjvl.js} +47 -46
  3. package/dist/_chunk/{zipWith-B-5AMf.js → zipWith-CDtN9Y.js} +11 -16
  4. package/dist/array/at.mjs +1 -1
  5. package/dist/array/index.js +7 -2
  6. package/dist/array/initial.d.mts +41 -13
  7. package/dist/array/initial.d.ts +41 -13
  8. package/dist/array/zip.mjs +8 -7
  9. package/dist/array/zipWith.mjs +2 -2
  10. package/dist/browser.global.js +1 -1
  11. package/dist/browser.global.js.map +1 -1
  12. package/dist/compat/array/drop.d.mts +19 -0
  13. package/dist/compat/array/drop.d.ts +19 -0
  14. package/dist/compat/array/drop.mjs +9 -0
  15. package/dist/compat/array/findLastIndex.mjs +8 -8
  16. package/dist/compat/array/some.d.mts +0 -1
  17. package/dist/compat/array/some.d.ts +0 -1
  18. package/dist/compat/function/curry.d.mts +48 -0
  19. package/dist/compat/function/curry.d.ts +48 -0
  20. package/dist/compat/function/curry.mjs +65 -0
  21. package/dist/compat/function/debounce.d.mts +73 -0
  22. package/dist/compat/function/debounce.d.ts +73 -0
  23. package/dist/compat/function/debounce.mjs +48 -0
  24. package/dist/compat/function/throttle.d.mts +52 -0
  25. package/dist/compat/function/throttle.d.ts +52 -0
  26. package/dist/compat/function/throttle.mjs +11 -0
  27. package/dist/compat/index.d.mts +8 -6
  28. package/dist/compat/index.d.ts +8 -6
  29. package/dist/compat/index.js +180 -28
  30. package/dist/compat/index.mjs +8 -6
  31. package/dist/compat/math/clamp.d.mts +32 -0
  32. package/dist/compat/math/clamp.d.ts +32 -0
  33. package/dist/compat/math/clamp.mjs +13 -0
  34. package/dist/compat/predicate/isNaN.d.mts +15 -0
  35. package/dist/compat/predicate/isNaN.d.ts +15 -0
  36. package/dist/compat/predicate/isNaN.mjs +5 -0
  37. package/dist/compat/predicate/isNumber.d.mts +20 -0
  38. package/dist/compat/predicate/isNumber.d.ts +20 -0
  39. package/dist/compat/predicate/isNumber.mjs +10 -0
  40. package/dist/compat/string/camelCase.d.mts +1 -1
  41. package/dist/compat/string/camelCase.d.ts +1 -1
  42. package/dist/compat/string/kebabCase.d.mts +2 -2
  43. package/dist/compat/string/kebabCase.d.ts +2 -2
  44. package/dist/compat/string/kebabCase.mjs +0 -1
  45. package/dist/compat/string/lowerCase.d.mts +2 -2
  46. package/dist/compat/string/lowerCase.d.ts +2 -2
  47. package/dist/compat/string/snakeCase.d.mts +2 -2
  48. package/dist/compat/string/snakeCase.d.ts +2 -2
  49. package/dist/compat/string/startCase.d.mts +2 -2
  50. package/dist/compat/string/startCase.d.ts +2 -2
  51. package/dist/compat/string/trim.d.mts +0 -1
  52. package/dist/compat/string/trim.d.ts +0 -1
  53. package/dist/compat/string/trimEnd.d.mts +0 -1
  54. package/dist/compat/string/trimEnd.d.ts +0 -1
  55. package/dist/compat/string/trimStart.d.mts +0 -1
  56. package/dist/compat/string/trimStart.d.ts +0 -1
  57. package/dist/compat/string/upperCase.d.mts +17 -0
  58. package/dist/compat/string/upperCase.d.ts +17 -0
  59. package/dist/compat/string/upperCase.mjs +9 -0
  60. package/dist/function/debounce.d.mts +30 -1
  61. package/dist/function/debounce.d.ts +30 -1
  62. package/dist/function/debounce.mjs +47 -12
  63. package/dist/function/index.js +55 -15
  64. package/dist/function/throttle.d.mts +18 -1
  65. package/dist/function/throttle.d.ts +18 -1
  66. package/dist/function/throttle.mjs +18 -8
  67. package/dist/index.js +17 -17
  68. package/dist/math/inRange.d.mts +23 -0
  69. package/dist/math/inRange.d.ts +23 -0
  70. package/package.json +1 -1
@@ -13,6 +13,6 @@
13
13
  * const convertedStr3 = camelCase('hyphen-text') // returns 'hyphenText'
14
14
  * const convertedStr4 = camelCase('HTTPRequest') // returns 'httpRequest'
15
15
  */
16
- declare function camelCase(str: string | object): string;
16
+ declare function camelCase(str?: string | object): string;
17
17
 
18
18
  export { camelCase };
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Kebab case is the naming convention in which each word is written in lowercase and separated by a dash (-) character.
5
5
  *
6
- * @param {string} str - The string that is to be changed to kebab case.
6
+ * @param {string | object} str - The string that is to be changed to kebab case.
7
7
  * @returns {string} - The converted string to kebab case.
8
8
  *
9
9
  * @example
@@ -12,6 +12,6 @@
12
12
  * const convertedStr3 = kebabCase('hyphen-text') // returns 'hyphen-text'
13
13
  * const convertedStr4 = kebabCase('HTTPRequest') // returns 'http-request'
14
14
  */
15
- declare function kebabCase(str: string | object): string;
15
+ declare function kebabCase(str?: string | object): string;
16
16
 
17
17
  export { kebabCase };
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Kebab case is the naming convention in which each word is written in lowercase and separated by a dash (-) character.
5
5
  *
6
- * @param {string} str - The string that is to be changed to kebab case.
6
+ * @param {string | object} str - The string that is to be changed to kebab case.
7
7
  * @returns {string} - The converted string to kebab case.
8
8
  *
9
9
  * @example
@@ -12,6 +12,6 @@
12
12
  * const convertedStr3 = kebabCase('hyphen-text') // returns 'hyphen-text'
13
13
  * const convertedStr4 = kebabCase('HTTPRequest') // returns 'http-request'
14
14
  */
15
- declare function kebabCase(str: string | object): string;
15
+ declare function kebabCase(str?: string | object): string;
16
16
 
17
17
  export { kebabCase };
@@ -1,5 +1,4 @@
1
1
  import { kebabCase as kebabCase$1 } from '../../string/kebabCase.mjs';
2
- import '../../string/deburr.mjs';
3
2
  import { normalizeForCase } from '../_internal/normalizeForCase.mjs';
4
3
 
5
4
  function kebabCase(str) {
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Lower case is the naming convention in which each word is written in lowercase and separated by an space ( ) character.
5
5
  *
6
- * @param {string} str - The string that is to be changed to lower case.
6
+ * @param {string | object} str - The string that is to be changed to lower case.
7
7
  * @returns {string} - The converted string to lower case.
8
8
  *
9
9
  * @example
@@ -12,6 +12,6 @@
12
12
  * const convertedStr3 = lowerCase('hyphen-text') // returns 'hyphen text'
13
13
  * const convertedStr4 = lowerCase('HTTPRequest') // returns 'http request'
14
14
  */
15
- declare function lowerCase(str: string | object): string;
15
+ declare function lowerCase(str?: string | object): string;
16
16
 
17
17
  export { lowerCase };
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Lower case is the naming convention in which each word is written in lowercase and separated by an space ( ) character.
5
5
  *
6
- * @param {string} str - The string that is to be changed to lower case.
6
+ * @param {string | object} str - The string that is to be changed to lower case.
7
7
  * @returns {string} - The converted string to lower case.
8
8
  *
9
9
  * @example
@@ -12,6 +12,6 @@
12
12
  * const convertedStr3 = lowerCase('hyphen-text') // returns 'hyphen text'
13
13
  * const convertedStr4 = lowerCase('HTTPRequest') // returns 'http request'
14
14
  */
15
- declare function lowerCase(str: string | object): string;
15
+ declare function lowerCase(str?: string | object): string;
16
16
 
17
17
  export { lowerCase };
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Snake case is the naming convention in which each word is written in lowercase and separated by an underscore (_) character.
5
5
  *
6
- * @param {string} str - The string that is to be changed to snake case.
6
+ * @param {string | object} str - The string that is to be changed to snake case.
7
7
  * @returns {string} - The converted string to snake case.
8
8
  *
9
9
  * @example
@@ -12,6 +12,6 @@
12
12
  * const convertedStr3 = snakeCase('hyphen-text') // returns 'hyphen_text'
13
13
  * const convertedStr4 = snakeCase('HTTPRequest') // returns 'http_request'
14
14
  */
15
- declare function snakeCase(str: string | object): string;
15
+ declare function snakeCase(str?: string | object): string;
16
16
 
17
17
  export { snakeCase };
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Snake case is the naming convention in which each word is written in lowercase and separated by an underscore (_) character.
5
5
  *
6
- * @param {string} str - The string that is to be changed to snake case.
6
+ * @param {string | object} str - The string that is to be changed to snake case.
7
7
  * @returns {string} - The converted string to snake case.
8
8
  *
9
9
  * @example
@@ -12,6 +12,6 @@
12
12
  * const convertedStr3 = snakeCase('hyphen-text') // returns 'hyphen_text'
13
13
  * const convertedStr4 = snakeCase('HTTPRequest') // returns 'http_request'
14
14
  */
15
- declare function snakeCase(str: string | object): string;
15
+ declare function snakeCase(str?: string | object): string;
16
16
 
17
17
  export { snakeCase };
@@ -2,7 +2,7 @@
2
2
  * Converts the first character of each word in a string to uppercase and the remaining characters to lowercase.
3
3
  *
4
4
  * Start case is the naming convention in which each word is written with an initial capital letter.
5
- * @param {string} str - The string to convert.
5
+ * @param {string | object} str - The string to convert.
6
6
  * @returns {string} The converted string.
7
7
  *
8
8
  * @example
@@ -11,6 +11,6 @@
11
11
  * const result3 = startCase('hello-world'); // result will be 'Hello World'
12
12
  * const result4 = startCase('hello_world'); // result will be 'Hello World'
13
13
  */
14
- declare function startCase(str: string | object): string;
14
+ declare function startCase(str?: string | object): string;
15
15
 
16
16
  export { startCase };
@@ -2,7 +2,7 @@
2
2
  * Converts the first character of each word in a string to uppercase and the remaining characters to lowercase.
3
3
  *
4
4
  * Start case is the naming convention in which each word is written with an initial capital letter.
5
- * @param {string} str - The string to convert.
5
+ * @param {string | object} str - The string to convert.
6
6
  * @returns {string} The converted string.
7
7
  *
8
8
  * @example
@@ -11,6 +11,6 @@
11
11
  * const result3 = startCase('hello-world'); // result will be 'Hello World'
12
12
  * const result4 = startCase('hello_world'); // result will be 'Hello World'
13
13
  */
14
- declare function startCase(str: string | object): string;
14
+ declare function startCase(str?: string | object): string;
15
15
 
16
16
  export { startCase };
@@ -3,7 +3,6 @@
3
3
  *
4
4
  * @param {string} str - The string from which leading and trailing characters will be trimmed.
5
5
  * @param {string | string[]} chars - The character(s) to remove from the end of the string. Defaults to `" "`.
6
- * @param guard
7
6
  * @returns {string} - The resulting string after the specified leading and trailing characters have been removed.
8
7
  *
9
8
  * @example
@@ -3,7 +3,6 @@
3
3
  *
4
4
  * @param {string} str - The string from which leading and trailing characters will be trimmed.
5
5
  * @param {string | string[]} chars - The character(s) to remove from the end of the string. Defaults to `" "`.
6
- * @param guard
7
6
  * @returns {string} - The resulting string after the specified leading and trailing characters have been removed.
8
7
  *
9
8
  * @example
@@ -3,7 +3,6 @@
3
3
  *
4
4
  * @param {string} str - The string from which trailing characters will be trimmed.
5
5
  * @param {string | string[]} chars - The character(s) to remove from the end of the string. Defaults to `" "`.
6
- * @param guard
7
6
  * @returns {string} - The resulting string after the specified trailing character has been removed.
8
7
  *
9
8
  * @example
@@ -3,7 +3,6 @@
3
3
  *
4
4
  * @param {string} str - The string from which trailing characters will be trimmed.
5
5
  * @param {string | string[]} chars - The character(s) to remove from the end of the string. Defaults to `" "`.
6
- * @param guard
7
6
  * @returns {string} - The resulting string after the specified trailing character has been removed.
8
7
  *
9
8
  * @example
@@ -3,7 +3,6 @@
3
3
  *
4
4
  * @param {string} str - The string from which leading characters will be trimmed.
5
5
  * @param {string | string[]} chars - The character(s) to remove from the end of the string. Defaults to `" "`.
6
- * @param guard
7
6
  * @returns {string} - The resulting string after the specified leading character has been removed.
8
7
  *
9
8
  * @example
@@ -3,7 +3,6 @@
3
3
  *
4
4
  * @param {string} str - The string from which leading characters will be trimmed.
5
5
  * @param {string | string[]} chars - The character(s) to remove from the end of the string. Defaults to `" "`.
6
- * @param guard
7
6
  * @returns {string} - The resulting string after the specified leading character has been removed.
8
7
  *
9
8
  * @example
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Converts a string to upper case.
3
+ *
4
+ * Upper case is the naming convention in which each word is written in uppercase and separated by an space ( ) character.
5
+ *
6
+ * @param {string | object} str - The string that is to be changed to upper case.
7
+ * @returns {string} - The converted string to upper case.
8
+ *
9
+ * @example
10
+ * const convertedStr1 = upperCase('camelCase') // returns 'CAMEL CASE'
11
+ * const convertedStr2 = upperCase('some whitespace') // returns 'SOME WHITESPACE'
12
+ * const convertedStr3 = upperCase('hyphen-text') // returns 'HYPHEN TEXT'
13
+ * const convertedStr4 = upperCase('HTTPRequest') // returns 'HTTP REQUEST'
14
+ */
15
+ declare function upperCase(str?: string | object): string;
16
+
17
+ export { upperCase };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Converts a string to upper case.
3
+ *
4
+ * Upper case is the naming convention in which each word is written in uppercase and separated by an space ( ) character.
5
+ *
6
+ * @param {string | object} str - The string that is to be changed to upper case.
7
+ * @returns {string} - The converted string to upper case.
8
+ *
9
+ * @example
10
+ * const convertedStr1 = upperCase('camelCase') // returns 'CAMEL CASE'
11
+ * const convertedStr2 = upperCase('some whitespace') // returns 'SOME WHITESPACE'
12
+ * const convertedStr3 = upperCase('hyphen-text') // returns 'HYPHEN TEXT'
13
+ * const convertedStr4 = upperCase('HTTPRequest') // returns 'HTTP REQUEST'
14
+ */
15
+ declare function upperCase(str?: string | object): string;
16
+
17
+ export { upperCase };
@@ -0,0 +1,9 @@
1
+ import { upperCase as upperCase$1 } from '../../string/upperCase.mjs';
2
+ import '../../string/deburr.mjs';
3
+ import { normalizeForCase } from '../_internal/normalizeForCase.mjs';
4
+
5
+ function upperCase(str) {
6
+ return upperCase$1(normalizeForCase(str));
7
+ }
8
+
9
+ export { upperCase };
@@ -1,5 +1,16 @@
1
1
  interface DebounceOptions {
2
+ /**
3
+ * An optional AbortSignal to cancel the debounced function.
4
+ */
2
5
  signal?: AbortSignal;
6
+ /**
7
+ * An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
8
+ * If `edges` includes "leading", the function will be invoked at the start of the delay period.
9
+ * If `edges` includes "trailing", the function will be invoked at the end of the delay period.
10
+ * If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period.
11
+ * @default ["trailing"]
12
+ */
13
+ edges?: Array<'leading' | 'trailing'>;
3
14
  }
4
15
  /**
5
16
  * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
@@ -36,8 +47,26 @@ interface DebounceOptions {
36
47
  * // Will cancel the debounced function call
37
48
  * controller.abort();
38
49
  */
39
- declare function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number, { signal }?: DebounceOptions): ((...args: Parameters<F>) => void) & {
50
+ declare function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number, { signal, edges }?: DebounceOptions): ((...args: Parameters<F>) => void) & {
51
+ /**
52
+ * Schedules the execution of the debounced function after the specified debounce delay.
53
+ * This method resets any existing timer, ensuring that the function is only invoked
54
+ * after the delay has elapsed since the last call to the debounced function.
55
+ * It is typically called internally whenever the debounced function is invoked.
56
+ *
57
+ * @returns {void}
58
+ */
59
+ schedule: () => void;
60
+ /**
61
+ * Cancels any pending execution of the debounced function.
62
+ * This method clears the active timer and resets any stored context or arguments.
63
+ */
40
64
  cancel: () => void;
65
+ /**
66
+ * Immediately invokes the debounced function if there is a pending execution.
67
+ * This method also cancels the current timer, ensuring that the function executes right away.
68
+ */
69
+ flush: () => void;
41
70
  };
42
71
 
43
72
  export { debounce };
@@ -1,5 +1,16 @@
1
1
  interface DebounceOptions {
2
+ /**
3
+ * An optional AbortSignal to cancel the debounced function.
4
+ */
2
5
  signal?: AbortSignal;
6
+ /**
7
+ * An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
8
+ * If `edges` includes "leading", the function will be invoked at the start of the delay period.
9
+ * If `edges` includes "trailing", the function will be invoked at the end of the delay period.
10
+ * If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period.
11
+ * @default ["trailing"]
12
+ */
13
+ edges?: Array<'leading' | 'trailing'>;
3
14
  }
4
15
  /**
5
16
  * Creates a debounced function that delays invoking the provided function until after `debounceMs` milliseconds
@@ -36,8 +47,26 @@ interface DebounceOptions {
36
47
  * // Will cancel the debounced function call
37
48
  * controller.abort();
38
49
  */
39
- declare function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number, { signal }?: DebounceOptions): ((...args: Parameters<F>) => void) & {
50
+ declare function debounce<F extends (...args: any[]) => void>(func: F, debounceMs: number, { signal, edges }?: DebounceOptions): ((...args: Parameters<F>) => void) & {
51
+ /**
52
+ * Schedules the execution of the debounced function after the specified debounce delay.
53
+ * This method resets any existing timer, ensuring that the function is only invoked
54
+ * after the delay has elapsed since the last call to the debounced function.
55
+ * It is typically called internally whenever the debounced function is invoked.
56
+ *
57
+ * @returns {void}
58
+ */
59
+ schedule: () => void;
60
+ /**
61
+ * Cancels any pending execution of the debounced function.
62
+ * This method clears the active timer and resets any stored context or arguments.
63
+ */
40
64
  cancel: () => void;
65
+ /**
66
+ * Immediately invokes the debounced function if there is a pending execution.
67
+ * This method also cancels the current timer, ensuring that the function executes right away.
68
+ */
69
+ flush: () => void;
41
70
  };
42
71
 
43
72
  export { debounce };
@@ -1,27 +1,62 @@
1
- function debounce(func, debounceMs, { signal } = {}) {
1
+ function debounce(func, debounceMs, { signal, edges } = {}) {
2
+ let pendingThis = undefined;
3
+ let pendingArgs = null;
4
+ const leading = edges != null && edges.includes('leading');
5
+ const trailing = edges == null || edges.includes('trailing');
6
+ const invoke = () => {
7
+ if (pendingArgs !== null) {
8
+ func.apply(pendingThis, pendingArgs);
9
+ pendingThis = undefined;
10
+ pendingArgs = null;
11
+ }
12
+ };
13
+ const onTimerEnd = () => {
14
+ if (trailing) {
15
+ invoke();
16
+ }
17
+ cancel();
18
+ };
2
19
  let timeoutId = null;
3
- const debounced = function (...args) {
4
- if (timeoutId !== null) {
20
+ const schedule = () => {
21
+ if (timeoutId != null) {
5
22
  clearTimeout(timeoutId);
6
23
  }
7
- if (signal?.aborted) {
8
- return;
9
- }
10
24
  timeoutId = setTimeout(() => {
11
- func(...args);
12
25
  timeoutId = null;
26
+ onTimerEnd();
13
27
  }, debounceMs);
14
28
  };
15
- const onAbort = function () {
16
- debounced.cancel();
17
- };
18
- debounced.cancel = function () {
29
+ const cancelTimer = () => {
19
30
  if (timeoutId !== null) {
20
31
  clearTimeout(timeoutId);
21
32
  timeoutId = null;
22
33
  }
23
34
  };
24
- signal?.addEventListener('abort', onAbort, { once: true });
35
+ const cancel = () => {
36
+ cancelTimer();
37
+ pendingThis = undefined;
38
+ pendingArgs = null;
39
+ };
40
+ const flush = () => {
41
+ cancelTimer();
42
+ invoke();
43
+ };
44
+ const debounced = function (...args) {
45
+ if (signal?.aborted) {
46
+ return;
47
+ }
48
+ pendingThis = this;
49
+ pendingArgs = args;
50
+ const isFirstCall = timeoutId == null;
51
+ schedule();
52
+ if (leading && isFirstCall) {
53
+ invoke();
54
+ }
55
+ };
56
+ debounced.schedule = schedule;
57
+ debounced.cancel = cancel;
58
+ debounced.flush = flush;
59
+ signal?.addEventListener('abort', cancel, { once: true });
25
60
  return debounced;
26
61
  }
27
62
 
@@ -2,7 +2,47 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const curry = require('../_chunk/curry-BmwJrK.js');
5
+ const rest = require('../_chunk/rest-pUyjvl.js');
6
+
7
+ function throttle(func, throttleMs, { signal, edges = ['leading', 'trailing'] } = {}) {
8
+ let pendingAt = null;
9
+ const debounced = rest.debounce(func, throttleMs, { signal, edges });
10
+ const throttled = function (...args) {
11
+ if (pendingAt == null) {
12
+ pendingAt = Date.now();
13
+ }
14
+ else {
15
+ if (Date.now() - pendingAt >= throttleMs) {
16
+ debounced.cancel();
17
+ debounced(...args);
18
+ }
19
+ }
20
+ debounced(...args);
21
+ };
22
+ throttled.cancel = debounced.cancel;
23
+ throttled.flush = debounced.flush;
24
+ return throttled;
25
+ }
26
+
27
+ function curry(func) {
28
+ if (func.length === 0 || func.length === 1) {
29
+ return func;
30
+ }
31
+ return function (arg) {
32
+ return makeCurry(func, func.length, [arg]);
33
+ };
34
+ }
35
+ function makeCurry(origin, argsLength, args) {
36
+ if (args.length === argsLength) {
37
+ return origin(...args);
38
+ }
39
+ else {
40
+ const next = function (arg) {
41
+ return makeCurry(origin, argsLength, [...args, arg]);
42
+ };
43
+ return next;
44
+ }
45
+ }
6
46
 
7
47
  function spread(func) {
8
48
  return function (argsArr) {
@@ -10,18 +50,18 @@ function spread(func) {
10
50
  };
11
51
  }
12
52
 
13
- exports.after = curry.after;
14
- exports.ary = curry.ary;
15
- exports.before = curry.before;
16
- exports.curry = curry.curry;
17
- exports.debounce = curry.debounce;
18
- exports.memoize = curry.memoize;
19
- exports.negate = curry.negate;
20
- exports.noop = curry.noop;
21
- exports.once = curry.once;
22
- exports.partial = curry.partial;
23
- exports.partialRight = curry.partialRight;
24
- exports.rest = curry.rest;
25
- exports.throttle = curry.throttle;
26
- exports.unary = curry.unary;
53
+ exports.after = rest.after;
54
+ exports.ary = rest.ary;
55
+ exports.before = rest.before;
56
+ exports.debounce = rest.debounce;
57
+ exports.memoize = rest.memoize;
58
+ exports.negate = rest.negate;
59
+ exports.noop = rest.noop;
60
+ exports.once = rest.once;
61
+ exports.partial = rest.partial;
62
+ exports.partialRight = rest.partialRight;
63
+ exports.rest = rest.rest;
64
+ exports.unary = rest.unary;
65
+ exports.curry = curry;
27
66
  exports.spread = spread;
67
+ exports.throttle = throttle;
@@ -1,3 +1,17 @@
1
+ interface ThrottleOptions {
2
+ /**
3
+ * An optional AbortSignal to cancel the debounced function.
4
+ */
5
+ signal?: AbortSignal;
6
+ /**
7
+ * An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
8
+ * If `edges` includes "leading", the function will be invoked at the start of the delay period.
9
+ * If `edges` includes "trailing", the function will be invoked at the end of the delay period.
10
+ * If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period.
11
+ * @default ["leading", "trailing"]
12
+ */
13
+ edges?: Array<'leading' | 'trailing'>;
14
+ }
1
15
  /**
2
16
  * Creates a throttled function that only invokes the provided function at most once
3
17
  * per every `throttleMs` milliseconds. Subsequent calls to the throttled function
@@ -24,6 +38,9 @@
24
38
  * throttledFunction(); // Will log 'Function executed'
25
39
  * }, 1000);
26
40
  */
27
- declare function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number): (...args: Parameters<F>) => void;
41
+ declare function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number, { signal, edges }?: ThrottleOptions): ((...args: Parameters<F>) => void) & {
42
+ cancel: () => void;
43
+ flush: () => void;
44
+ };
28
45
 
29
46
  export { throttle };
@@ -1,3 +1,17 @@
1
+ interface ThrottleOptions {
2
+ /**
3
+ * An optional AbortSignal to cancel the debounced function.
4
+ */
5
+ signal?: AbortSignal;
6
+ /**
7
+ * An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
8
+ * If `edges` includes "leading", the function will be invoked at the start of the delay period.
9
+ * If `edges` includes "trailing", the function will be invoked at the end of the delay period.
10
+ * If both "leading" and "trailing" are included, the function will be invoked at both the start and end of the delay period.
11
+ * @default ["leading", "trailing"]
12
+ */
13
+ edges?: Array<'leading' | 'trailing'>;
14
+ }
1
15
  /**
2
16
  * Creates a throttled function that only invokes the provided function at most once
3
17
  * per every `throttleMs` milliseconds. Subsequent calls to the throttled function
@@ -24,6 +38,9 @@
24
38
  * throttledFunction(); // Will log 'Function executed'
25
39
  * }, 1000);
26
40
  */
27
- declare function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number): (...args: Parameters<F>) => void;
41
+ declare function throttle<F extends (...args: any[]) => void>(func: F, throttleMs: number, { signal, edges }?: ThrottleOptions): ((...args: Parameters<F>) => void) & {
42
+ cancel: () => void;
43
+ flush: () => void;
44
+ };
28
45
 
29
46
  export { throttle };
@@ -1,13 +1,23 @@
1
- function throttle(func, throttleMs) {
2
- let lastCallTime;
3
- const throttledFunction = function (...args) {
4
- const now = Date.now();
5
- if (lastCallTime == null || now - lastCallTime >= throttleMs) {
6
- lastCallTime = now;
7
- func(...args);
1
+ import { debounce } from './debounce.mjs';
2
+
3
+ function throttle(func, throttleMs, { signal, edges = ['leading', 'trailing'] } = {}) {
4
+ let pendingAt = null;
5
+ const debounced = debounce(func, throttleMs, { signal, edges });
6
+ const throttled = function (...args) {
7
+ if (pendingAt == null) {
8
+ pendingAt = Date.now();
9
+ }
10
+ else {
11
+ if (Date.now() - pendingAt >= throttleMs) {
12
+ debounced.cancel();
13
+ debounced(...args);
14
+ }
8
15
  }
16
+ debounced(...args);
9
17
  };
10
- return throttledFunction;
18
+ throttled.cancel = debounced.cancel;
19
+ throttled.flush = debounced.flush;
20
+ return throttled;
11
21
  }
12
22
 
13
23
  export { throttle };