Last change
on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
755 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 | var $ = require('../internals/export');
|
---|
| 3 | var iterate = require('../internals/iterate');
|
---|
| 4 | var aFunction = require('../internals/a-function');
|
---|
| 5 |
|
---|
| 6 | // `Map.groupBy` method
|
---|
| 7 | // https://github.com/tc39/proposal-collection-methods
|
---|
| 8 | $({ target: 'Map', stat: true }, {
|
---|
| 9 | groupBy: function groupBy(iterable, keyDerivative) {
|
---|
| 10 | var newMap = new this();
|
---|
| 11 | aFunction(keyDerivative);
|
---|
| 12 | var has = aFunction(newMap.has);
|
---|
| 13 | var get = aFunction(newMap.get);
|
---|
| 14 | var set = aFunction(newMap.set);
|
---|
| 15 | iterate(iterable, function (element) {
|
---|
| 16 | var derivedKey = keyDerivative(element);
|
---|
| 17 | if (!has.call(newMap, derivedKey)) set.call(newMap, derivedKey, [element]);
|
---|
| 18 | else get.call(newMap, derivedKey).push(element);
|
---|
| 19 | });
|
---|
| 20 | return newMap;
|
---|
| 21 | }
|
---|
| 22 | });
|
---|
Note:
See
TracBrowser
for help on using the repository browser.