source: node_modules/es-toolkit/dist/compat/array/reduce.mjs

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 967 bytes
RevLine 
[a762898]1import { identity } from '../../function/identity.mjs';
2import { range } from '../../math/range.mjs';
3import { isArrayLike } from '../predicate/isArrayLike.mjs';
4
5function 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
33export { reduce };
Note: See TracBrowser for help on using the repository browser.