Skip to content

Commit

Permalink
adapt to the actual spec draft
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jul 29, 2021
1 parent db449ca commit 6245936
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
20 changes: 13 additions & 7 deletions packages/core-js/internals/array-group-by.js
Expand Up @@ -3,24 +3,30 @@ var has = require('../internals/has');
var IndexedObject = require('../internals/indexed-object');
var toObject = require('../internals/to-object');
var toLength = require('../internals/to-length');
var toPrimitive = require('../internals/to-primitive');
var toPropertyKey = require('../internals/to-property-key');
var objectCreate = require('../internals/object-create');
var arrayFromConstructorAndList = require('../internals/array-from-constructor-and-list');

var push = [].push;

module.exports = function ($this, callbackfn, that, specificCreate) {
module.exports = function ($this, callbackfn, that, specificConstructor) {
var O = toObject($this);
var self = IndexedObject(O);
var boundFunction = bind(callbackfn, that, 3);
var target = objectCreate(null);
var length = toLength(self.length);
var index = 0;
var value, result, array;
var Constructor, key, value;
for (;length > index; index++) {
value = self[index];
result = toPrimitive(boundFunction(value, index, O), 'string');
if (has(target, result)) array = target[result];
else target[result] = array = specificCreate ? specificCreate($this, 0) : [];
push.call(array, value);
key = toPropertyKey(boundFunction(value, index, O));
if (has(target, key)) push.call(target[key], value);
else target[key] = [value];
}
if (specificConstructor) {
Constructor = specificConstructor(O);
if (Constructor !== Array) {
for (key in target) target[key] = arrayFromConstructorAndList(Constructor, target[key]);
}
} return target;
};
5 changes: 3 additions & 2 deletions packages/core-js/modules/esnext.array.group-by.js
@@ -1,14 +1,15 @@
'use strict';
var $ = require('../internals/export');
var $groupBy = require('../internals/array-group-by');
var arraySpeciesCreate = require('../internals/array-species-create');
var arraySpeciesConstructor = require('../internals/array-species-constructor');
var addToUnscopables = require('../internals/add-to-unscopables');

// `Array.prototype.groupBy` method
// https://github.com/tc39/proposal-array-grouping
$({ target: 'Array', proto: true }, {
groupBy: function groupBy(callbackfn /* , thisArg */) {
return $groupBy(this, callbackfn, arguments.length > 1 ? arguments[1] : undefined, arraySpeciesCreate);
var thisArg = arguments.length > 1 ? arguments[1] : undefined;
return $groupBy(this, callbackfn, thisArg, arraySpeciesConstructor);
}
});

Expand Down
14 changes: 3 additions & 11 deletions packages/core-js/modules/esnext.typed-array.group-by.js
@@ -1,22 +1,14 @@
'use strict';
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
var $groupBy = require('../internals/array-group-by');
var speciesConstructor = require('../internals/species-constructor');
var typedArraySpeciesConstructor = require('../internals/typed-array-species-constructor');

var aTypedArray = ArrayBufferViewCore.aTypedArray;
var aTypedArrayConstructor = ArrayBufferViewCore.aTypedArrayConstructor;
var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;

// `%TypedArray%.prototype.groupBy` method
// https://github.com/tc39/proposal-array-grouping
exportTypedArrayMethod('groupBy', function groupBy(callbackfn /* , thisArg */) {
var result = $groupBy(aTypedArray(this), callbackfn, arguments.length > 1 ? arguments[1] : undefined);
var TypedArray = aTypedArrayConstructor(speciesConstructor(this, this.constructor));
var key, array, typedArray, index, length;
for (key in result) {
array = result[key];
length = array.length;
result[key] = typedArray = new TypedArray(length);
for (index = 0; index < length; index++) typedArray[index] = array[index];
} return result;
var thisArg = arguments.length > 1 ? arguments[1] : undefined;
return $groupBy(aTypedArray(this), callbackfn, thisArg, typedArraySpeciesConstructor);
});

0 comments on commit 6245936

Please sign in to comment.