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.

es.object.group-by.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var getBuiltIn = require('../internals/get-built-in');
  4. var uncurryThis = require('../internals/function-uncurry-this');
  5. var aCallable = require('../internals/a-callable');
  6. var requireObjectCoercible = require('../internals/require-object-coercible');
  7. var toPropertyKey = require('../internals/to-property-key');
  8. var iterate = require('../internals/iterate');
  9. var fails = require('../internals/fails');
  10. // eslint-disable-next-line es/no-object-groupby -- testing
  11. var nativeGroupBy = Object.groupBy;
  12. var create = getBuiltIn('Object', 'create');
  13. var push = uncurryThis([].push);
  14. var DOES_NOT_WORK_WITH_PRIMITIVES = !nativeGroupBy || fails(function () {
  15. return nativeGroupBy('ab', function (it) {
  16. return it;
  17. }).a.length !== 1;
  18. });
  19. // `Object.groupBy` method
  20. // https://github.com/tc39/proposal-array-grouping
  21. $({ target: 'Object', stat: true, forced: DOES_NOT_WORK_WITH_PRIMITIVES }, {
  22. groupBy: function groupBy(items, callbackfn) {
  23. requireObjectCoercible(items);
  24. aCallable(callbackfn);
  25. var obj = create(null);
  26. var k = 0;
  27. iterate(items, function (value) {
  28. var key = toPropertyKey(callbackfn(value, k++));
  29. // in some IE versions, `hasOwnProperty` returns incorrect result on integer keys
  30. // but since it's a `null` prototype object, we can safely use `in`
  31. if (key in obj) push(obj[key], value);
  32. else obj[key] = [value];
  33. });
  34. return obj;
  35. }
  36. });