UNPKG

928 BJavaScriptView Raw
1'use strict';
2var $ = require('../internals/export');
3var aCallable = require('../internals/a-callable');
4var aMap = require('../internals/a-map');
5var iterate = require('../internals/map-iterate');
6
7var $TypeError = TypeError;
8
9// `Map.prototype.reduce` method
10// https://github.com/tc39/proposal-collection-methods
11$({ target: 'Map', proto: true, real: true, forced: true }, {
12 reduce: function reduce(callbackfn /* , initialValue */) {
13 var map = aMap(this);
14 var noInitial = arguments.length < 2;
15 var accumulator = noInitial ? undefined : arguments[1];
16 aCallable(callbackfn);
17 iterate(map, function (value, key) {
18 if (noInitial) {
19 noInitial = false;
20 accumulator = value;
21 } else {
22 accumulator = callbackfn(accumulator, value, key, map);
23 }
24 });
25 if (noInitial) throw new $TypeError('Reduce of empty map with no initial value');
26 return accumulator;
27 }
28});