|
Last change
on this file since a762898 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
1.1 KB
|
| Line | |
|---|
| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|---|
| 4 |
|
|---|
| 5 | const identity = require('../../function/identity.js');
|
|---|
| 6 | const range = require('../../math/range.js');
|
|---|
| 7 | const isArrayLike = require('../predicate/isArrayLike.js');
|
|---|
| 8 |
|
|---|
| 9 | function reduce(collection, iteratee = identity.identity, accumulator) {
|
|---|
| 10 | if (!collection) {
|
|---|
| 11 | return accumulator;
|
|---|
| 12 | }
|
|---|
| 13 | let keys;
|
|---|
| 14 | let startIndex = 0;
|
|---|
| 15 | if (isArrayLike.isArrayLike(collection)) {
|
|---|
| 16 | keys = range.range(0, collection.length);
|
|---|
| 17 | if (accumulator == null && collection.length > 0) {
|
|---|
| 18 | accumulator = collection[0];
|
|---|
| 19 | startIndex += 1;
|
|---|
| 20 | }
|
|---|
| 21 | }
|
|---|
| 22 | else {
|
|---|
| 23 | keys = Object.keys(collection);
|
|---|
| 24 | if (accumulator == null) {
|
|---|
| 25 | accumulator = collection[keys[0]];
|
|---|
| 26 | startIndex += 1;
|
|---|
| 27 | }
|
|---|
| 28 | }
|
|---|
| 29 | for (let i = startIndex; i < keys.length; i++) {
|
|---|
| 30 | const key = keys[i];
|
|---|
| 31 | const value = collection[key];
|
|---|
| 32 | accumulator = iteratee(accumulator, value, key, collection);
|
|---|
| 33 | }
|
|---|
| 34 | return accumulator;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | exports.reduce = reduce;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.