1 | 'use strict';
|
2 | var aCallable = require('../internals/a-callable');
|
3 | var toObject = require('../internals/to-object');
|
4 | var IndexedObject = require('../internals/indexed-object');
|
5 | var lengthOfArrayLike = require('../internals/length-of-array-like');
|
6 |
|
7 | var $TypeError = TypeError;
|
8 |
|
9 | var REDUCE_EMPTY = 'Reduce of empty array with no initial value';
|
10 |
|
11 |
|
12 | var createMethod = function (IS_RIGHT) {
|
13 | return function (that, callbackfn, argumentsLength, memo) {
|
14 | var O = toObject(that);
|
15 | var self = IndexedObject(O);
|
16 | var length = lengthOfArrayLike(O);
|
17 | aCallable(callbackfn);
|
18 | if (length === 0 && argumentsLength < 2) throw new $TypeError(REDUCE_EMPTY);
|
19 | var index = IS_RIGHT ? length - 1 : 0;
|
20 | var i = IS_RIGHT ? -1 : 1;
|
21 | if (argumentsLength < 2) while (true) {
|
22 | if (index in self) {
|
23 | memo = self[index];
|
24 | index += i;
|
25 | break;
|
26 | }
|
27 | index += i;
|
28 | if (IS_RIGHT ? index < 0 : length <= index) {
|
29 | throw new $TypeError(REDUCE_EMPTY);
|
30 | }
|
31 | }
|
32 | for (;IS_RIGHT ? index >= 0 : length > index; index += i) if (index in self) {
|
33 | memo = callbackfn(memo, self[index], index, O);
|
34 | }
|
35 | return memo;
|
36 | };
|
37 | };
|
38 |
|
39 | module.exports = {
|
40 |
|
41 |
|
42 | left: createMethod(false),
|
43 |
|
44 |
|
45 | right: createMethod(true)
|
46 | };
|