1 var global = require('../internals/global');
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');
7 var TypeError = global.TypeError;
9 // `Array.prototype.{ reduce, reduceRight }` methods implementation
10 var createMethod = function (IS_RIGHT) {
11 return function (that, callbackfn, argumentsLength, memo) {
12 aCallable(callbackfn);
13 var O = toObject(that);
14 var self = IndexedObject(O);
15 var length = lengthOfArrayLike(O);
16 var index = IS_RIGHT ? length - 1 : 0;
17 var i = IS_RIGHT ? -1 : 1;
18 if (argumentsLength < 2) while (true) {
25 if (IS_RIGHT ? index < 0 : length <= index) {
26 throw TypeError('Reduce of empty array with no initial value');
29 for (;IS_RIGHT ? index >= 0 : length > index; index += i) if (index in self) {
30 memo = callbackfn(memo, self[index], index, O);
37 // `Array.prototype.reduce` method
38 // https://tc39.es/ecma262/#sec-array.prototype.reduce
39 left: createMethod(false),
40 // `Array.prototype.reduceRight` method
41 // https://tc39.es/ecma262/#sec-array.prototype.reduceright
42 right: createMethod(true)