es-toolkit 1.17.0-dev.508 → 1.17.0-dev.510

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,47 @@
1
+ /**
2
+ * Creates a new function that spreads elements of an array argument into individual arguments
3
+ * for the original function. The array argument is positioned based on the `argsIndex` parameter.
4
+ *
5
+ * @template F - A function type with any number of parameters and any return type.
6
+ * @param {F} func - The function to be transformed. It can be any function with any number of arguments.
7
+ * @param {number} [argsIndex=0] - The index where the array argument is positioned among the other arguments.
8
+ * If `argsIndex` is negative or `NaN`, it defaults to `0`. If it's a fractional number, it is rounded to the nearest integer.
9
+ * @returns {(...args: any[]) => ReturnType<F>} - A new function that takes multiple arguments, including an array of arguments at the specified `argsIndex`,
10
+ * and returns the result of calling the original function with those arguments.
11
+ *
12
+ * @example
13
+ * function add(a, b) {
14
+ * return a + b;
15
+ * }
16
+ *
17
+ * const spreadAdd = spread(add);
18
+ * console.log(spreadAdd([1, 2])); // Output: 3
19
+ *
20
+ * @example
21
+ * // Example function to spread arguments over
22
+ * function add(a, b) {
23
+ * return a + b;
24
+ * }
25
+ *
26
+ * // Create a new function that uses `spread` to combine arguments
27
+ * const spreadAdd = spread(add, 1);
28
+ *
29
+ * // Calling `spreadAdd` with an array as the second argument
30
+ * console.log(spreadAdd(1, [2])); // Output: 3
31
+ *
32
+ * @example
33
+ * // Function with default arguments
34
+ * function greet(name, greeting = 'Hello') {
35
+ * return `${greeting}, ${name}!`;
36
+ * }
37
+ *
38
+ * // Create a new function that uses `spread` to position the argument array at index 0
39
+ * const spreadGreet = spread(greet, 0);
40
+ *
41
+ * // Calling `spreadGreet` with an array of arguments
42
+ * console.log(spreadGreet(['Alice'])); // Output: Hello, Alice!
43
+ * console.log(spreadGreet(['Bob', 'Hi'])); // Output: Hi, Bob!
44
+ */
45
+ declare function spread<F extends (...args: any[]) => any>(func: F, argsIndex?: number): (...args: any[]) => ReturnType<F>;
46
+
47
+ export { spread };
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Creates a new function that spreads elements of an array argument into individual arguments
3
+ * for the original function. The array argument is positioned based on the `argsIndex` parameter.
4
+ *
5
+ * @template F - A function type with any number of parameters and any return type.
6
+ * @param {F} func - The function to be transformed. It can be any function with any number of arguments.
7
+ * @param {number} [argsIndex=0] - The index where the array argument is positioned among the other arguments.
8
+ * If `argsIndex` is negative or `NaN`, it defaults to `0`. If it's a fractional number, it is rounded to the nearest integer.
9
+ * @returns {(...args: any[]) => ReturnType<F>} - A new function that takes multiple arguments, including an array of arguments at the specified `argsIndex`,
10
+ * and returns the result of calling the original function with those arguments.
11
+ *
12
+ * @example
13
+ * function add(a, b) {
14
+ * return a + b;
15
+ * }
16
+ *
17
+ * const spreadAdd = spread(add);
18
+ * console.log(spreadAdd([1, 2])); // Output: 3
19
+ *
20
+ * @example
21
+ * // Example function to spread arguments over
22
+ * function add(a, b) {
23
+ * return a + b;
24
+ * }
25
+ *
26
+ * // Create a new function that uses `spread` to combine arguments
27
+ * const spreadAdd = spread(add, 1);
28
+ *
29
+ * // Calling `spreadAdd` with an array as the second argument
30
+ * console.log(spreadAdd(1, [2])); // Output: 3
31
+ *
32
+ * @example
33
+ * // Function with default arguments
34
+ * function greet(name, greeting = 'Hello') {
35
+ * return `${greeting}, ${name}!`;
36
+ * }
37
+ *
38
+ * // Create a new function that uses `spread` to position the argument array at index 0
39
+ * const spreadGreet = spread(greet, 0);
40
+ *
41
+ * // Calling `spreadGreet` with an array of arguments
42
+ * console.log(spreadGreet(['Alice'])); // Output: Hello, Alice!
43
+ * console.log(spreadGreet(['Bob', 'Hi'])); // Output: Hi, Bob!
44
+ */
45
+ declare function spread<F extends (...args: any[]) => any>(func: F, argsIndex?: number): (...args: any[]) => ReturnType<F>;
46
+
47
+ export { spread };
@@ -0,0 +1,16 @@
1
+ function spread(func, argsIndex = 0) {
2
+ argsIndex = Number.parseInt(argsIndex, 10);
3
+ if (Number.isNaN(argsIndex) || argsIndex < 0) {
4
+ argsIndex = 0;
5
+ }
6
+ return function (...args) {
7
+ const array = args[argsIndex];
8
+ const params = args.slice(0, argsIndex);
9
+ if (array) {
10
+ params.push(...array);
11
+ }
12
+ return func.apply(this, params);
13
+ };
14
+ }
15
+
16
+ export { spread };
@@ -116,6 +116,7 @@ export { indexOf } from './array/indexOf.mjs';
116
116
  export { ary } from './function/ary.mjs';
117
117
  export { bind } from './function/bind.mjs';
118
118
  export { rest } from './function/rest.mjs';
119
+ export { spread } from './function/spread.mjs';
119
120
  export { get } from './object/get.mjs';
120
121
  export { set } from './object/set.mjs';
121
122
  export { has } from './object/has.mjs';
@@ -116,6 +116,7 @@ export { indexOf } from './array/indexOf.js';
116
116
  export { ary } from './function/ary.js';
117
117
  export { bind } from './function/bind.js';
118
118
  export { rest } from './function/rest.js';
119
+ export { spread } from './function/spread.js';
119
120
  export { get } from './object/get.js';
120
121
  export { set } from './object/set.js';
121
122
  export { has } from './object/has.js';
@@ -4,7 +4,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
5
  const initial = require('../_chunk/initial-Ci2bn_.js');
6
6
  const promise_index = require('../_chunk/index-BGZDR9.js');
7
- const function_index = require('../function/index.js');
7
+ const rest$1 = require('../_chunk/rest-Bzm2XK.js');
8
8
  const math_index = require('../math/index.js');
9
9
  const randomInt = require('../_chunk/randomInt-CF7bZK.js');
10
10
  const toMerged = require('../_chunk/toMerged-BGwYW5.js');
@@ -574,7 +574,7 @@ function ary(func, n = func.length, guard) {
574
574
  if (Number.isNaN(n) || n < 0) {
575
575
  n = 0;
576
576
  }
577
- return function_index.ary(func, n);
577
+ return rest$1.ary(func, n);
578
578
  }
579
579
 
580
580
  function bind(func, thisObj, ...partialArgs) {
@@ -608,7 +608,22 @@ function rest(func, start = func.length - 1) {
608
608
  if (Number.isNaN(start) || start < 0) {
609
609
  start = func.length - 1;
610
610
  }
611
- return function_index.rest(func, start);
611
+ return rest$1.rest(func, start);
612
+ }
613
+
614
+ function spread(func, argsIndex = 0) {
615
+ argsIndex = Number.parseInt(argsIndex, 10);
616
+ if (Number.isNaN(argsIndex) || argsIndex < 0) {
617
+ argsIndex = 0;
618
+ }
619
+ return function (...args) {
620
+ const array = args[argsIndex];
621
+ const params = args.slice(0, argsIndex);
622
+ if (array) {
623
+ params.push(...array);
624
+ }
625
+ return func.apply(this, params);
626
+ };
612
627
  }
613
628
 
614
629
  function identity(x) {
@@ -742,7 +757,7 @@ function mergeWithDeep(target, source, merge, stack) {
742
757
  }
743
758
 
744
759
  function merge(object, ...sources) {
745
- return mergeWith(object, ...sources, function_index.noop);
760
+ return mergeWith(object, ...sources, rest$1.noop);
746
761
  }
747
762
 
748
763
  function isArray(value) {
@@ -872,17 +887,17 @@ exports.TimeoutError = promise_index.TimeoutError;
872
887
  exports.delay = promise_index.delay;
873
888
  exports.timeout = promise_index.timeout;
874
889
  exports.withTimeout = promise_index.withTimeout;
875
- exports.after = function_index.after;
876
- exports.before = function_index.before;
877
- exports.debounce = function_index.debounce;
878
- exports.memoize = function_index.memoize;
879
- exports.negate = function_index.negate;
880
- exports.noop = function_index.noop;
881
- exports.once = function_index.once;
882
- exports.partial = function_index.partial;
883
- exports.partialRight = function_index.partialRight;
884
- exports.throttle = function_index.throttle;
885
- exports.unary = function_index.unary;
890
+ exports.after = rest$1.after;
891
+ exports.before = rest$1.before;
892
+ exports.debounce = rest$1.debounce;
893
+ exports.memoize = rest$1.memoize;
894
+ exports.negate = rest$1.negate;
895
+ exports.noop = rest$1.noop;
896
+ exports.once = rest$1.once;
897
+ exports.partial = rest$1.partial;
898
+ exports.partialRight = rest$1.partialRight;
899
+ exports.throttle = rest$1.throttle;
900
+ exports.unary = rest$1.unary;
886
901
  exports.clamp = math_index.clamp;
887
902
  exports.inRange = math_index.inRange;
888
903
  exports.mean = math_index.mean;
@@ -961,5 +976,6 @@ exports.property = property;
961
976
  exports.rest = rest;
962
977
  exports.set = set;
963
978
  exports.size = size;
979
+ exports.spread = spread;
964
980
  exports.startsWith = startsWith;
965
981
  exports.zipObjectDeep = zipObjectDeep;
@@ -117,6 +117,7 @@ export { indexOf } from './array/indexOf.mjs';
117
117
  export { ary } from './function/ary.mjs';
118
118
  export { bind } from './function/bind.mjs';
119
119
  export { rest } from './function/rest.mjs';
120
+ export { spread } from './function/spread.mjs';
120
121
  export { get } from './object/get.mjs';
121
122
  export { set } from './object/set.mjs';
122
123
  export { has } from './object/has.mjs';
@@ -11,3 +11,4 @@ export { unary } from './unary.mjs';
11
11
  export { partial } from './partial.mjs';
12
12
  export { partialRight } from './partialRight.mjs';
13
13
  export { rest } from './rest.mjs';
14
+ export { spread } from './spread.mjs';
@@ -11,3 +11,4 @@ export { unary } from './unary.js';
11
11
  export { partial } from './partial.js';
12
12
  export { partialRight } from './partialRight.js';
13
13
  export { rest } from './rest.js';
14
+ export { spread } from './spread.js';
@@ -2,183 +2,25 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const before = (n, func) => {
6
- if (!Number.isInteger(n) || n < 0) {
7
- throw new Error('n must be a non-negative integer.');
8
- }
9
- let counter = 0;
10
- return ((...args) => {
11
- if (++counter < n) {
12
- return func(...args);
13
- }
14
- return undefined;
15
- });
16
- };
17
-
18
- const after = (n, func) => {
19
- if (!Number.isInteger(n) || n < 0) {
20
- throw new Error(`n must be a non-negative integer.`);
21
- }
22
- let counter = 0;
23
- return ((...args) => {
24
- if (++counter >= n) {
25
- return func(...args);
26
- }
27
- return undefined;
28
- });
29
- };
30
-
31
- function debounce(func, debounceMs, { signal } = {}) {
32
- let timeoutId = null;
33
- const debounced = function (...args) {
34
- if (timeoutId !== null) {
35
- clearTimeout(timeoutId);
36
- }
37
- if (signal?.aborted) {
38
- return;
39
- }
40
- timeoutId = setTimeout(() => {
41
- func(...args);
42
- timeoutId = null;
43
- }, debounceMs);
44
- };
45
- const onAbort = function () {
46
- debounced.cancel();
47
- };
48
- debounced.cancel = function () {
49
- if (timeoutId !== null) {
50
- clearTimeout(timeoutId);
51
- timeoutId = null;
52
- }
53
- };
54
- signal?.addEventListener('abort', onAbort, { once: true });
55
- return debounced;
56
- }
57
-
58
- function noop() { }
59
-
60
- function once(func) {
61
- let called = false;
62
- let cache;
63
- return function () {
64
- if (called) {
65
- return cache;
66
- }
67
- const result = func();
68
- called = true;
69
- cache = result;
70
- return result;
71
- };
72
- }
73
-
74
- function throttle(func, throttleMs) {
75
- let lastCallTime;
76
- const throttledFunction = function (...args) {
77
- const now = Date.now();
78
- if (lastCallTime == null || now - lastCallTime >= throttleMs) {
79
- lastCallTime = now;
80
- func(...args);
81
- }
82
- };
83
- return throttledFunction;
84
- }
85
-
86
- function negate(func) {
87
- return ((...args) => !func(...args));
88
- }
89
-
90
- function memoize(fn, options = {}) {
91
- const { cache = new Map(), getCacheKey } = options;
92
- const memoizedFn = function (arg) {
93
- const key = getCacheKey ? getCacheKey(arg) : arg;
94
- if (cache.has(key)) {
95
- return cache.get(key);
96
- }
97
- const result = fn.call(this, arg);
98
- cache.set(key, result);
99
- return result;
100
- };
101
- memoizedFn.cache = cache;
102
- return memoizedFn;
103
- }
104
-
105
- function ary(func, n) {
106
- return function (...args) {
107
- return func.apply(this, args.slice(0, n));
108
- };
109
- }
110
-
111
- function unary(func) {
112
- return ary(func, 1);
113
- }
114
-
115
- function partial(func, ...partialArgs) {
116
- return function (...providedArgs) {
117
- const args = [];
118
- let startIndex = 0;
119
- for (let i = 0; i < partialArgs.length; i++) {
120
- const arg = partialArgs[i];
121
- if (arg === partial.placeholder) {
122
- args.push(providedArgs[startIndex++]);
123
- }
124
- else {
125
- args.push(arg);
126
- }
127
- }
128
- for (let i = startIndex; i < providedArgs.length; i++) {
129
- args.push(providedArgs[i]);
130
- }
131
- return func.apply(this, args);
132
- };
133
- }
134
- const partialPlaceholder = Symbol('partial.placeholder');
135
- partial.placeholder = partialPlaceholder;
136
-
137
- function partialRight(func, ...partialArgs) {
138
- return function (...providedArgs) {
139
- const placeholderLength = partialArgs.filter(arg => arg === partialRightPlaceholder).length;
140
- const rangeLength = Math.max(providedArgs.length - placeholderLength, 0);
141
- const args = [];
142
- let providedIndex = 0;
143
- for (let i = 0; i < rangeLength; i++) {
144
- args.push(providedArgs[providedIndex++]);
145
- }
146
- for (let i = 0; i < partialArgs.length; i++) {
147
- const arg = partialArgs[i];
148
- if (arg === partialRight.placeholder) {
149
- args.push(providedArgs[providedIndex++]);
150
- }
151
- else {
152
- args.push(arg);
153
- }
154
- }
155
- return func.apply(this, args);
156
- };
157
- }
158
- const partialRightPlaceholder = Symbol('partialRight.placeholder');
159
- partialRight.placeholder = partialRightPlaceholder;
160
-
161
- function rest(func, startIndex = func.length - 1) {
162
- return function (...args) {
163
- const rest = args.slice(startIndex);
164
- const params = args.slice(0, startIndex);
165
- while (params.length < startIndex) {
166
- params.push(undefined);
167
- }
168
- return func.apply(this, [...params, rest]);
169
- };
170
- }
171
-
172
- exports.after = after;
173
- exports.ary = ary;
174
- exports.before = before;
175
- exports.debounce = debounce;
176
- exports.memoize = memoize;
177
- exports.negate = negate;
178
- exports.noop = noop;
179
- exports.once = once;
180
- exports.partial = partial;
181
- exports.partialRight = partialRight;
182
- exports.rest = rest;
183
- exports.throttle = throttle;
184
- exports.unary = unary;
5
+ const rest = require('../_chunk/rest-Bzm2XK.js');
6
+
7
+ function spread(func) {
8
+ return function (argsArr) {
9
+ return func.apply(this, argsArr);
10
+ };
11
+ }
12
+
13
+ exports.after = rest.after;
14
+ exports.ary = rest.ary;
15
+ exports.before = rest.before;
16
+ exports.debounce = rest.debounce;
17
+ exports.memoize = rest.memoize;
18
+ exports.negate = rest.negate;
19
+ exports.noop = rest.noop;
20
+ exports.once = rest.once;
21
+ exports.partial = rest.partial;
22
+ exports.partialRight = rest.partialRight;
23
+ exports.rest = rest.rest;
24
+ exports.throttle = rest.throttle;
25
+ exports.unary = rest.unary;
26
+ exports.spread = spread;
@@ -11,3 +11,4 @@ export { unary } from './unary.mjs';
11
11
  export { partial } from './partial.mjs';
12
12
  export { partialRight } from './partialRight.mjs';
13
13
  export { rest } from './rest.mjs';
14
+ export { spread } from './spread.mjs';
@@ -0,0 +1,19 @@
1
+ /**
2
+ Creates a new function that spreads elements of an array argument into individual arguments
3
+ * for the original function.
4
+ *
5
+ * @template F - A function type with any number of parameters and any return type.
6
+ * @param {F} func - The function to be transformed. It can be any function with any number of arguments.
7
+ * @returns {(argsArr: Parameters<F>) => ReturnType<F>} - A new function that takes an array of arguments and returns the result of calling the original function with those arguments.
8
+ *
9
+ * @example
10
+ * function add(a, b) {
11
+ * return a + b;
12
+ * }
13
+ *
14
+ * const spreadAdd = spread(add);
15
+ * console.log(spreadAdd([1, 2])); // Output: 3
16
+ */
17
+ declare function spread<F extends (...args: any[]) => any>(func: F): (argsArr: Parameters<F>) => ReturnType<F>;
18
+
19
+ export { spread };
@@ -0,0 +1,19 @@
1
+ /**
2
+ Creates a new function that spreads elements of an array argument into individual arguments
3
+ * for the original function.
4
+ *
5
+ * @template F - A function type with any number of parameters and any return type.
6
+ * @param {F} func - The function to be transformed. It can be any function with any number of arguments.
7
+ * @returns {(argsArr: Parameters<F>) => ReturnType<F>} - A new function that takes an array of arguments and returns the result of calling the original function with those arguments.
8
+ *
9
+ * @example
10
+ * function add(a, b) {
11
+ * return a + b;
12
+ * }
13
+ *
14
+ * const spreadAdd = spread(add);
15
+ * console.log(spreadAdd([1, 2])); // Output: 3
16
+ */
17
+ declare function spread<F extends (...args: any[]) => any>(func: F): (argsArr: Parameters<F>) => ReturnType<F>;
18
+
19
+ export { spread };
@@ -0,0 +1,7 @@
1
+ function spread(func) {
2
+ return function (argsArr) {
3
+ return func.apply(this, argsArr);
4
+ };
5
+ }
6
+
7
+ export { spread };
package/dist/index.d.mts CHANGED
@@ -68,6 +68,7 @@ export { unary } from './function/unary.mjs';
68
68
  export { partial } from './function/partial.mjs';
69
69
  export { partialRight } from './function/partialRight.mjs';
70
70
  export { rest } from './function/rest.mjs';
71
+ export { spread } from './function/spread.mjs';
71
72
  export { clamp } from './math/clamp.mjs';
72
73
  export { inRange } from './math/inRange.mjs';
73
74
  export { mean } from './math/mean.mjs';
package/dist/index.d.ts CHANGED
@@ -68,6 +68,7 @@ export { unary } from './function/unary.js';
68
68
  export { partial } from './function/partial.js';
69
69
  export { partialRight } from './function/partialRight.js';
70
70
  export { rest } from './function/rest.js';
71
+ export { spread } from './function/spread.js';
71
72
  export { clamp } from './math/clamp.js';
72
73
  export { inRange } from './math/inRange.js';
73
74
  export { mean } from './math/mean.js';
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5
5
  const initial = require('./_chunk/initial-Ci2bn_.js');
6
6
  const array_index = require('./array/index.js');
7
7
  const promise_index = require('./_chunk/index-BGZDR9.js');
8
+ const rest = require('./_chunk/rest-Bzm2XK.js');
8
9
  const function_index = require('./function/index.js');
9
10
  const math_index = require('./math/index.js');
10
11
  const randomInt = require('./_chunk/randomInt-CF7bZK.js');
@@ -77,19 +78,20 @@ exports.TimeoutError = promise_index.TimeoutError;
77
78
  exports.delay = promise_index.delay;
78
79
  exports.timeout = promise_index.timeout;
79
80
  exports.withTimeout = promise_index.withTimeout;
80
- exports.after = function_index.after;
81
- exports.ary = function_index.ary;
82
- exports.before = function_index.before;
83
- exports.debounce = function_index.debounce;
84
- exports.memoize = function_index.memoize;
85
- exports.negate = function_index.negate;
86
- exports.noop = function_index.noop;
87
- exports.once = function_index.once;
88
- exports.partial = function_index.partial;
89
- exports.partialRight = function_index.partialRight;
90
- exports.rest = function_index.rest;
91
- exports.throttle = function_index.throttle;
92
- exports.unary = function_index.unary;
81
+ exports.after = rest.after;
82
+ exports.ary = rest.ary;
83
+ exports.before = rest.before;
84
+ exports.debounce = rest.debounce;
85
+ exports.memoize = rest.memoize;
86
+ exports.negate = rest.negate;
87
+ exports.noop = rest.noop;
88
+ exports.once = rest.once;
89
+ exports.partial = rest.partial;
90
+ exports.partialRight = rest.partialRight;
91
+ exports.rest = rest.rest;
92
+ exports.throttle = rest.throttle;
93
+ exports.unary = rest.unary;
94
+ exports.spread = function_index.spread;
93
95
  exports.clamp = math_index.clamp;
94
96
  exports.inRange = math_index.inRange;
95
97
  exports.mean = math_index.mean;
package/dist/index.mjs CHANGED
@@ -68,6 +68,7 @@ export { unary } from './function/unary.mjs';
68
68
  export { partial } from './function/partial.mjs';
69
69
  export { partialRight } from './function/partialRight.mjs';
70
70
  export { rest } from './function/rest.mjs';
71
+ export { spread } from './function/spread.mjs';
71
72
  export { clamp } from './math/clamp.mjs';
72
73
  export { inRange } from './math/inRange.mjs';
73
74
  export { mean } from './math/mean.mjs';
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.17.0-dev.508+5bccc517",
4
+ "version": "1.17.0-dev.510+a84ffa9a",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {