[79a0317] | 1 | 'use strict';
|
---|
| 2 | var bind = require('../internals/function-bind-context');
|
---|
| 3 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
| 4 | var IndexedObject = require('../internals/indexed-object');
|
---|
| 5 | var toObject = require('../internals/to-object');
|
---|
| 6 | var lengthOfArrayLike = require('../internals/length-of-array-like');
|
---|
| 7 | var MapHelpers = require('../internals/map-helpers');
|
---|
| 8 |
|
---|
| 9 | var Map = MapHelpers.Map;
|
---|
| 10 | var mapGet = MapHelpers.get;
|
---|
| 11 | var mapHas = MapHelpers.has;
|
---|
| 12 | var mapSet = MapHelpers.set;
|
---|
| 13 | var push = uncurryThis([].push);
|
---|
| 14 |
|
---|
| 15 | // `Array.prototype.groupToMap` method
|
---|
| 16 | // https://github.com/tc39/proposal-array-grouping
|
---|
| 17 | module.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 | };
|
---|