array-group-by.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. var global = require('../internals/global');
  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 toPropertyKey = require('../internals/to-property-key');
  7. var lengthOfArrayLike = require('../internals/length-of-array-like');
  8. var objectCreate = require('../internals/object-create');
  9. var arrayFromConstructorAndList = require('../internals/array-from-constructor-and-list');
  10. var Array = global.Array;
  11. var push = uncurryThis([].push);
  12. module.exports = function ($this, callbackfn, that, specificConstructor) {
  13. var O = toObject($this);
  14. var self = IndexedObject(O);
  15. var boundFunction = bind(callbackfn, that);
  16. var target = objectCreate(null);
  17. var length = lengthOfArrayLike(self);
  18. var index = 0;
  19. var Constructor, key, value;
  20. for (;length > index; index++) {
  21. value = self[index];
  22. key = toPropertyKey(boundFunction(value, index, O));
  23. // in some IE10 builds, `hasOwnProperty` returns incorrect result on integer keys
  24. // but since it's a `null` prototype object, we can safely use `in`
  25. if (key in target) push(target[key], value);
  26. else target[key] = [value];
  27. }
  28. // TODO: Remove this block from `core-js@4`
  29. if (specificConstructor) {
  30. Constructor = specificConstructor(O);
  31. if (Constructor !== Array) {
  32. for (key in target) target[key] = arrayFromConstructorAndList(Constructor, target[key]);
  33. }
  34. } return target;
  35. };