Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

array-group-to-map.js 1.1KB

12345678910111213141516171819202122232425262728293031
  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. var Map = MapHelpers.Map;
  9. var mapGet = MapHelpers.get;
  10. var mapHas = MapHelpers.has;
  11. var mapSet = MapHelpers.set;
  12. var push = uncurryThis([].push);
  13. // `Array.prototype.groupToMap` method
  14. // https://github.com/tc39/proposal-array-grouping
  15. module.exports = function groupToMap(callbackfn /* , thisArg */) {
  16. var O = toObject(this);
  17. var self = IndexedObject(O);
  18. var boundFunction = bind(callbackfn, arguments.length > 1 ? arguments[1] : undefined);
  19. var map = new Map();
  20. var length = lengthOfArrayLike(self);
  21. var index = 0;
  22. var key, value;
  23. for (;length > index; index++) {
  24. value = self[index];
  25. key = boundFunction(value, index, O);
  26. if (mapHas(map, key)) push(mapGet(map, key), value);
  27. else mapSet(map, key, [value]);
  28. } return map;
  29. };