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.
40 lines
1.4 KiB
40 lines
1.4 KiB
1 month ago
|
'use strict';
|
||
|
var $ = require('../internals/export');
|
||
|
var getBuiltIn = require('../internals/get-built-in');
|
||
|
var uncurryThis = require('../internals/function-uncurry-this');
|
||
|
var aCallable = require('../internals/a-callable');
|
||
|
var requireObjectCoercible = require('../internals/require-object-coercible');
|
||
|
var toPropertyKey = require('../internals/to-property-key');
|
||
|
var iterate = require('../internals/iterate');
|
||
|
var fails = require('../internals/fails');
|
||
|
|
||
|
// eslint-disable-next-line es/no-object-groupby -- testing
|
||
|
var nativeGroupBy = Object.groupBy;
|
||
|
var create = getBuiltIn('Object', 'create');
|
||
|
var push = uncurryThis([].push);
|
||
|
|
||
|
var DOES_NOT_WORK_WITH_PRIMITIVES = !nativeGroupBy || fails(function () {
|
||
|
return nativeGroupBy('ab', function (it) {
|
||
|
return it;
|
||
|
}).a.length !== 1;
|
||
|
});
|
||
|
|
||
|
// `Object.groupBy` method
|
||
|
// https://github.com/tc39/proposal-array-grouping
|
||
|
$({ target: 'Object', stat: true, forced: DOES_NOT_WORK_WITH_PRIMITIVES }, {
|
||
|
groupBy: function groupBy(items, callbackfn) {
|
||
|
requireObjectCoercible(items);
|
||
|
aCallable(callbackfn);
|
||
|
var obj = create(null);
|
||
|
var k = 0;
|
||
|
iterate(items, function (value) {
|
||
|
var key = toPropertyKey(callbackfn(value, k++));
|
||
|
// in some IE versions, `hasOwnProperty` returns incorrect result on integer keys
|
||
|
// but since it's a `null` prototype object, we can safely use `in`
|
||
|
if (key in obj) push(obj[key], value);
|
||
|
else obj[key] = [value];
|
||
|
});
|
||
|
return obj;
|
||
|
}
|
||
|
});
|