|
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 | 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 };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.