esnext.map.group-by.js 1014 B

12345678910111213141516171819202122232425262728293031
  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 Map = MapHelpers.Map;
  10. var has = MapHelpers.has;
  11. var get = MapHelpers.get;
  12. var set = MapHelpers.set;
  13. var push = uncurryThis([].push);
  14. // `Map.groupBy` method
  15. // https://github.com/tc39/proposal-array-grouping
  16. $({ target: 'Map', stat: true, forced: IS_PURE }, {
  17. groupBy: function groupBy(items, callbackfn) {
  18. requireObjectCoercible(items);
  19. aCallable(callbackfn);
  20. var map = new Map();
  21. var k = 0;
  22. iterate(items, function (value) {
  23. var key = callbackfn(value, k++);
  24. if (!has(map, key)) set(map, key, [value]);
  25. else push(get(map, key), value);
  26. });
  27. return map;
  28. }
  29. });