array-group.js 1.5 KB

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