Skip to content

Commit 6245936

Browse files
committedJul 29, 2021
adapt to the actual spec draft
1 parent db449ca commit 6245936

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed
 

‎packages/core-js/internals/array-group-by.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,30 @@ var has = require('../internals/has');
33
var IndexedObject = require('../internals/indexed-object');
44
var toObject = require('../internals/to-object');
55
var toLength = require('../internals/to-length');
6-
var toPrimitive = require('../internals/to-primitive');
6+
var toPropertyKey = require('../internals/to-property-key');
77
var objectCreate = require('../internals/object-create');
8+
var arrayFromConstructorAndList = require('../internals/array-from-constructor-and-list');
89

910
var push = [].push;
1011

11-
module.exports = function ($this, callbackfn, that, specificCreate) {
12+
module.exports = function ($this, callbackfn, that, specificConstructor) {
1213
var O = toObject($this);
1314
var self = IndexedObject(O);
1415
var boundFunction = bind(callbackfn, that, 3);
1516
var target = objectCreate(null);
1617
var length = toLength(self.length);
1718
var index = 0;
18-
var value, result, array;
19+
var Constructor, key, value;
1920
for (;length > index; index++) {
2021
value = self[index];
21-
result = toPrimitive(boundFunction(value, index, O), 'string');
22-
if (has(target, result)) array = target[result];
23-
else target[result] = array = specificCreate ? specificCreate($this, 0) : [];
24-
push.call(array, value);
22+
key = toPropertyKey(boundFunction(value, index, O));
23+
if (has(target, key)) push.call(target[key], value);
24+
else target[key] = [value];
25+
}
26+
if (specificConstructor) {
27+
Constructor = specificConstructor(O);
28+
if (Constructor !== Array) {
29+
for (key in target) target[key] = arrayFromConstructorAndList(Constructor, target[key]);
30+
}
2531
} return target;
2632
};

‎packages/core-js/modules/esnext.array.group-by.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
'use strict';
22
var $ = require('../internals/export');
33
var $groupBy = require('../internals/array-group-by');
4-
var arraySpeciesCreate = require('../internals/array-species-create');
4+
var arraySpeciesConstructor = require('../internals/array-species-constructor');
55
var addToUnscopables = require('../internals/add-to-unscopables');
66

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

Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
'use strict';
22
var ArrayBufferViewCore = require('../internals/array-buffer-view-core');
33
var $groupBy = require('../internals/array-group-by');
4-
var speciesConstructor = require('../internals/species-constructor');
4+
var typedArraySpeciesConstructor = require('../internals/typed-array-species-constructor');
55

66
var aTypedArray = ArrayBufferViewCore.aTypedArray;
7-
var aTypedArrayConstructor = ArrayBufferViewCore.aTypedArrayConstructor;
87
var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod;
98

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

0 commit comments

Comments
 (0)
Please sign in to comment.