UNPKG

1.13 kBJavaScriptView Raw
1'use strict';
2var bind = require('../internals/function-bind-context');
3var uncurryThis = require('../internals/function-uncurry-this');
4var IndexedObject = require('../internals/indexed-object');
5var toObject = require('../internals/to-object');
6var lengthOfArrayLike = require('../internals/length-of-array-like');
7var MapHelpers = require('../internals/map-helpers');
8
9var Map = MapHelpers.Map;
10var mapGet = MapHelpers.get;
11var mapHas = MapHelpers.has;
12var mapSet = MapHelpers.set;
13var push = uncurryThis([].push);
14
15// `Array.prototype.groupToMap` method
16// https://github.com/tc39/proposal-array-grouping
17module.exports = function groupToMap(callbackfn /* , thisArg */) {
18 var O = toObject(this);
19 var self = IndexedObject(O);
20 var boundFunction = bind(callbackfn, arguments.length > 1 ? arguments[1] : undefined);
21 var map = new Map();
22 var length = lengthOfArrayLike(self);
23 var index = 0;
24 var key, value;
25 for (;length > index; index++) {
26 value = self[index];
27 key = boundFunction(value, index, O);
28 if (mapHas(map, key)) push(mapGet(map, key), value);
29 else mapSet(map, key, [value]);
30 } return map;
31};