1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
4 | var aCallable = require('../internals/a-callable');
|
---|
5 | var requireObjectCoercible = require('../internals/require-object-coercible');
|
---|
6 | var iterate = require('../internals/iterate');
|
---|
7 | var MapHelpers = require('../internals/map-helpers');
|
---|
8 | var IS_PURE = require('../internals/is-pure');
|
---|
9 | var fails = require('../internals/fails');
|
---|
10 |
|
---|
11 | var Map = MapHelpers.Map;
|
---|
12 | var has = MapHelpers.has;
|
---|
13 | var get = MapHelpers.get;
|
---|
14 | var set = MapHelpers.set;
|
---|
15 | var push = uncurryThis([].push);
|
---|
16 |
|
---|
17 | var DOES_NOT_WORK_WITH_PRIMITIVES = IS_PURE || fails(function () {
|
---|
18 | return Map.groupBy('ab', function (it) {
|
---|
19 | return it;
|
---|
20 | }).get('a').length !== 1;
|
---|
21 | });
|
---|
22 |
|
---|
23 | // `Map.groupBy` method
|
---|
24 | // https://tc39.es/ecma262/#sec-map.groupby
|
---|
25 | $({ target: 'Map', stat: true, forced: IS_PURE || DOES_NOT_WORK_WITH_PRIMITIVES }, {
|
---|
26 | groupBy: function groupBy(items, callbackfn) {
|
---|
27 | requireObjectCoercible(items);
|
---|
28 | aCallable(callbackfn);
|
---|
29 | var map = new Map();
|
---|
30 | var k = 0;
|
---|
31 | iterate(items, function (value) {
|
---|
32 | var key = callbackfn(value, k++);
|
---|
33 | if (!has(map, key)) set(map, key, [value]);
|
---|
34 | else push(get(map, key), value);
|
---|
35 | });
|
---|
36 | return map;
|
---|
37 | }
|
---|
38 | });
|
---|