diff --git a/packages/core-js/internals/array-group-by.js b/packages/core-js/internals/array-group-by.js index ad7219b65ee9..6c225b0bfe97 100644 --- a/packages/core-js/internals/array-group-by.js +++ b/packages/core-js/internals/array-group-by.js @@ -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; }; diff --git a/packages/core-js/modules/esnext.array.group-by.js b/packages/core-js/modules/esnext.array.group-by.js index 0eb47760fc50..6e82b72f86d6 100644 --- a/packages/core-js/modules/esnext.array.group-by.js +++ b/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); } }); diff --git a/packages/core-js/modules/esnext.typed-array.group-by.js b/packages/core-js/modules/esnext.typed-array.group-by.js index 0f72d6db6f6f..7b42ccd8ecad 100644 --- a/packages/core-js/modules/esnext.typed-array.group-by.js +++ b/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); });