es-toolkit 1.32.0-dev.1007 → 1.32.0-dev.1009

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,109 @@
1
+ /**
2
+ * Reduces an array to a single value using an iteratee function.
3
+ *
4
+ * The `reduce()` function goes through each element in an array and applies a special function (called a "reducer") to them, one by one.
5
+ * This function takes the result of the previous step and the current element to perform a calculation.
6
+ * After going through all the elements, the function gives you one final result.
7
+ *
8
+ * When the `reduce()` function starts, there's no previous result to use.
9
+ * If you provide an initial value, it starts with that.
10
+ * If not, it uses the first element of the array and begins with the second element for the calculation.
11
+ *
12
+ * @param {readonly T[]} collection - The collection to iterate over.
13
+ * @param {(accumulator: U, value: T, index: number, collection: readonly T[]) => U} iteratee - The function invoked per iteration.
14
+ * @param {U} initialValue - The initial value.
15
+ * @returns {U} - Returns the accumulated value.
16
+ *
17
+ * @example
18
+ * const arrayLike = [1, 2, 3];
19
+ * reduce(arrayLike, (acc, value) => acc && value % 2 === 0, true); // => false
20
+ */
21
+ declare function reduce<T, U>(collection: readonly T[], iteratee: (accumulator: U, value: T, index: number, collection: readonly T[]) => U, initialValue: U): U;
22
+ /**
23
+ * Reduces an array to a single value using an iteratee function.
24
+ *
25
+ * The `reduce()` function goes through each element in an array and applies a special function (called a "reducer") to them, one by one.
26
+ * This function takes the result of the previous step and the current element to perform a calculation.
27
+ * After going through all the elements, the function gives you one final result.
28
+ *
29
+ * When the `reduce()` function starts, there's no previous result to use.
30
+ * If you provide an initial value, it starts with that.
31
+ * If not, it uses the first element of the array and begins with the second element for the calculation.
32
+ *
33
+ * @param {readonly T[]} collection - The collection to iterate over.
34
+ * @param {(accumulator: T, value: T, index: number, collection: readonly T[]) => T} iteratee - The function invoked per iteration.
35
+ * @returns {T} - Returns the accumulated value.
36
+ *
37
+ * @example
38
+ * const arrayLike = [1, 2, 3];
39
+ * reduce(arrayLike, (acc, value) => acc + value); // => 6
40
+ */
41
+ declare function reduce<T>(collection: readonly T[], iteratee: (accumulator: T, value: T, index: number, collection: readonly T[]) => T): T;
42
+ /**
43
+ * Reduces an array to a single value using an iteratee function.
44
+ *
45
+ * The `reduce()` function goes through each element in an array and applies a special function (called a "reducer") to them, one by one.
46
+ * This function takes the result of the previous step and the current element to perform a calculation.
47
+ * After going through all the elements, the function gives you one final result.
48
+ *
49
+ * When the `reduce()` function starts, there's no previous result to use.
50
+ * If you provide an initial value, it starts with that.
51
+ * If not, it uses the first element of the array and begins with the second element for the calculation.
52
+ *
53
+ * @param {ArrayLike<T>} collection - The collection to iterate over.
54
+ * @param {(accumulator: U, value: T, index: number, collection: ArrayLike<T>) => U} iteratee - The function invoked per iteration.
55
+ * @param {U} initialValue - The initial value.
56
+ * @returns {U} - Returns the accumulated value.
57
+ *
58
+ * @example
59
+ * const arrayLike = {0: 1, 1: 2, 2: 3, length: 3};
60
+ * reduce(arrayLike, (acc, value) => acc + value % 2 === 0, true); // => false
61
+ */
62
+ declare function reduce<T, U>(collection: ArrayLike<T>, iteratee: (accumulator: U, value: T, index: number, collection: ArrayLike<T>) => U, initialValue: U): U;
63
+ /**
64
+ * Reduces an array to a single value using an iteratee function.
65
+ *
66
+ * The `reduce()` function goes through each element in an array and applies a special function (called a "reducer") to them, one by one.
67
+ * This function takes the result of the previous step and the current element to perform a calculation.
68
+ * After going through all the elements, the function gives you one final result.
69
+ *
70
+ * When the `reduce()` function starts, there's no previous result to use.
71
+ * If you provide an initial value, it starts with that.
72
+ * If not, it uses the first element of the array and begins with the second element for the calculation.
73
+ *
74
+ * @param {ArrayLike<T>} collection - The collection to iterate over.
75
+ * @param {(accumulator: U, value: T, index: number, collection: ArrayLike<T>) => U} iteratee - The function invoked per iteration.
76
+ * @returns {T} - Returns the accumulated value.
77
+ *
78
+ * @example
79
+ * const arrayLike = {0: 1, 1: 2, 2: 3, length: 3};
80
+ * reduce(arrayLike, (acc, value) => acc + value); // => 6
81
+ */
82
+ declare function reduce<T>(collection: ArrayLike<T>, iteratee: (accumulator: T, value: T, index: number, collection: ArrayLike<T>) => T): T;
83
+ /**
84
+ * Reduces an object to a single value using an iteratee function.
85
+ *
86
+ * @param {T} collection - The object to iterate over.
87
+ * @param {(accumulator: U, value: T[keyof T], key: string, collection: T) => U} iteratee - The function invoked per iteration.
88
+ * @param {U} initialValue - The initial value.
89
+ * @returns {U} - Returns the accumulated value.
90
+ *
91
+ * @example
92
+ * const obj = { a: 1, b: 2, c: 3 };
93
+ * reduce(obj, (acc, value) => acc + value % 2 === 0, true); // => false
94
+ */
95
+ declare function reduce<T extends object, U>(collection: T, iteratee: (accumulator: U, value: T[keyof T], key: keyof T, collection: T) => U, initialValue: U): U;
96
+ /**
97
+ * Reduces an object to a single value using an iteratee function.
98
+ *
99
+ * @param {T} collection - The object to iterate over.
100
+ * @param {(accumulator: T[keyof T], value: T[keyof T], key: keyof T, collection: T) => U} iteratee - The function invoked per iteration.
101
+ * @returns {T[keyof T]} - Returns the accumulated value.
102
+ *
103
+ * @example
104
+ * const obj = { a: 1, b: 2, c: 3 };
105
+ * reduce(obj, (acc, value) => acc + value); // => 6
106
+ */
107
+ declare function reduce<T extends object>(collection: T, iteratee: (accumulator: T[keyof T], value: T[keyof T], key: keyof T, collection: T) => T[keyof T]): T[keyof T];
108
+
109
+ export { reduce };
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Reduces an array to a single value using an iteratee function.
3
+ *
4
+ * The `reduce()` function goes through each element in an array and applies a special function (called a "reducer") to them, one by one.
5
+ * This function takes the result of the previous step and the current element to perform a calculation.
6
+ * After going through all the elements, the function gives you one final result.
7
+ *
8
+ * When the `reduce()` function starts, there's no previous result to use.
9
+ * If you provide an initial value, it starts with that.
10
+ * If not, it uses the first element of the array and begins with the second element for the calculation.
11
+ *
12
+ * @param {readonly T[]} collection - The collection to iterate over.
13
+ * @param {(accumulator: U, value: T, index: number, collection: readonly T[]) => U} iteratee - The function invoked per iteration.
14
+ * @param {U} initialValue - The initial value.
15
+ * @returns {U} - Returns the accumulated value.
16
+ *
17
+ * @example
18
+ * const arrayLike = [1, 2, 3];
19
+ * reduce(arrayLike, (acc, value) => acc && value % 2 === 0, true); // => false
20
+ */
21
+ declare function reduce<T, U>(collection: readonly T[], iteratee: (accumulator: U, value: T, index: number, collection: readonly T[]) => U, initialValue: U): U;
22
+ /**
23
+ * Reduces an array to a single value using an iteratee function.
24
+ *
25
+ * The `reduce()` function goes through each element in an array and applies a special function (called a "reducer") to them, one by one.
26
+ * This function takes the result of the previous step and the current element to perform a calculation.
27
+ * After going through all the elements, the function gives you one final result.
28
+ *
29
+ * When the `reduce()` function starts, there's no previous result to use.
30
+ * If you provide an initial value, it starts with that.
31
+ * If not, it uses the first element of the array and begins with the second element for the calculation.
32
+ *
33
+ * @param {readonly T[]} collection - The collection to iterate over.
34
+ * @param {(accumulator: T, value: T, index: number, collection: readonly T[]) => T} iteratee - The function invoked per iteration.
35
+ * @returns {T} - Returns the accumulated value.
36
+ *
37
+ * @example
38
+ * const arrayLike = [1, 2, 3];
39
+ * reduce(arrayLike, (acc, value) => acc + value); // => 6
40
+ */
41
+ declare function reduce<T>(collection: readonly T[], iteratee: (accumulator: T, value: T, index: number, collection: readonly T[]) => T): T;
42
+ /**
43
+ * Reduces an array to a single value using an iteratee function.
44
+ *
45
+ * The `reduce()` function goes through each element in an array and applies a special function (called a "reducer") to them, one by one.
46
+ * This function takes the result of the previous step and the current element to perform a calculation.
47
+ * After going through all the elements, the function gives you one final result.
48
+ *
49
+ * When the `reduce()` function starts, there's no previous result to use.
50
+ * If you provide an initial value, it starts with that.
51
+ * If not, it uses the first element of the array and begins with the second element for the calculation.
52
+ *
53
+ * @param {ArrayLike<T>} collection - The collection to iterate over.
54
+ * @param {(accumulator: U, value: T, index: number, collection: ArrayLike<T>) => U} iteratee - The function invoked per iteration.
55
+ * @param {U} initialValue - The initial value.
56
+ * @returns {U} - Returns the accumulated value.
57
+ *
58
+ * @example
59
+ * const arrayLike = {0: 1, 1: 2, 2: 3, length: 3};
60
+ * reduce(arrayLike, (acc, value) => acc + value % 2 === 0, true); // => false
61
+ */
62
+ declare function reduce<T, U>(collection: ArrayLike<T>, iteratee: (accumulator: U, value: T, index: number, collection: ArrayLike<T>) => U, initialValue: U): U;
63
+ /**
64
+ * Reduces an array to a single value using an iteratee function.
65
+ *
66
+ * The `reduce()` function goes through each element in an array and applies a special function (called a "reducer") to them, one by one.
67
+ * This function takes the result of the previous step and the current element to perform a calculation.
68
+ * After going through all the elements, the function gives you one final result.
69
+ *
70
+ * When the `reduce()` function starts, there's no previous result to use.
71
+ * If you provide an initial value, it starts with that.
72
+ * If not, it uses the first element of the array and begins with the second element for the calculation.
73
+ *
74
+ * @param {ArrayLike<T>} collection - The collection to iterate over.
75
+ * @param {(accumulator: U, value: T, index: number, collection: ArrayLike<T>) => U} iteratee - The function invoked per iteration.
76
+ * @returns {T} - Returns the accumulated value.
77
+ *
78
+ * @example
79
+ * const arrayLike = {0: 1, 1: 2, 2: 3, length: 3};
80
+ * reduce(arrayLike, (acc, value) => acc + value); // => 6
81
+ */
82
+ declare function reduce<T>(collection: ArrayLike<T>, iteratee: (accumulator: T, value: T, index: number, collection: ArrayLike<T>) => T): T;
83
+ /**
84
+ * Reduces an object to a single value using an iteratee function.
85
+ *
86
+ * @param {T} collection - The object to iterate over.
87
+ * @param {(accumulator: U, value: T[keyof T], key: string, collection: T) => U} iteratee - The function invoked per iteration.
88
+ * @param {U} initialValue - The initial value.
89
+ * @returns {U} - Returns the accumulated value.
90
+ *
91
+ * @example
92
+ * const obj = { a: 1, b: 2, c: 3 };
93
+ * reduce(obj, (acc, value) => acc + value % 2 === 0, true); // => false
94
+ */
95
+ declare function reduce<T extends object, U>(collection: T, iteratee: (accumulator: U, value: T[keyof T], key: keyof T, collection: T) => U, initialValue: U): U;
96
+ /**
97
+ * Reduces an object to a single value using an iteratee function.
98
+ *
99
+ * @param {T} collection - The object to iterate over.
100
+ * @param {(accumulator: T[keyof T], value: T[keyof T], key: keyof T, collection: T) => U} iteratee - The function invoked per iteration.
101
+ * @returns {T[keyof T]} - Returns the accumulated value.
102
+ *
103
+ * @example
104
+ * const obj = { a: 1, b: 2, c: 3 };
105
+ * reduce(obj, (acc, value) => acc + value); // => 6
106
+ */
107
+ declare function reduce<T extends object>(collection: T, iteratee: (accumulator: T[keyof T], value: T[keyof T], key: keyof T, collection: T) => T[keyof T]): T[keyof T];
108
+
109
+ export { reduce };
@@ -0,0 +1,33 @@
1
+ import { identity } from '../../function/identity.mjs';
2
+ import { range } from '../../math/range.mjs';
3
+ import { isArrayLike } from '../predicate/isArrayLike.mjs';
4
+
5
+ function reduce(collection, iteratee = identity, accumulator) {
6
+ if (!collection) {
7
+ return accumulator;
8
+ }
9
+ let keys;
10
+ let startIndex = 0;
11
+ if (isArrayLike(collection)) {
12
+ keys = range(0, collection.length);
13
+ if (accumulator == null && collection.length > 0) {
14
+ accumulator = collection[0];
15
+ startIndex += 1;
16
+ }
17
+ }
18
+ else {
19
+ keys = Object.keys(collection);
20
+ if (accumulator == null) {
21
+ accumulator = collection[keys[0]];
22
+ startIndex += 1;
23
+ }
24
+ }
25
+ for (let i = startIndex; i < keys.length; i++) {
26
+ const key = keys[i];
27
+ const value = collection[key];
28
+ accumulator = iteratee(accumulator, value, key, collection);
29
+ }
30
+ return accumulator;
31
+ }
32
+
33
+ export { reduce };
@@ -105,6 +105,7 @@ export { orderBy } from './array/orderBy.mjs';
105
105
  export { pull } from './array/pull.mjs';
106
106
  export { pullAll } from './array/pullAll.mjs';
107
107
  export { pullAllBy } from './array/pullAllBy.mjs';
108
+ export { reduce } from './array/reduce.mjs';
108
109
  export { remove } from './array/remove.mjs';
109
110
  export { reverse } from './array/reverse.mjs';
110
111
  export { sample } from './array/sample.mjs';
@@ -105,6 +105,7 @@ export { orderBy } from './array/orderBy.js';
105
105
  export { pull } from './array/pull.js';
106
106
  export { pullAll } from './array/pullAll.js';
107
107
  export { pullAllBy } from './array/pullAllBy.js';
108
+ export { reduce } from './array/reduce.js';
108
109
  export { remove } from './array/remove.js';
109
110
  export { reverse } from './array/reverse.js';
110
111
  export { sample } from './array/sample.js';
@@ -1250,6 +1250,34 @@ function pullAllBy(arr, valuesToRemove, _getValue) {
1250
1250
  return arr;
1251
1251
  }
1252
1252
 
1253
+ function reduce(collection, iteratee = unary.identity, accumulator) {
1254
+ if (!collection) {
1255
+ return accumulator;
1256
+ }
1257
+ let keys;
1258
+ let startIndex = 0;
1259
+ if (isArrayLike(collection)) {
1260
+ keys = range$1.range(0, collection.length);
1261
+ if (accumulator == null && collection.length > 0) {
1262
+ accumulator = collection[0];
1263
+ startIndex += 1;
1264
+ }
1265
+ }
1266
+ else {
1267
+ keys = Object.keys(collection);
1268
+ if (accumulator == null) {
1269
+ accumulator = collection[keys[0]];
1270
+ startIndex += 1;
1271
+ }
1272
+ }
1273
+ for (let i = startIndex; i < keys.length; i++) {
1274
+ const key = keys[i];
1275
+ const value = collection[key];
1276
+ accumulator = iteratee(accumulator, value, key, collection);
1277
+ }
1278
+ return accumulator;
1279
+ }
1280
+
1253
1281
  function remove(arr, shouldRemoveElement) {
1254
1282
  return zipWith.remove(arr, iteratee(shouldRemoveElement));
1255
1283
  }
