es-toolkit 1.23.0-dev.731 → 1.23.0-dev.732

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.
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Creates a function that invokes `func`, with the `this` binding and arguments
3
+ * of the created function, while it's called less than `n` times. Subsequent
4
+ * calls to the created function return the result of the last `func` invocation.
5
+ *
6
+ * @template F - The type of the function to be invoked.
7
+ * @param {number} n - The number of times the returned function is allowed to call `func` before stopping.
8
+ * - If `n` is 0, `func` will never be called.
9
+ * - If `n` is a positive integer, `func` will be called up to `n-1` times.
10
+ * @param {F} func - The function to be called with the limit applied.
11
+ * @returns {(...args: Parameters<F>) => ReturnType<F> } - A new function that:
12
+ * - Tracks the number of calls.
13
+ * - Invokes `func` until the `n-1`-th call.
14
+ * - Returns last result of `func`, if `n` is reached.
15
+ * @throws {TypeError} - If `func` is not a function.
16
+ * @example
17
+ * let count = 0;
18
+ * const before3 = before(3, () => ++count);
19
+ *
20
+ * before3(); // => 1
21
+ * before3(); // => 2
22
+ * before3(); // => 3
23
+ * before3(); // => 3
24
+ */
25
+ declare function before<F extends (...args: any[]) => any>(n: number, func: F): (...args: Parameters<F>) => ReturnType<F>;
26
+
27
+ export { before };
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Creates a function that invokes `func`, with the `this` binding and arguments
3
+ * of the created function, while it's called less than `n` times. Subsequent
4
+ * calls to the created function return the result of the last `func` invocation.
5
+ *
6
+ * @template F - The type of the function to be invoked.
7
+ * @param {number} n - The number of times the returned function is allowed to call `func` before stopping.
8
+ * - If `n` is 0, `func` will never be called.
9
+ * - If `n` is a positive integer, `func` will be called up to `n-1` times.
10
+ * @param {F} func - The function to be called with the limit applied.
11
+ * @returns {(...args: Parameters<F>) => ReturnType<F> } - A new function that:
12
+ * - Tracks the number of calls.
13
+ * - Invokes `func` until the `n-1`-th call.
14
+ * - Returns last result of `func`, if `n` is reached.
15
+ * @throws {TypeError} - If `func` is not a function.
16
+ * @example
17
+ * let count = 0;
18
+ * const before3 = before(3, () => ++count);
19
+ *
20
+ * before3(); // => 1
21
+ * before3(); // => 2
22
+ * before3(); // => 3
23
+ * before3(); // => 3
24
+ */
25
+ declare function before<F extends (...args: any[]) => any>(n: number, func: F): (...args: Parameters<F>) => ReturnType<F>;
26
+
27
+ export { before };
@@ -0,0 +1,20 @@
1
+ import { toInteger } from '../util/toInteger.mjs';
2
+
3
+ function before(n, func) {
4
+ if (typeof func !== 'function') {
5
+ throw new TypeError('Expected a function');
6
+ }
7
+ let result;
8
+ n = toInteger(n);
9
+ return function (...args) {
10
+ if (--n > 0) {
11
+ result = func.apply(this, args);
12
+ }
13
+ if (n <= 1 && func) {
14
+ func = undefined;
15
+ }
16
+ return result;
17
+ };
18
+ }
19
+
20
+ export { before };
@@ -46,7 +46,6 @@ export { zipObject } from '../array/zipObject.mjs';
46
46
  export { zipWith } from '../array/zipWith.mjs';
47
47
  export { AbortError } from '../error/AbortError.mjs';
48
48
  export { TimeoutError } from '../error/TimeoutError.mjs';
49
- export { before } from '../function/before.mjs';
50
49
  export { after } from '../function/after.mjs';
51
50
  export { noop } from '../function/noop.mjs';
52
51
  export { once } from '../function/once.mjs';
@@ -117,6 +116,7 @@ export { zipObjectDeep } from './array/zipObjectDeep.mjs';
117
116
  export { filter } from './array/filter.mjs';
118
117
  export { ary } from './function/ary.mjs';
119
118
  export { bind } from './function/bind.mjs';
119
+ export { before } from './function/before.mjs';
120
120
  export { bindKey } from './function/bindKey.mjs';
121
121
  export { defer } from './function/defer.mjs';
122
122
  export { rest } from './function/rest.mjs';
@@ -46,7 +46,6 @@ export { zipObject } from '../array/zipObject.js';
46
46
  export { zipWith } from '../array/zipWith.js';
47
47
  export { AbortError } from '../error/AbortError.js';
48
48
  export { TimeoutError } from '../error/TimeoutError.js';
49
- export { before } from '../function/before.js';
50
49
  export { after } from '../function/after.js';
51
50
  export { noop } from '../function/noop.js';
52
51
  export { once } from '../function/once.js';
@@ -117,6 +116,7 @@ export { zipObjectDeep } from './array/zipObjectDeep.js';
117
116
  export { filter } from './array/filter.js';
118
117
  export { ary } from './function/ary.js';
119
118
  export { bind } from './function/bind.js';
119
+ export { before } from './function/before.js';
120
120
  export { bindKey } from './function/bindKey.js';
121
121
  export { defer } from './function/defer.js';
122
122
  export { rest } from './function/rest.js';
@@ -4,7 +4,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
5
  const zipWith = require('../_chunk/zipWith-EOU_KZ.js');
6
6
  const promise_index = require('../_chunk/index-BGZDR9.js');
7
- const flowRight$1 = require('../_chunk/flowRight-Dv8J0U.js');
7
+ const flowRight$1 = require('../_chunk/flowRight-CVcg72.js');
8
8
  const noop = require('../_chunk/noop-2IwLUk.js');
9
9
  const range = require('../_chunk/range-BXlMmn.js');
10
10
  const randomInt = require('../_chunk/randomInt-CF7bZK.js');
@@ -939,6 +939,23 @@ function bind(func, thisObj, ...partialArgs) {
939
939
  const bindPlaceholder = Symbol('bind.placeholder');
940
940
  bind.placeholder = bindPlaceholder;
941
941
 
942
+ function before(n, func) {
943
+ if (typeof func !== 'function') {
944
+ throw new TypeError('Expected a function');
945
+ }
946
+ let result;
947
+ n = toInteger(n);
948
+ return function (...args) {
949
+ if (--n > 0) {
950
+ result = func.apply(this, args);
951
+ }
952
+ if (n <= 1 && func) {
953
+ func = undefined;
954
+ }
955
+ return result;
956
+ };
957
+ }
958
+
942
959
  function bindKey(object, key, ...partialArgs) {
943
960
  const bound = function (...providedArgs) {
944
961
  const args = [];
@@ -1946,7 +1963,6 @@ exports.delay = promise_index.delay;
1946
1963
  exports.timeout = promise_index.timeout;
1947
1964
  exports.withTimeout = promise_index.withTimeout;
1948
1965
  exports.after = flowRight$1.after;
1949
- exports.before = flowRight$1.before;
1950
1966
  exports.curryRight = flowRight$1.curryRight;
1951
1967
  exports.memoize = flowRight$1.memoize;
1952
1968
  exports.negate = flowRight$1.negate;
@@ -1990,6 +2006,7 @@ exports.unescape = pad$1.unescape;
1990
2006
  exports.upperFirst = pad$1.upperFirst;
1991
2007
  exports.ary = ary;
1992
2008
  exports.attempt = attempt;
2009
+ exports.before = before;
1993
2010
  exports.bind = bind;
1994
2011
  exports.bindKey = bindKey;
1995
2012
  exports.camelCase = camelCase;
@@ -46,7 +46,6 @@ export { zipObject } from '../array/zipObject.mjs';
46
46
  export { zipWith } from '../array/zipWith.mjs';
47
47
  export { AbortError } from '../error/AbortError.mjs';
48
48
  export { TimeoutError } from '../error/TimeoutError.mjs';
49
- export { before } from '../function/before.mjs';
50
49
  export { after } from '../function/after.mjs';
51
50
  export { noop } from '../function/noop.mjs';
52
51
  export { once } from '../function/once.mjs';
@@ -118,6 +117,7 @@ export { zipObjectDeep } from './array/zipObjectDeep.mjs';
118
117
  export { filter } from './array/filter.mjs';
119
118
  export { ary } from './function/ary.mjs';
120
119
  export { bind } from './function/bind.mjs';
120
+ export { before } from './function/before.mjs';
121
121
  export { bindKey } from './function/bindKey.mjs';
122
122
  export { defer } from './function/defer.mjs';
123
123
  export { rest } from './function/rest.mjs';
@@ -2,9 +2,22 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const flowRight = require('../_chunk/flowRight-Dv8J0U.js');
5
+ const flowRight = require('../_chunk/flowRight-CVcg72.js');
6
6
  const noop = require('../_chunk/noop-2IwLUk.js');
7
7
 
8
+ function before(n, func) {
9
+ if (!Number.isInteger(n) || n < 0) {
10
+ throw new Error('n must be a non-negative integer.');
11
+ }
12
+ let counter = 0;
13
+ return ((...args) => {
14
+ if (++counter < n) {
15
+ return func(...args);
16
+ }
17
+ return undefined;
18
+ });
19
+ }
20
+
8
21
  function throttle(func, throttleMs, { signal, edges = ['leading', 'trailing'] } = {}) {
9
22
  let pendingAt = null;
10
23
  const debounced = flowRight.debounce(func, throttleMs, { signal, edges });
@@ -54,7 +67,6 @@ function spread(func) {
54
67
 
55
68
  exports.after = flowRight.after;
56
69
  exports.ary = flowRight.ary;
57
- exports.before = flowRight.before;
58
70
  exports.curryRight = flowRight.curryRight;
59
71
  exports.debounce = flowRight.debounce;
60
72
  exports.flow = flowRight.flow;
@@ -67,6 +79,7 @@ exports.partialRight = flowRight.partialRight;
67
79
  exports.rest = flowRight.rest;
68
80
  exports.unary = flowRight.unary;
69
81
  exports.noop = noop.noop;
82
+ exports.before = before;
70
83
  exports.curry = curry;
71
84
  exports.spread = spread;
72
85
  exports.throttle = throttle;
package/dist/index.js CHANGED
@@ -5,9 +5,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5
5
  const zipWith = require('./_chunk/zipWith-EOU_KZ.js');
6
6
  const array_index = require('./array/index.js');
7
7
  const promise_index = require('./_chunk/index-BGZDR9.js');
8
- const flowRight = require('./_chunk/flowRight-Dv8J0U.js');
9
- const noop = require('./_chunk/noop-2IwLUk.js');
10
8
  const function_index = require('./function/index.js');
9
+ const flowRight = require('./_chunk/flowRight-CVcg72.js');
10
+ const noop = require('./_chunk/noop-2IwLUk.js');
11
11
  const range = require('./_chunk/range-BXlMmn.js');
12
12
  const randomInt = require('./_chunk/randomInt-CF7bZK.js');
13
13
  const math_index = require('./math/index.js');
@@ -82,9 +82,12 @@ exports.TimeoutError = promise_index.TimeoutError;
82
82
  exports.delay = promise_index.delay;
83
83
  exports.timeout = promise_index.timeout;
84
84
  exports.withTimeout = promise_index.withTimeout;
85
+ exports.before = function_index.before;
86
+ exports.curry = function_index.curry;
87
+ exports.spread = function_index.spread;
88
+ exports.throttle = function_index.throttle;
85
89
  exports.after = flowRight.after;
86
90
  exports.ary = flowRight.ary;
87
- exports.before = flowRight.before;
88
91
  exports.curryRight = flowRight.curryRight;
89
92
  exports.debounce = flowRight.debounce;
90
93
  exports.flow = flowRight.flow;
@@ -97,9 +100,6 @@ exports.partialRight = flowRight.partialRight;
97
100
  exports.rest = flowRight.rest;
98
101
  exports.unary = flowRight.unary;
99
102
  exports.noop = noop.noop;
100
- exports.curry = function_index.curry;
101
- exports.spread = function_index.spread;
102
- exports.throttle = function_index.throttle;
103
103
  exports.clamp = range.clamp;
104
104
  exports.inRange = range.inRange;
105
105
  exports.mean = range.mean;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
3
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
4
- "version": "1.23.0-dev.731+aca23aba",
4
+ "version": "1.23.0-dev.732+65d65b57",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {