es-toolkit 1.32.0 → 1.33.0

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 (66) hide show
  1. package/CHANGELOG.md +11 -1
  2. package/dist/_chunk/{isWeakSet-DoHqUM.js → isWeakSet-TIM260.js} +14 -0
  3. package/dist/_chunk/{toMerged-BQTfB8.js → toMerged-CwnQF6.js} +0 -14
  4. package/dist/_chunk/{upperFirst-CorAVn.js → upperFirst-nA5L7X.js} +5 -0
  5. package/dist/browser.global.js +1 -1
  6. package/dist/browser.global.js.map +1 -1
  7. package/dist/compat/array/find.mjs +12 -32
  8. package/dist/compat/array/findLast.d.mts +130 -0
  9. package/dist/compat/array/findLast.d.ts +130 -0
  10. package/dist/compat/array/findLast.mjs +32 -0
  11. package/dist/compat/array/intersectionWith.d.mts +96 -0
  12. package/dist/compat/array/intersectionWith.d.ts +96 -0
  13. package/dist/compat/array/intersectionWith.mjs +43 -0
  14. package/dist/compat/array/pullAllBy.d.mts +64 -0
  15. package/dist/compat/array/pullAllBy.d.ts +64 -0
  16. package/dist/compat/array/pullAllBy.mjs +22 -0
  17. package/dist/compat/array/reduce.d.mts +109 -0
  18. package/dist/compat/array/reduce.d.ts +109 -0
  19. package/dist/compat/array/reduce.mjs +33 -0
  20. package/dist/compat/array/reduceRight.d.mts +109 -0
  21. package/dist/compat/array/reduceRight.d.ts +109 -0
  22. package/dist/compat/array/reduceRight.mjs +39 -0
  23. package/dist/compat/index.d.mts +12 -3
  24. package/dist/compat/index.d.ts +12 -3
  25. package/dist/compat/index.js +264 -63
  26. package/dist/compat/index.mjs +12 -3
  27. package/dist/compat/math/divide.d.mts +18 -0
  28. package/dist/compat/math/divide.d.ts +18 -0
  29. package/dist/compat/math/divide.mjs +23 -0
  30. package/dist/compat/math/maxBy.d.mts +31 -0
  31. package/dist/compat/math/maxBy.d.ts +31 -0
  32. package/dist/compat/math/maxBy.mjs +11 -0
  33. package/dist/compat/math/multiply.mjs +4 -3
  34. package/dist/compat/math/sumBy.mjs +8 -3
  35. package/dist/compat/object/assignIn.mjs +1 -1
  36. package/dist/compat/object/pickBy.d.mts +22 -0
  37. package/dist/compat/object/pickBy.d.ts +22 -0
  38. package/dist/compat/object/pickBy.mjs +23 -0
  39. package/dist/compat/object/values.d.mts +32 -0
  40. package/dist/compat/object/values.d.ts +32 -0
  41. package/dist/compat/object/values.mjs +5 -0
  42. package/dist/compat/object/valuesIn.d.mts +63 -0
  43. package/dist/compat/object/valuesIn.d.ts +63 -0
  44. package/dist/compat/object/valuesIn.mjs +13 -0
  45. package/dist/index.d.mts +2 -0
  46. package/dist/index.d.ts +2 -0
  47. package/dist/index.js +6 -4
  48. package/dist/index.mjs +2 -0
  49. package/dist/object/flattenObject.d.mts +1 -1
  50. package/dist/object/flattenObject.d.ts +1 -1
  51. package/dist/object/index.js +15 -2
  52. package/dist/predicate/index.d.mts +1 -0
  53. package/dist/predicate/index.d.ts +1 -0
  54. package/dist/predicate/index.js +2 -1
  55. package/dist/predicate/index.mjs +1 -0
  56. package/dist/predicate/isJSON.d.mts +31 -0
  57. package/dist/predicate/isJSON.d.ts +31 -0
  58. package/dist/predicate/isJSON.mjs +14 -0
  59. package/dist/string/index.d.mts +1 -0
  60. package/dist/string/index.d.ts +1 -0
  61. package/dist/string/index.js +2 -1
  62. package/dist/string/index.mjs +1 -0
  63. package/dist/string/reverseString.d.mts +16 -0
  64. package/dist/string/reverseString.d.ts +16 -0
  65. package/dist/string/reverseString.mjs +5 -0
  66. package/package.json +2 -1
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Removes all specified values from an array using an iteratee function.
3
+ *
4
+ * This function changes `arr` in place.
5
+ * If you want to remove values without modifying the original array, use `differenceBy`.
6
+ *
7
+ * @template T
8
+ * @param {T[]} arr - The array to modify.
9
+ * @param {ArrayLike<T>} valuesToRemove - The values to remove from the array.
10
+ * @param {(value: T) => unknown} getValue - The iteratee invoked per element.
11
+ * @returns {T[]} The modified array with the specified values removed.
12
+ *
13
+ * @example
14
+ * const items = [{ value: 1 }, { value: 2 }, { value: 3 }, { value: 1 }];
15
+ * const result = pullAllBy(items, [{ value: 1 }, { value: 3 }], obj => obj.value);
16
+ * console.log(result); // [{ value: 2 }]
17
+ */
18
+ declare function pullAllBy<T>(arr: T[], valuesToRemove: ArrayLike<T>, getValue: (value: T) => unknown): T[];
19
+ /**
20
+ * Removes all specified values from an array using an iteratee function.
21
+ *
22
+ * This function changes `arr` in place.
23
+ * If you want to remove values without modifying the original array, use `differenceBy`.
24
+ *
25
+ * @template T
26
+ * @param {T[]} arr - The array to modify.
27
+ * @param {ArrayLike<T>} valuesToRemove - The values to remove from the array.
28
+ * @param {Partial<T>} getValue - The partial object to match against each element.
29
+ * @returns {T[]} The modified array with the specified values removed.
30
+ */
31
+ declare function pullAllBy<T>(arr: T[], valuesToRemove: ArrayLike<T>, getValue: Partial<T>): T[];
32
+ /**
33
+ * Removes all specified values from an array using an iteratee function.
34
+ *
35
+ * This function changes `arr` in place.
36
+ * If you want to remove values without modifying the original array, use `differenceBy`.
37
+ *
38
+ * @template T
39
+ * @param {T[]} arr - The array to modify.
40
+ * @param {ArrayLike<T>} valuesToRemove - The values to remove from the array.
41
+ * @param {[keyof T, unknown]} getValue - The property-value pair to match against each element.
42
+ * @returns {T[]} The modified array with the specified values removed.
43
+ */
44
+ declare function pullAllBy<T>(arr: T[], valuesToRemove: ArrayLike<T>, getValue: [keyof T, unknown]): T[];
45
+ /**
46
+ * Removes all specified values from an array using an iteratee function.
47
+ *
48
+ * This function changes `arr` in place.
49
+ * If you want to remove values without modifying the original array, use `differenceBy`.
50
+ *
51
+ * @template T
52
+ * @param {T[]} arr - The array to modify.
53
+ * @param {ArrayLike<T>} valuesToRemove - The values to remove from the array.
54
+ * @param {keyof T} getValue - The key of the property to match against each element.
55
+ * @returns {T[]} The modified array with the specified values removed.
56
+ *
57
+ * @example
58
+ * const items = [{ value: 1 }, { value: 2 }, { value: 3 }, { value: 1 }];
59
+ * const result = pullAllBy(items, [{ value: 1 }, { value: 3 }], 'value');
60
+ * console.log(result); // [{ value: 2 }]
61
+ */
62
+ declare function pullAllBy<T>(arr: T[], valuesToRemove: ArrayLike<T>, getValue: keyof T): T[];
63
+
64
+ export { pullAllBy };
@@ -0,0 +1,22 @@
1
+ import { iteratee } from '../util/iteratee.mjs';
2
+
3
+ function pullAllBy(arr, valuesToRemove, _getValue) {
4
+ const getValue = iteratee(_getValue);
5
+ const valuesSet = new Set(Array.from(valuesToRemove).map(x => getValue(x)));
6
+ let resultIndex = 0;
7
+ for (let i = 0; i < arr.length; i++) {
8
+ const value = getValue(arr[i]);
9
+ if (valuesSet.has(value)) {
10
+ continue;
11
+ }
12
+ if (!Object.hasOwn(arr, i)) {
13
+ delete arr[resultIndex++];
14
+ continue;
15
+ }
16
+ arr[resultIndex++] = arr[i];
17
+ }
18
+ arr.length = resultIndex;
19
+ return arr;
20
+ }
21
+
22
+ export { pullAllBy };
@@ -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 };
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Reduces an array to a single value using an iteratee function, starting from the right.
3
+ *
4
+ * The `reduceRight()` function goes through each element in an array from right to left 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 `reduceRight()` 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 last element of the array and begins with the second to last 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
+ * reduceRight(arrayLike, (acc, value) => acc && value % 2 === 0, true); // => false
20
+ */
21
+ declare function reduceRight<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, starting from the right.
24
+ *
25
+ * The `reduceRight()` function goes through each element in an array from right to left 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 `reduceRight()` 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 last element of the array and begins with the second to last 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
+ * reduceRight(arrayLike, (acc, value) => acc + value); // => 6
40
+ */
41
+ declare function reduceRight<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, starting from the right.
44
+ *
45
+ * The `reduceRight()` function goes through each element in an array from right to left 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 `reduceRight()` 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 last element of the array and begins with the second to last 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
+ * reduceRight(arrayLike, (acc, value) => acc + value % 2 === 0, true); // => false
61
+ */
62
+ declare function reduceRight<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, starting from the right.
65
+ *
66
+ * The `reduceRight()` function goes through each element in an array from right to left 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 `reduceRight()` 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 last element of the array and begins with the second to last 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
+ * reduceRight(arrayLike, (acc, value) => acc + value); // => 6
81
+ */
82
+ declare function reduceRight<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, starting from the right.
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
+ * reduceRight(obj, (acc, value) => acc + value % 2 === 0, true); // => false
94
+ */
95
+ declare function reduceRight<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, starting from the right.
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
+ * reduceRight(obj, (acc, value) => acc + value); // => 6
106
+ */
107
+ declare function reduceRight<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 { reduceRight };
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Reduces an array to a single value using an iteratee function, starting from the right.
3
+ *
4
+ * The `reduceRight()` function goes through each element in an array from right to left 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 `reduceRight()` 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 last element of the array and begins with the second to last 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
+ * reduceRight(arrayLike, (acc, value) => acc && value % 2 === 0, true); // => false
20
+ */
21
+ declare function reduceRight<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, starting from the right.
24
+ *
25
+ * The `reduceRight()` function goes through each element in an array from right to left 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 `reduceRight()` 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 last element of the array and begins with the second to last 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
+ * reduceRight(arrayLike, (acc, value) => acc + value); // => 6
40
+ */
41
+ declare function reduceRight<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, starting from the right.
44
+ *
45
+ * The `reduceRight()` function goes through each element in an array from right to left 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 `reduceRight()` 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 last element of the array and begins with the second to last 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
+ * reduceRight(arrayLike, (acc, value) => acc + value % 2 === 0, true); // => false
61
+ */
62
+ declare function reduceRight<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, starting from the right.
65
+ *
66
+ * The `reduceRight()` function goes through each element in an array from right to left 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 `reduceRight()` 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 last element of the array and begins with the second to last 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
+ * reduceRight(arrayLike, (acc, value) => acc + value); // => 6
81
+ */
82
+ declare function reduceRight<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, starting from the right.
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
+ * reduceRight(obj, (acc, value) => acc + value % 2 === 0, true); // => false
94
+ */
95
+ declare function reduceRight<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, starting from the right.
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
+ * reduceRight(obj, (acc, value) => acc + value); // => 6
106
+ */
107
+ declare function reduceRight<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 { reduceRight };
@@ -0,0 +1,39 @@
1
+ import { identity } from '../../function/identity.mjs';
2
+ import { range } from '../../math/range.mjs';
3
+ import { isArrayLike } from '../predicate/isArrayLike.mjs';
4
+
5
+ function reduceRight(collection, iteratee = identity, accumulator) {
6
+ if (!collection) {
7
+ return accumulator;
8
+ }
9
+ let keys;
10
+ let startIndex;
11
+ if (isArrayLike(collection)) {
12
+ keys = range(0, collection.length).reverse();
13
+ if (accumulator == null && collection.length > 0) {
14
+ accumulator = collection[collection.length - 1];
15
+ startIndex = 1;
16
+ }
17
+ else {
18
+ startIndex = 0;
19
+ }
20
+ }
21
+ else {
22
+ keys = Object.keys(collection).reverse();
23
+ if (accumulator == null) {
24
+ accumulator = collection[keys[0]];
25
+ startIndex = 1;
26
+ }
27
+ else {
28
+ startIndex = 0;
29
+ }
30
+ }
31
+ for (let i = startIndex; i < keys.length; i++) {
32
+ const key = keys[i];
33
+ const value = collection[key];
34
+ accumulator = iteratee(accumulator, value, key, collection);
35
+ }
36
+ return accumulator;
37
+ }
38
+
39
+ export { reduceRight };