@@ -2173,11 +2201,16 @@ function sumBy(array, iteratee$1) {
2173
2201
  if (iteratee$1 != null) {
2174
2202
  iteratee$1 = iteratee(iteratee$1);
2175
2203
  }
2176
- let result = iteratee$1 ? iteratee$1(array[0]) : array[0];
2177
- for (let i = 1; i < array.length; i++) {
2204
+ let result = undefined;
2205
+ for (let i = 0; i < array.length; i++) {
2178
2206
  const current = iteratee$1 ? iteratee$1(array[i]) : array[i];
2179
2207
  if (current !== undefined) {
2180
- result += current;
2208
+ if (result === undefined) {
2209
+ result = current;
2210
+ }
2211
+ else {
2212
+ result += current;
2213
+ }
2181
2214
  }
2182
2215
  }
2183
2216
  return result;
@@ -3429,6 +3462,7 @@ exports.random = random;
3429
3462
  exports.range = range;
3430
3463
  exports.rangeRight = rangeRight;
3431
3464
  exports.rearg = rearg;
3465
+ exports.reduce = reduce;
3432
3466
  exports.remove = remove;
3433
3467
  exports.repeat = repeat;
3434
3468
  exports.replace = replace;
@@ -107,6 +107,7 @@ export { orderBy } from './array/orderBy.mjs';
107
107
  export { pull } from './array/pull.mjs';
108
108
  export { pullAll } from './array/pullAll.mjs';
109
109
  export { pullAllBy } from './array/pullAllBy.mjs';
110
+ export { reduce } from './array/reduce.mjs';
110
111
  export { remove } from './array/remove.mjs';
111
112
  export { reverse } from './array/reverse.mjs';
112
113
  export { sample } from './array/sample.mjs';
@@ -7,11 +7,16 @@ function sumBy(array, iteratee$1) {
7
7
  if (iteratee$1 != null) {
8
8
  iteratee$1 = iteratee(iteratee$1);
9
9
  }
10
- let result = iteratee$1 ? iteratee$1(array[0]) : array[0];
11
- for (let i = 1; i < array.length; i++) {
10
+ let result = undefined;
11
+ for (let i = 0; i < array.length; i++) {
12
12
  const current = iteratee$1 ? iteratee$1(array[i]) : array[i];
13
13
  if (current !== undefined) {
14
- result += current;
14
+ if (result === undefined) {
15
+ result = current;
16
+ }
17
+ else {
18
+ result += current;
19
+ }
15
20
  }
16
21
  }
17
22
  return result;
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.32.0-dev.1007+9bc5fa98",
4
+ "version": "1.32.0-dev.1009+f0e254af",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {