diff --git a/features.txt b/features.txt index de9b10f44b0..a193406c8db 100644 --- a/features.txt +++ b/features.txt @@ -63,7 +63,7 @@ ShadowRealm # https://github.com/tc39/proposal-array-find-from-last array-find-from-last -# Array.prototype.group & Array.prototype.groupToMap +# Array.groupBy & Map.groupBy # https://github.com/tc39/proposal-array-grouping array-grouping diff --git a/test/built-ins/Array/prototype/group/array-like.js b/test/built-ins/Array/prototype/group/array-like.js deleted file mode 100644 index 42a7b085ae9..00000000000 --- a/test/built-ins/Array/prototype/group/array-like.js +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group iterates array-like up to length -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 2. Let len be ? LengthOfArrayLike(O). - ... - 4. Let k be 0. - ... - 6. Repeat, while k < len - - ... -includes: [compareArray.js] -features: [array-grouping] ----*/ - -const arrayLike = {0: 1, 1: 2, 2: 3, 3: 4, length: 3 }; - -let calls = 0; - -const obj = Array.prototype.group.call(arrayLike, function (i) { calls++; return i % 2 === 0 ? 'even' : 'odd'; }); - -assert.sameValue(calls, 3, 'only calls length times'); -assert.compareArray(Object.keys(obj), ['odd', 'even']); -assert.compareArray(obj['even'], [2]); -assert.compareArray(obj['odd'], [1, 3]); diff --git a/test/built-ins/Array/prototype/group/callback-arg.js b/test/built-ins/Array/prototype/group/callback-arg.js deleted file mode 100644 index 5d38d65df73..00000000000 --- a/test/built-ins/Array/prototype/group/callback-arg.js +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group calls function with correct arguments -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - - a. Let Pk be ! ToString(𝔽(k)). - b. Let kValue be ? Get(O, Pk). - c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)). - d. Perform ! AddValueToKeyedGroup(groups, propertyKey, kValue). - e. Set k to k + 1. - ... -features: [array-grouping] ----*/ - - -const arr = [-0, 0, 1, 2, 3]; - -let calls = 0; - -const map = arr.group(function (n, i, testArr) { - calls++; - assert.sameValue(n, arr[i], "selected element aligns with index"); - assert.sameValue(testArr, arr, "original array is passed as final argument"); - return null; -}); - -assert.sameValue(calls, 5, 'called for all 5 elements'); diff --git a/test/built-ins/Array/prototype/group/callback-throws.js b/test/built-ins/Array/prototype/group/callback-throws.js deleted file mode 100644 index 785a7b74a33..00000000000 --- a/test/built-ins/Array/prototype/group/callback-throws.js +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group errors when return value cannot be converted to a property key. -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)). - - ... -features: [array-grouping] ----*/ - -assert.throws(Test262Error, function() { - const array = [1]; - array.group(function() { - throw new Test262Error('throw in callback'); - }) -}); diff --git a/test/built-ins/Array/prototype/group/emptyList.js b/test/built-ins/Array/prototype/group/emptyList.js deleted file mode 100644 index 366c12d08d0..00000000000 --- a/test/built-ins/Array/prototype/group/emptyList.js +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Callback is not called and object is not populated if the array is empty -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 8. For each Record { [[Key]], [[Elements]] } g of groups, do - - a. Let elements be ! CreateArrayFromList(g.[[Elements]]). - b. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements). - - ... -features: [array-grouping] ----*/ - -const original = []; - -const obj = original.group(function () { - throw new Test262Error('callback function should not be called') -}); - -assert.notSameValue(original, obj, 'group returns a object'); -assert.sameValue(Object.keys(obj).length, 0); diff --git a/test/built-ins/Array/prototype/group/evenOdd.js b/test/built-ins/Array/prototype/group/evenOdd.js deleted file mode 100644 index 6d22cb69400..00000000000 --- a/test/built-ins/Array/prototype/group/evenOdd.js +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group populates object with correct keys and values -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 8. For each Record { [[Key]], [[Elements]] } g of groups, do - - a. Let elements be ! CreateArrayFromList(g.[[Elements]]). - b. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements). - - ... -includes: [compareArray.js] -features: [array-grouping] ----*/ - -const array = [1, 2, 3]; - -const obj = array.group(function (i) { - return i % 2 === 0 ? 'even' : 'odd'; -}); - -assert.compareArray(Object.keys(obj), ['odd', 'even']); -assert.compareArray(obj['even'], [2]); -assert.compareArray(obj['odd'], [1, 3]); diff --git a/test/built-ins/Array/prototype/group/get-throws.js b/test/built-ins/Array/prototype/group/get-throws.js deleted file mode 100644 index d9182c661d5..00000000000 --- a/test/built-ins/Array/prototype/group/get-throws.js +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group errors when accessing a getter throws. -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - a. Let Pk be ! ToString(𝔽(k)). - b. Let kValue be ? Get(O, Pk). - - ... -features: [array-grouping] ----*/ - -assert.throws(Test262Error, function() { - const arrayLike = Object.defineProperty({ - length: 1, - }, '0', { - get: function() { - throw new Test262Error('no element for you'); - } - }); - Array.prototype.group.call(arrayLike, function() { - return 'key'; - }); -}); diff --git a/test/built-ins/Array/prototype/group/invalid-callback.js b/test/built-ins/Array/prototype/group/invalid-callback.js deleted file mode 100644 index ee5253c21d6..00000000000 --- a/test/built-ins/Array/prototype/group/invalid-callback.js +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group called with non-callable throws TypeError -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 3. If IsCallable(callbackfn) is false, throw a TypeError exception. - - ... -features: [array-grouping] ----*/ - - -assert.throws(TypeError, function() { - [].group(null) -}, "null callback throws TypeError"); - -assert.throws(TypeError, function() { - [].group(undefined) -}, "undefined callback throws TypeError"); - -assert.throws(TypeError, function() { - [].group({}) -}, "object callback throws TypeError"); diff --git a/test/built-ins/Array/prototype/group/invalid-object.js b/test/built-ins/Array/prototype/group/invalid-object.js deleted file mode 100644 index 244377832a5..00000000000 --- a/test/built-ins/Array/prototype/group/invalid-object.js +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group applied to null/undefined throws a TypeError -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 1. Let O be ? ToObject(this value). - - ... -features: [array-grouping] ----*/ - -const throws = function() { - throw new Test262Error('callback function should not be called') -}; - -assert.throws(TypeError, function() { - Array.prototype.group.call(undefined, throws); -}, 'undefined this value'); - -assert.throws(TypeError, function() { - Array.prototype.group.call(undefined, throws); -}, 'null this value'); diff --git a/test/built-ins/Array/prototype/group/invalid-property-key.js b/test/built-ins/Array/prototype/group/invalid-property-key.js deleted file mode 100644 index ce92006f1d8..00000000000 --- a/test/built-ins/Array/prototype/group/invalid-property-key.js +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group errors when return value cannot be converted to a property key. -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)). - - ... -features: [array-grouping] ----*/ - -assert.throws(Test262Error, function() { - const array = [1]; - array.group(function() { - return { - toString() { - throw new Test262Error('not a property key'); - } - }; - }) -}); diff --git a/test/built-ins/Array/prototype/group/length-throw.js b/test/built-ins/Array/prototype/group/length-throw.js deleted file mode 100644 index e3d1747e129..00000000000 --- a/test/built-ins/Array/prototype/group/length-throw.js +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group errors when array-like's length can't be coerced. -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 2. Let len be ? LengthOfArrayLike(O). - - ... -features: [array-grouping] ----*/ - -assert.throws(Test262Error, function() { - const arrayLike = Object.defineProperty({}, 'length', { - get: function() { - throw new Test262Error('no length for you'); - } - }); - Array.prototype.group.call(arrayLike, function() { - return 'key'; - }); -}); - -assert.throws(TypeError, function() { - const arrayLike = { - length: 1n, - }; - Array.prototype.group.call(arrayLike, function() { - return 'key'; - }); -}); diff --git a/test/built-ins/Array/prototype/group/length.js b/test/built-ins/Array/prototype/group/length.js deleted file mode 100644 index ea4896c73d8..00000000000 --- a/test/built-ins/Array/prototype/group/length.js +++ /dev/null @@ -1,26 +0,0 @@ - -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group property length descriptor -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 17 ECMAScript Standard Built-in Objects - - ... - -includes: [propertyHelper.js] -features: [array-grouping] ----*/ - -verifyProperty(Array.prototype.group, "length", { - value: 1, - enumerable: false, - writable: false, - configurable: true -}); diff --git a/test/built-ins/Array/prototype/group/name.js b/test/built-ins/Array/prototype/group/name.js deleted file mode 100644 index 26ea6e395e1..00000000000 --- a/test/built-ins/Array/prototype/group/name.js +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group property name descriptor -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 17 ECMAScript Standard Built-in Objects - - ... - -includes: [propertyHelper.js] -features: [array-grouping] ----*/ - -verifyProperty(Array.prototype.group, "name", { - value: "group", - enumerable: false, - writable: false, - configurable: true -}); diff --git a/test/built-ins/Array/prototype/group/null-prototype.js b/test/built-ins/Array/prototype/group/null-prototype.js deleted file mode 100644 index c977ee39e48..00000000000 --- a/test/built-ins/Array/prototype/group/null-prototype.js +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group returns a null prototype object -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 7. Let obj be OrdinaryObjectCreate(null). - ... - 9. Return obj. - - ... -features: [array-grouping] ----*/ - -const array = [1, 2, 3]; - -const obj = array.group(function (i) { - return i % 2 === 0 ? 'even' : 'odd'; -}); - -assert.sameValue(Object.getPrototypeOf(obj), null); -assert.sameValue(obj.hasOwnProperty, undefined); diff --git a/test/built-ins/Array/prototype/group/sparse-array.js b/test/built-ins/Array/prototype/group/sparse-array.js deleted file mode 100644 index ab794d169b2..00000000000 --- a/test/built-ins/Array/prototype/group/sparse-array.js +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group adds undefined to the group for sparse arrays -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - a. Let Pk be ! ToString(𝔽(k)). - b. Let kValue be ? Get(O, Pk). - c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)). - d. Perform AddValueToKeyedGroup(groups, propertyKey, kValue). - - ... -includes: [compareArray.js] -features: [array-grouping] ----*/ - -let calls = 0; -const array = [, , ,]; - -const obj = array.group(function () { - calls++; - return 'key'; -}); - -assert.sameValue(calls, 3); - -assert(0 in obj['key'], 'group has a first element'); -assert(1 in obj['key'], 'group has a second element'); -assert(2 in obj['key'], 'group has a third element'); -assert.compareArray(obj['key'], [void 0, void 0, void 0]); diff --git a/test/built-ins/Array/prototype/group/this-arg-strict.js b/test/built-ins/Array/prototype/group/this-arg-strict.js deleted file mode 100644 index 7f3cfc71230..00000000000 --- a/test/built-ins/Array/prototype/group/this-arg-strict.js +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group calls with thisArg -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)). - - ... -flags: [onlyStrict] -features: [array-grouping] ----*/ - - -let sentinel = {}; -let result; - -[1].group(function() { - result = this; -}, "TestString"); -assert.sameValue(result, "TestString"); - -[1].group(function() { - result = this; -}, 1); -assert.sameValue(result, 1); - -[1].group(function() { - result = this; -}, true); -assert.sameValue(result, true); - -[1].group(function() { - result = this; -}, null); -assert.sameValue(result, null); - -[1].group(function() { - result = this; -}, sentinel); -assert.sameValue(result, sentinel); - -[1].group(function() { - result = this; -}, void 0); -assert.sameValue(result, void 0); - -[1].group(function() { - result = this; -}); -assert.sameValue(result, void 0); diff --git a/test/built-ins/Array/prototype/group/this-arg.js b/test/built-ins/Array/prototype/group/this-arg.js deleted file mode 100644 index 15d36c5c6b1..00000000000 --- a/test/built-ins/Array/prototype/group/this-arg.js +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group calls with thisArg -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)). - - ... -flags: [noStrict] -features: [array-grouping] ----*/ - - -let sentinel = {}; -let result; - -[1].group(function() { - result = this; -}, "TestString"); -assert.sameValue(result instanceof String, true); -assert.sameValue(result.valueOf(), "TestString"); - -[1].group(function() { - result = this; -}, 1); -assert.sameValue(result instanceof Number, true); -assert.sameValue(result.valueOf(), 1); - -[1].group(function() { - result = this; -}, true); -assert.sameValue(result instanceof Boolean, true); -assert.sameValue(result.valueOf(), true); - -[1].group(function() { - result = this; -}, null); -assert.sameValue(result, globalThis); - -[1].group(function() { - result = this; -}, sentinel); -assert.sameValue(result, sentinel); - -[1].group(function() { - result = this; -}, void 0); -assert.sameValue(result, globalThis); - -[1].group(function() { - result = this; -}); -assert.sameValue(result, globalThis); diff --git a/test/built-ins/Array/prototype/group/toPropertyKey.js b/test/built-ins/Array/prototype/group/toPropertyKey.js deleted file mode 100644 index b14ad7488ac..00000000000 --- a/test/built-ins/Array/prototype/group/toPropertyKey.js +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.group -description: Array.prototype.group coerces return value with ToPropertyKey -info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)). - d. Perform AddValueToKeyedGroup(groups, propertyKey, kValue). - ... - 8. For each Record { [[Key]], [[Elements]] } g of groups, do - a. Let elements be ! CreateArrayFromList(g.[[Elements]]). - b. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements). - - ... -includes: [compareArray.js] -features: [array-grouping] ----*/ - -let calls = 0; -const stringable = { - toString() { - return 1; - } -} - -const array = [1, '1', stringable]; - -const obj = array.group(function (v) { return v; }); - -assert.compareArray(Object.keys(obj), ['1']); -assert.compareArray(obj['1'], [1, '1', stringable]); diff --git a/test/built-ins/Array/prototype/groupToMap/array-like.js b/test/built-ins/Array/prototype/groupToMap/array-like.js deleted file mode 100644 index a9686063d32..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/array-like.js +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap iterates array-like up to length -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 2. Let len be ? LengthOfArrayLike(O). - ... - 4. Let k be 0. - ... - 6. Repeat, while k < len - - ... -includes: [compareArray.js] -features: [array-grouping, Map, Symbol.iterator] ----*/ - -const arrayLike = {0: 1, 1: 2, 2: 3, 3: 4, length: 3 }; - -let calls = 0; - -const map = Array.prototype.groupToMap.call(arrayLike, function (i) { - calls++; - return i % 2 === 0 ? 'even' : 'odd'; -}); - -assert.sameValue(calls, 3, 'only calls length times'); -assert.compareArray(Array.from(map.keys()), ['odd', 'even']); -assert.compareArray(map.get('even'), [2]); -assert.compareArray(map.get('odd'), [1, 3]); diff --git a/test/built-ins/Array/prototype/groupToMap/callback-arg.js b/test/built-ins/Array/prototype/groupToMap/callback-arg.js deleted file mode 100644 index 6f6a38a2e16..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/callback-arg.js +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap calls function with correct arguments -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - a. Let Pk be ! ToString(𝔽(k)). - b. Let kValue be ? Get(O, Pk). - c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »). - e. Perform ! AddValueToKeyedGroup(groups, key, kValue). - ... -features: [array-grouping, Map] ----*/ - - -const arr = [-0, 0, 1, 2, 3]; - -let calls = 0; - -arr.groupToMap(function (n, i, testArr) { - calls++; - assert.sameValue(n, arr[i], "selected element aligns with index"); - assert.sameValue(testArr, arr, "original array is passed as final argument"); - return null; -}); - -assert.sameValue(calls, 5, 'called for all 5 elements'); diff --git a/test/built-ins/Array/prototype/groupToMap/callback-throws.js b/test/built-ins/Array/prototype/groupToMap/callback-throws.js deleted file mode 100644 index 162d8b0180e..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/callback-throws.js +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap errors when return value cannot be converted to a property key. -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)). - - ... -features: [array-grouping] ----*/ - -assert.throws(Test262Error, function() { - const array = [1]; - array.groupToMap(function() { - throw new Test262Error('throw in callback'); - }) -}); diff --git a/test/built-ins/Array/prototype/groupToMap/emptyList.js b/test/built-ins/Array/prototype/groupToMap/emptyList.js deleted file mode 100644 index e5834ff4a20..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/emptyList.js +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Callback is not called and object is not populated if the array is empty -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 8. For each Record { [[Key]], [[Elements]] } g of groups, do - - a. Let elements be ! CreateArrayFromList(g.[[Elements]]). - b. Perform ! CreateDataPropertyOrThrow(map, g.[[Key]], elements). - - ... -features: [array-grouping] ----*/ - -const original = []; - -const map = original.groupToMap(function () { - throw new Test262Error('callback function should not be called') -}); - -assert.notSameValue(original, map, 'groupToMap returns a map'); -assert.sameValue(map.size, 0); diff --git a/test/built-ins/Array/prototype/groupToMap/evenOdd.js b/test/built-ins/Array/prototype/groupToMap/evenOdd.js deleted file mode 100644 index 1c675de51d1..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/evenOdd.js +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap populates object with correct keys and values -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 8. For each Record { [[Key]], [[Elements]] } g of groups, do - - a. Let elements be ! CreateArrayFromList(g.[[Elements]]). - b. Perform ! CreateDataPropertyOrThrow(map, g.[[Key]], elements). - - ... -includes: [compareArray.js] -features: [array-grouping, Map, Symbol.iterator] ----*/ - -const array = [1, 2, 3]; - -const map = array.groupToMap(function (i) { - return i % 2 === 0 ? 'even' : 'odd'; -}); - -assert.compareArray(Array.from(map.keys()), ['odd', 'even']); -assert.compareArray(map.get('even'), [2]); -assert.compareArray(map.get('odd'), [1, 3]); diff --git a/test/built-ins/Array/prototype/groupToMap/get-throws.js b/test/built-ins/Array/prototype/groupToMap/get-throws.js deleted file mode 100644 index cd64d474bf9..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/get-throws.js +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap errors when accessing a getter throws. -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - a. Let Pk be ! ToString(𝔽(k)). - b. Let kValue be ? Get(O, Pk). - - ... -features: [array-grouping] ----*/ - -assert.throws(Test262Error, function() { - const arrayLike = Object.defineProperty({ - length: 1, - }, '0', { - get: function() { - throw new Test262Error('no element for you'); - } - }); - Array.prototype.groupToMap.call(arrayLike, function() { - return 'key'; - }); -}); diff --git a/test/built-ins/Array/prototype/groupToMap/groupLength.js b/test/built-ins/Array/prototype/groupToMap/groupLength.js deleted file mode 100644 index b06d5f0d673..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/groupLength.js +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap populates object with correct keys and values -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 8. For each Record { [[Key]], [[Elements]] } g of groups, do - - a. Let elements be ! CreateArrayFromList(g.[[Elements]]). - b. Perform ! CreateDataPropertyOrThrow(map, g.[[Key]], elements). - - ... -includes: [compareArray.js] -features: [array-grouping, Map, Symbol.iterator] ----*/ - -const arr = ['hello', 'test', 'world']; - -const map = arr.groupToMap(function (i) { return i.length; }); - -assert.compareArray(Array.from(map.keys()), [5, 4]); -assert.compareArray(map.get(5), ['hello', 'world']); -assert.compareArray(map.get(4), ['test']); diff --git a/test/built-ins/Array/prototype/groupToMap/invalid-callback.js b/test/built-ins/Array/prototype/groupToMap/invalid-callback.js deleted file mode 100644 index b59d57c6ba8..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/invalid-callback.js +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap called with non-callable throws TypeError -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 3. If IsCallable(callbackfn) is false, throw a TypeError exception. - - ... -features: [array-grouping] ----*/ - - -assert.throws(TypeError, function() { - [].groupToMap(null) -}, "null callback throws TypeError"); - -assert.throws(TypeError, function() { - [].groupToMap(undefined) -}, "undefined callback throws TypeError"); - -assert.throws(TypeError, function() { - [].groupToMap({}) -}, "object callback throws TypeError"); diff --git a/test/built-ins/Array/prototype/groupToMap/invalid-object.js b/test/built-ins/Array/prototype/groupToMap/invalid-object.js deleted file mode 100644 index f7e95b63ad2..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/invalid-object.js +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap applied to null/undefined throws a TypeError -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 1. Let O be ? ToObject(this value). - - ... -features: [array-grouping] ----*/ - -const throws = function() { - throw new Test262Error('callback function should not be called') -}; - -assert.throws(TypeError, function() { - Array.prototype.groupToMap.call(undefined, throws); -}, 'undefined this value'); - -assert.throws(TypeError, function() { - Array.prototype.groupToMap.call(undefined, throws); -}, 'null this value'); diff --git a/test/built-ins/Array/prototype/groupToMap/invalid-property-key.js b/test/built-ins/Array/prototype/groupToMap/invalid-property-key.js deleted file mode 100644 index 990eea14f82..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/invalid-property-key.js +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap does not error when return value cannot be converted to a property key. -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)). - - ... -includes: [compareArray.js] -features: [array-grouping, Map, Symbol.iterator] ----*/ - -const key = { - toString() { - throw new Test262Error('not a property key'); - } -}; - -const map = [1].groupToMap(function() { - return key; -}); - -assert.compareArray(Array.from(map.keys()), [key]); -assert.compareArray(map.get(key), [1]); diff --git a/test/built-ins/Array/prototype/groupToMap/length-throw.js b/test/built-ins/Array/prototype/groupToMap/length-throw.js deleted file mode 100644 index 63eb8581cf4..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/length-throw.js +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap errors when array-like's length can't be coerced. -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 2. Let len be ? LengthOfArrayLike(O). - - ... -features: [array-grouping] ----*/ - -assert.throws(Test262Error, function() { - const arrayLike = Object.defineProperty({}, 'length', { - get: function() { - throw new Test262Error('no length for you'); - } - }); - Array.prototype.groupToMap.call(arrayLike, function() { - return 'key'; - }); -}); - -assert.throws(TypeError, function() { - const arrayLike = { - length: 1n, - }; - Array.prototype.groupToMap.call(arrayLike, function() { - return 'key'; - }); -}); diff --git a/test/built-ins/Array/prototype/groupToMap/length.js b/test/built-ins/Array/prototype/groupToMap/length.js deleted file mode 100644 index 822764026c2..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/length.js +++ /dev/null @@ -1,26 +0,0 @@ - -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap property length descriptor -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 17 ECMAScript Standard Built-in Objects - - ... - -includes: [propertyHelper.js] -features: [array-grouping] ----*/ - -verifyProperty(Array.prototype.groupToMap, "length", { - value: 1, - enumerable: false, - writable: false, - configurable: true -}); diff --git a/test/built-ins/Array/prototype/groupToMap/map-instance.js b/test/built-ins/Array/prototype/groupToMap/map-instance.js deleted file mode 100644 index bd5c6987f76..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/map-instance.js +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap returns a Map instance -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 7. Let map be ! Construct(%Map%). - ... - 9. Return map. - - ... -features: [array-grouping, Map] ----*/ - -const array = [1, 2, 3]; - -const map = array.groupToMap(function (i) { - return i % 2 === 0 ? 'even' : 'odd'; -}); - -assert.sameValue(map instanceof Map, true); diff --git a/test/built-ins/Array/prototype/groupToMap/name.js b/test/built-ins/Array/prototype/groupToMap/name.js deleted file mode 100644 index 26212fead5a..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/name.js +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap property name descriptor -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 17 ECMAScript Standard Built-in Objects - - ... - -includes: [propertyHelper.js] -features: [array-grouping] ----*/ - -verifyProperty(Array.prototype.groupToMap, "name", { - value: "groupToMap", - enumerable: false, - writable: false, - configurable: true -}); diff --git a/test/built-ins/Array/prototype/groupToMap/negativeZero.js b/test/built-ins/Array/prototype/groupToMap/negativeZero.js deleted file mode 100644 index 7e6cc0acb91..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/negativeZero.js +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap normalize 0 for Map key -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 6.d. If key is -0𝔽, set key to +0𝔽. - - ... -includes: [compareArray.js] -features: [array-grouping, Map] ----*/ - - -const arr = [-0, +0]; - -const map = arr.groupToMap(function (i) { return i; }); - -assert.sameValue(map.size, 1); -assert.compareArray(map.get(0), [-0, 0]); diff --git a/test/built-ins/Array/prototype/groupToMap/sparse-array.js b/test/built-ins/Array/prototype/groupToMap/sparse-array.js deleted file mode 100644 index 801fc5f2ced..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/sparse-array.js +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap adds undefined to the group for sparse arrays -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - a. Let Pk be ! ToString(𝔽(k)). - b. Let kValue be ? Get(O, Pk). - c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »). - e. Perform AddValueToKeyedGroup(groups, key, kValue). - - ... -includes: [compareArray.js] -features: [array-grouping, Map] ----*/ - -let calls = 0; -const array = [, , ,]; - -const map = array.groupToMap(function () { - calls++; - return 'key'; -}); - -assert.sameValue(calls, 3); - -const key = map.get('key'); -assert(0 in key, 'group has a first element'); -assert(1 in key, 'group has a second element'); -assert(2 in key, 'group has a third element'); -assert.compareArray(key, [void 0, void 0, void 0]); diff --git a/test/built-ins/Array/prototype/groupToMap/this-arg-strict.js b/test/built-ins/Array/prototype/groupToMap/this-arg-strict.js deleted file mode 100644 index 7d3ec3c1cd1..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/this-arg-strict.js +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap calls with thisArg -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »). - - ... -flags: [onlyStrict] -features: [array-grouping, Map] ----*/ - - -let sentinel = {}; -let result; - -[1].groupToMap(function() { - result = this; -}, "TestString"); -assert.sameValue(result, "TestString"); - -[1].groupToMap(function() { - result = this; -}, 1); -assert.sameValue(result, 1); - -[1].groupToMap(function() { - result = this; -}, true); -assert.sameValue(result, true); - -[1].groupToMap(function() { - result = this; -}, null); -assert.sameValue(result, null); - -[1].groupToMap(function() { - result = this; -}, sentinel); -assert.sameValue(result, sentinel); - -[1].groupToMap(function() { - result = this; -}, void 0); -assert.sameValue(result, void 0); - -[1].groupToMap(function() { - result = this; -}); -assert.sameValue(result, void 0); diff --git a/test/built-ins/Array/prototype/groupToMap/this-arg.js b/test/built-ins/Array/prototype/groupToMap/this-arg.js deleted file mode 100644 index dbe05972e32..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/this-arg.js +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap calls with thisArg -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »). - - ... -flags: [noStrict] -features: [array-grouping, Map] ----*/ - - -let sentinel = {}; -let result; - -[1].groupToMap(function() { - result = this; -}, "TestString"); -assert.sameValue(result instanceof String, true); -assert.sameValue(result.valueOf(), "TestString"); - -[1].groupToMap(function() { - result = this; -}, 1); -assert.sameValue(result instanceof Number, true); -assert.sameValue(result.valueOf(), 1); - -[1].groupToMap(function() { - result = this; -}, true); -assert.sameValue(result instanceof Boolean, true); -assert.sameValue(result.valueOf(), true); - -[1].groupToMap(function() { - result = this; -}, null); -assert.sameValue(result, globalThis); - -[1].groupToMap(function() { - result = this; -}, sentinel); -assert.sameValue(result, sentinel); - -[1].groupToMap(function() { - result = this; -}, void 0); -assert.sameValue(result, globalThis); - -[1].groupToMap(function() { - result = this; -}); -assert.sameValue(result, globalThis); diff --git a/test/built-ins/Array/prototype/groupToMap/toPropertyKey.js b/test/built-ins/Array/prototype/groupToMap/toPropertyKey.js deleted file mode 100644 index f3f8c5f46ff..00000000000 --- a/test/built-ins/Array/prototype/groupToMap/toPropertyKey.js +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-array.prototype.groupToMap -description: Array.prototype.groupToMap does not coerce return value with ToPropertyKey -info: | - 22.1.3.15 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ) - - ... - - 6. Repeat, while k < len - c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »). - e. Perform AddValueToKeyedGroup(groups, key, kValue). - ... - 8. For each Record { [[Key]], [[Elements]] } g of groups, do - a. Let elements be ! CreateArrayFromList(g.[[Elements]]). - b. Perform ! CreateDataPropertyOrThrow(map, g.[[Key]], elements). - - ... -includes: [compareArray.js] -features: [array-grouping] ----*/ - -let calls = 0; -const stringable = { - toString() { - return 1; - } -} - -const array = [1, '1', stringable]; - -const map = array.groupToMap(v => v); - -assert.compareArray(Array.from(map.keys()), [1, '1', stringable]); -assert.compareArray(map.get('1'), ['1']); -assert.compareArray(map.get(1), [1]); -assert.compareArray(map.get(stringable), [stringable]); diff --git a/test/built-ins/Map/groupBy/callback-arg.js b/test/built-ins/Map/groupBy/callback-arg.js new file mode 100644 index 00000000000..7b01aa50ba0 --- /dev/null +++ b/test/built-ins/Map/groupBy/callback-arg.js @@ -0,0 +1,32 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-map.groupby +description: Map.groupBy calls function with correct arguments +info: | + Map.groupBy ( items, callbackfn ) + + ... + GroupBy ( items, callbackfn, coercion ) + + 6. Repeat, + + e. Let key be Completion(Call(callbackfn, undefined, « value, 𝔽(k) »)). + ... +features: [array-grouping, Map] +---*/ + + +const arr = [-0, 0, 1, 2, 3]; + +let calls = 0; + +Map.groupBy(arr, function (n, i) { + calls++; + assert.sameValue(n, arr[i], "selected element aligns with index"); + assert.sameValue(arguments.length, 2, "only two arguments are passed"); + return null; +}); + +assert.sameValue(calls, 5, 'called for all 5 elements'); diff --git a/test/built-ins/Map/groupBy/callback-throws.js b/test/built-ins/Map/groupBy/callback-throws.js new file mode 100644 index 00000000000..5f586e841ec --- /dev/null +++ b/test/built-ins/Map/groupBy/callback-throws.js @@ -0,0 +1,25 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-map.groupby +description: Map.groupBy throws when callback throws +info: | + Map.groupBy ( items, callbackfn ) + + ... + GroupBy ( items, callbackfn, coercion ) + + 6. Repeat, + e. Let key be Completion(Call(callbackfn, undefined, « value, 𝔽(k) »)). + f. IfAbruptCloseIterator(key, iteratorRecord). + ... +features: [array-grouping, Map] +---*/ + +assert.throws(Test262Error, function() { + const array = [1]; + Map.groupBy(array, function() { + throw new Test262Error('throw in callback'); + }) +}); diff --git a/test/built-ins/Map/groupBy/emptyList.js b/test/built-ins/Map/groupBy/emptyList.js new file mode 100644 index 00000000000..cff59207a70 --- /dev/null +++ b/test/built-ins/Map/groupBy/emptyList.js @@ -0,0 +1,27 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-map.groupby +description: Callback is not called and object is not populated if the iterable is empty +info: | + Map.groupBy ( items, callbackfn ) + + ... + GroupBy ( items, callbackfn, coercion ) + + 6. Repeat, + c. If next is false, then + i. Return groups. + ... +features: [array-grouping, Map] +---*/ + +const original = []; + +const map = Map.groupBy(original, function () { + throw new Test262Error('callback function should not be called') +}); + +assert.notSameValue(original, map, 'Map.groupBy returns a map'); +assert.sameValue(map.size, 0); diff --git a/test/built-ins/Map/groupBy/evenOdd.js b/test/built-ins/Map/groupBy/evenOdd.js new file mode 100644 index 00000000000..965bfe4101a --- /dev/null +++ b/test/built-ins/Map/groupBy/evenOdd.js @@ -0,0 +1,22 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-map.groupby +description: Map.groupBy populates Map with correct keys and values +info: | + Map.groupBy ( items, callbackfn ) + ... +includes: [compareArray.js] +features: [array-grouping, Map] +---*/ + +const array = [1, 2, 3]; + +const map = Map.groupBy(array, function (i) { + return i % 2 === 0 ? 'even' : 'odd'; +}); + +assert.compareArray(Array.from(map.keys()), ['odd', 'even']); +assert.compareArray(map.get('even'), [2]); +assert.compareArray(map.get('odd'), [1, 3]); diff --git a/test/built-ins/Map/groupBy/groupLength.js b/test/built-ins/Map/groupBy/groupLength.js new file mode 100644 index 00000000000..a00df19ce05 --- /dev/null +++ b/test/built-ins/Map/groupBy/groupLength.js @@ -0,0 +1,21 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-map.groupby +description: Map.groupBy populates Map with correct keys and values +info: | + Map.groupBy ( items, callbackfn ) + + ... +includes: [compareArray.js] +features: [array-grouping, Map, Symbol.iterator] +---*/ + +const arr = ['hello', 'test', 'world']; + +const map = Map.groupBy(arr, function (i) { return i.length; }); + +assert.compareArray(Array.from(map.keys()), [5, 4]); +assert.compareArray(map.get(5), ['hello', 'world']); +assert.compareArray(map.get(4), ['test']); diff --git a/test/built-ins/Map/groupBy/invalid-callback.js b/test/built-ins/Map/groupBy/invalid-callback.js new file mode 100644 index 00000000000..b6cd835d6c6 --- /dev/null +++ b/test/built-ins/Map/groupBy/invalid-callback.js @@ -0,0 +1,29 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-map.groupby +description: Map.groupBy called with non-callable throws TypeError +info: | + Map.groupBy ( items, callbackfn ) + + ... + GroupBy ( items, callbackfn, coercion ) + + 2. If IsCallable(callbackfn) is false, throw a TypeError exception. + ... +features: [array-grouping, Map] +---*/ + + +assert.throws(TypeError, function() { + Map.groupBy([], null) +}, "null callback throws TypeError"); + +assert.throws(TypeError, function() { + Map.groupBy([], undefined) +}, "undefined callback throws TypeError"); + +assert.throws(TypeError, function() { + Map.groupBy([], {}) +}, "object callback throws TypeError"); diff --git a/test/built-ins/Map/groupBy/invalid-iterable.js b/test/built-ins/Map/groupBy/invalid-iterable.js new file mode 100644 index 00000000000..51031fbe577 --- /dev/null +++ b/test/built-ins/Map/groupBy/invalid-iterable.js @@ -0,0 +1,34 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-map.groupby +description: Map.groupBy with a nullish Symbol.iterator throws +info: | + Map.groupBy ( items, callbackfn ) + + ... + GroupBy ( items, callbackfn, coercion ) + + 4. Let iteratorRecord be ? GetIterator(items). + + ... +features: [array-grouping, Map] +---*/ + +const throws = function () { + throw new Test262Error('callback function should not be called') +}; + +function makeIterable(obj, iteratorFn) { + obj[Symbol.iterator] = iteratorFn; + return obj; +} + +assert.throws(TypeError, function () { + Map.groupBy(makeIterable({}, undefined), throws); +}, 'undefined Symbol.iterator'); + +assert.throws(TypeError, function () { + Map.groupBy(makeIterable({}, null), throws); +}, 'null Symbol.iterator'); diff --git a/test/built-ins/Map/groupBy/iterator-next-throws.js b/test/built-ins/Map/groupBy/iterator-next-throws.js new file mode 100644 index 00000000000..70656d56b69 --- /dev/null +++ b/test/built-ins/Map/groupBy/iterator-next-throws.js @@ -0,0 +1,31 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-map.groupby +description: Map.groupBy throws when iterator next throws +info: | + Map.groupBy ( items, callbackfn ) + + ... + + GroupBy ( items, callbackfn, coercion ) + + 6. Repeat, + b. Let next be ? IteratorStep(iteratorRecord). + + ... +features: [array-grouping, Map] +---*/ + +const throwingIterator = { + next: function next() { + throw new Test262Error('next() method was called'); + } +}; + +assert.throws(Test262Error, function () { + Map.groupBy(throwingIterator, function () { + return 'key'; + }); +}); diff --git a/test/built-ins/Map/groupBy/length.js b/test/built-ins/Map/groupBy/length.js new file mode 100644 index 00000000000..a4c83bd090a --- /dev/null +++ b/test/built-ins/Map/groupBy/length.js @@ -0,0 +1,25 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-map.groupby +description: Map.groupBy property length descriptor +info: | + Map.groupBy ( items, callbackfn ) + + ... + + 17 ECMAScript Standard Built-in Objects + + ... + +includes: [propertyHelper.js] +features: [array-grouping, Map] +---*/ + +verifyProperty(Map.groupBy, "length", { + value: 2, + enumerable: false, + writable: false, + configurable: true +}); diff --git a/test/built-ins/Map/groupBy/map-instance.js b/test/built-ins/Map/groupBy/map-instance.js new file mode 100644 index 00000000000..d871b3c355c --- /dev/null +++ b/test/built-ins/Map/groupBy/map-instance.js @@ -0,0 +1,26 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-map.groupby +description: Map.groupBy returns a Map instance +info: | + Map.groupBy ( items, callbackfn ) + + ... + + 2. Let map be ! Construct(%Map%). + ... + 4. Return map. + + ... +features: [array-grouping, Map] +---*/ + +const array = [1, 2, 3]; + +const map = Map.groupBy(array, function (i) { + return i % 2 === 0 ? 'even' : 'odd'; +}); + +assert.sameValue(map instanceof Map, true); diff --git a/test/built-ins/Map/groupBy/name.js b/test/built-ins/Map/groupBy/name.js new file mode 100644 index 00000000000..d2dd26034c3 --- /dev/null +++ b/test/built-ins/Map/groupBy/name.js @@ -0,0 +1,25 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-map.groupby +description: Map.groupBy property name descriptor +info: | + Map.groupBy ( items, callbackfn ) + + ... + + 17 ECMAScript Standard Built-in Objects + + ... + +includes: [propertyHelper.js] +features: [array-grouping, Map] +---*/ + +verifyProperty(Map.groupBy, "name", { + value: "groupBy", + enumerable: false, + writable: false, + configurable: true +}); diff --git a/test/built-ins/Map/groupBy/negativeZero.js b/test/built-ins/Map/groupBy/negativeZero.js new file mode 100644 index 00000000000..f73f474a7b9 --- /dev/null +++ b/test/built-ins/Map/groupBy/negativeZero.js @@ -0,0 +1,30 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-map.groupby +description: Map.groupBy normalizes 0 for Map key +info: | + Map.groupBy ( items, callbackfn ) + + ... + + GroupBy ( items, callbackfn, coercion ) + + 6. Repeat, + h. Else, + i. Assert: coercion is zero. + ii. If key is -0𝔽, set key to +0𝔽. + + ... +includes: [compareArray.js] +features: [array-grouping, Map] +---*/ + + +const arr = [-0, +0]; + +const map = Map.groupBy(arr, function (i) { return i; }); + +assert.sameValue(map.size, 1); +assert.compareArray(map.get(0), [-0, 0]); diff --git a/test/built-ins/Map/groupBy/toPropertyKey.js b/test/built-ins/Map/groupBy/toPropertyKey.js new file mode 100644 index 00000000000..f162f1ea43d --- /dev/null +++ b/test/built-ins/Map/groupBy/toPropertyKey.js @@ -0,0 +1,29 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-map.groupby +description: Map.groupBy does not coerce return value with ToPropertyKey +info: | + Map.groupBy ( items, callbackfn ) + + ... +includes: [compareArray.js] +features: [array-grouping, Map] +---*/ + +let calls = 0; +const stringable = { + toString: function toString() { + return 1; + } +}; + +const array = [1, '1', stringable]; + +const map = Map.groupBy(array, function (v) { return v; }); + +assert.compareArray(Array.from(map.keys()), [1, '1', stringable]); +assert.compareArray(map.get('1'), ['1']); +assert.compareArray(map.get(1), [1]); +assert.compareArray(map.get(stringable), [stringable]); diff --git a/test/built-ins/Object/groupBy/callback-arg.js b/test/built-ins/Object/groupBy/callback-arg.js new file mode 100644 index 00000000000..b6fd2c6bbea --- /dev/null +++ b/test/built-ins/Object/groupBy/callback-arg.js @@ -0,0 +1,32 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-object.groupby +description: Object.groupBy calls function with correct arguments +info: | + Object.groupBy ( items, callbackfn ) + + ... + GroupBy ( items, callbackfn, coercion ) + + 6. Repeat, + + e. Let key be Completion(Call(callbackfn, undefined, « value, 𝔽(k) »)). + ... +features: [array-grouping] +---*/ + + +const arr = [-0, 0, 1, 2, 3]; + +let calls = 0; + +Object.groupBy(arr, function (n, i) { + calls++; + assert.sameValue(n, arr[i], "selected element aligns with index"); + assert.sameValue(arguments.length, 2, "only two arguments are passed"); + return null; +}); + +assert.sameValue(calls, 5, 'called for all 5 elements'); diff --git a/test/built-ins/Object/groupBy/callback-throws.js b/test/built-ins/Object/groupBy/callback-throws.js new file mode 100644 index 00000000000..8430b8f98d1 --- /dev/null +++ b/test/built-ins/Object/groupBy/callback-throws.js @@ -0,0 +1,25 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-object.groupby +description: Object.groupBy throws when callback throws +info: | + Object.groupBy ( items, callbackfn ) + + ... + GroupBy ( items, callbackfn, coercion ) + + 6. Repeat, + e. Let key be Completion(Call(callbackfn, undefined, « value, 𝔽(k) »)). + f. IfAbruptCloseIterator(key, iteratorRecord). + ... +features: [array-grouping] +---*/ + +assert.throws(Test262Error, function() { + const array = [1]; + Object.groupBy(array, function() { + throw new Test262Error('throw in callback'); + }) +}); diff --git a/test/built-ins/Object/groupBy/emptyList.js b/test/built-ins/Object/groupBy/emptyList.js new file mode 100644 index 00000000000..52e2d093c93 --- /dev/null +++ b/test/built-ins/Object/groupBy/emptyList.js @@ -0,0 +1,27 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-object.groupby +description: Callback is not called and object is not populated if the iterable is empty +info: | + Object.groupBy ( items, callbackfn ) + + ... + GroupBy ( items, callbackfn, coercion ) + + 6. Repeat, + c. If next is false, then + i. Return groups. + ... +features: [array-grouping] +---*/ + +const original = []; + +const obj = Object.groupBy(original, function () { + throw new Test262Error('callback function should not be called') +}); + +assert.notSameValue(original, obj, 'Object.groupBy returns an object'); +assert.sameValue(Object.keys(obj).length, 0); diff --git a/test/built-ins/Object/groupBy/evenOdd.js b/test/built-ins/Object/groupBy/evenOdd.js new file mode 100644 index 00000000000..f884b2a3ce9 --- /dev/null +++ b/test/built-ins/Object/groupBy/evenOdd.js @@ -0,0 +1,22 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-object.groupby +description: Object.groupBy populates object with correct keys and values +info: | + Object.groupBy ( items, callbackfn ) + ... +includes: [compareArray.js] +features: [array-grouping] +---*/ + +const array = [1, 2, 3]; + +const obj = Object.groupBy(array, function (i) { + return i % 2 === 0 ? 'even' : 'odd'; +}); + +assert.compareArray(Object.keys(obj), ['odd', 'even']); +assert.compareArray(obj['even'], [2]); +assert.compareArray(obj['odd'], [1, 3]); diff --git a/test/built-ins/Array/prototype/group/groupLength.js b/test/built-ins/Object/groupBy/groupLength.js similarity index 50% rename from test/built-ins/Array/prototype/group/groupLength.js rename to test/built-ins/Object/groupBy/groupLength.js index 2123378e113..9be21a39686 100644 --- a/test/built-ins/Array/prototype/group/groupLength.js +++ b/test/built-ins/Object/groupBy/groupLength.js @@ -1,19 +1,18 @@ -// Copyright (c) 2021 Ecma International. All rights reserved. +// Copyright (c) 2023 Ecma International. All rights reserved. // This code is governed by the BSD license found in the LICENSE file. /*--- -esid: sec-array.prototype.group +esid: sec-object.groupby description: Callback can return numbers that are converted to property keys info: | - 22.1.3.14 Array.prototype.group ( callbackfn [ , thisArg ] ) + Object.groupBy ( items, callbackfn ) ... + GroupBy ( items, callbackfn, coercion ) - 8. For each Record { [[Key]], [[Elements]] } g of groups, do - - a. Let elements be ! CreateArrayFromList(g.[[Elements]]). - b. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements). - + 6. Repeat, + c. If next is false, then + i. Return groups. ... includes: [compareArray.js] features: [array-grouping] @@ -21,7 +20,7 @@ features: [array-grouping] const arr = ['hello', 'test', 'world']; -const obj = arr.group(function (i) { return i.length; }); +const obj = Object.groupBy(arr, function (i) { return i.length; }); assert.compareArray(Object.keys(obj), ['4', '5']); assert.compareArray(obj['5'], ['hello', 'world']); diff --git a/test/built-ins/Object/groupBy/invalid-callback.js b/test/built-ins/Object/groupBy/invalid-callback.js new file mode 100644 index 00000000000..f5348e4ff20 --- /dev/null +++ b/test/built-ins/Object/groupBy/invalid-callback.js @@ -0,0 +1,29 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-object.groupby +description: Object.groupBy called with non-callable throws TypeError +info: | + Object.groupBy ( items, callbackfn ) + + ... + GroupBy ( items, callbackfn, coercion ) + + 2. If IsCallable(callbackfn) is false, throw a TypeError exception. + ... +features: [array-grouping] +---*/ + + +assert.throws(TypeError, function() { + Object.groupBy([], null) +}, "null callback throws TypeError"); + +assert.throws(TypeError, function() { + Object.groupBy([], undefined) +}, "undefined callback throws TypeError"); + +assert.throws(TypeError, function() { + Object.groupBy([], {}) +}, "object callback throws TypeError"); diff --git a/test/built-ins/Object/groupBy/invalid-iterable.js b/test/built-ins/Object/groupBy/invalid-iterable.js new file mode 100644 index 00000000000..1f984a0f652 --- /dev/null +++ b/test/built-ins/Object/groupBy/invalid-iterable.js @@ -0,0 +1,34 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-object.groupby +description: Object.groupBy with a nullish Symbol.iterator throws +info: | + Object.groupBy ( items, callbackfn ) + + ... + GroupBy ( items, callbackfn, coercion ) + + 4. Let iteratorRecord be ? GetIterator(items). + + ... +features: [array-grouping] +---*/ + +const throws = function () { + throw new Test262Error('callback function should not be called') +}; + +function makeIterable(obj, iteratorFn) { + obj[Symbol.iterator] = iteratorFn; + return obj; +} + +assert.throws(TypeError, function () { + Object.groupBy(makeIterable({}, undefined), throws); +}, 'undefined Symbol.iterator'); + +assert.throws(TypeError, function () { + Object.groupBy(makeIterable({}, null), throws); +}, 'null Symbol.iterator'); diff --git a/test/built-ins/Object/groupBy/invalid-property-key.js b/test/built-ins/Object/groupBy/invalid-property-key.js new file mode 100644 index 00000000000..47d21f935f3 --- /dev/null +++ b/test/built-ins/Object/groupBy/invalid-property-key.js @@ -0,0 +1,31 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-object.groupby +description: Object.groupBy errors when callback return value cannot be converted to a property key. +info: | + Object.groupBy ( items, callbackfn ) + + ... + GroupBy ( items, callbackfn, coercion ) + + 6. Repeat, + g. If coercion is property, then + i. Set key to Completion(ToPropertyKey(key)). + ii. IfAbruptCloseIterator(key, iteratorRecord). + + ... +features: [array-grouping] +---*/ + +assert.throws(Test262Error, function () { + const array = [1]; + Object.groupBy(array, function () { + return { + toString() { + throw new Test262Error('not a property key'); + } + }; + }) +}); diff --git a/test/built-ins/Object/groupBy/iterator-next-throws.js b/test/built-ins/Object/groupBy/iterator-next-throws.js new file mode 100644 index 00000000000..97ee2a4135d --- /dev/null +++ b/test/built-ins/Object/groupBy/iterator-next-throws.js @@ -0,0 +1,31 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-object.groupby +description: Object.groupBy throws when iterator next throws +info: | + Object.groupBy ( items, callbackfn ) + + ... + + GroupBy ( items, callbackfn, coercion ) + + 6. Repeat, + b. Let next be ? IteratorStep(iteratorRecord). + + ... +features: [array-grouping] +---*/ + +const throwingIterator = { + next: function next() { + throw new Test262Error('next() method was called'); + } +}; + +assert.throws(Test262Error, function () { + Object.groupBy(throwingIterator, function () { + return 'key'; + }); +}); diff --git a/test/built-ins/Object/groupBy/length.js b/test/built-ins/Object/groupBy/length.js new file mode 100644 index 00000000000..6d7c1ff6d57 --- /dev/null +++ b/test/built-ins/Object/groupBy/length.js @@ -0,0 +1,25 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-object.groupby +description: Object.groupBy property length descriptor +info: | + Object.groupBy ( items, callbackfn ) + + ... + + 17 ECMAScript Standard Built-in Objects + + ... + +includes: [propertyHelper.js] +features: [array-grouping] +---*/ + +verifyProperty(Object.groupBy, "length", { + value: 2, + enumerable: false, + writable: false, + configurable: true +}); diff --git a/test/built-ins/Object/groupBy/name.js b/test/built-ins/Object/groupBy/name.js new file mode 100644 index 00000000000..6e4a96df67a --- /dev/null +++ b/test/built-ins/Object/groupBy/name.js @@ -0,0 +1,25 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-object.groupby +description: Object.groupBy property name descriptor +info: | + Object.groupBy ( items, callbackfn ) + + ... + + 17 ECMAScript Standard Built-in Objects + + ... + +includes: [propertyHelper.js] +features: [array-grouping] +---*/ + +verifyProperty(Object.groupBy, "name", { + value: "groupBy", + enumerable: false, + writable: false, + configurable: true +}); diff --git a/test/built-ins/Object/groupBy/null-prototype.js b/test/built-ins/Object/groupBy/null-prototype.js new file mode 100644 index 00000000000..d37fcb03997 --- /dev/null +++ b/test/built-ins/Object/groupBy/null-prototype.js @@ -0,0 +1,27 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-object.groupby +description: Object.groupBy returns a null prototype object +info: | + Object.groupBy ( items, callbackfn ) + + ... + + 2. Let obj be OrdinaryObjectCreate(null). + ... + 4. Return obj. + + ... +features: [array-grouping] +---*/ + +const array = [1, 2, 3]; + +const obj = Object.groupBy(array, function (i) { + return i % 2 === 0 ? 'even' : 'odd'; +}); + +assert.sameValue(Object.getPrototypeOf(obj), null); +assert.sameValue(obj.hasOwnProperty, undefined); diff --git a/test/built-ins/Object/groupBy/toPropertyKey.js b/test/built-ins/Object/groupBy/toPropertyKey.js new file mode 100644 index 00000000000..df3734fc0ca --- /dev/null +++ b/test/built-ins/Object/groupBy/toPropertyKey.js @@ -0,0 +1,36 @@ +// Copyright (c) 2023 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-object.groupby +description: Object.groupBy coerces return value with ToPropertyKey +info: | + Object.groupBy ( items, callbackfn ) + + ... + + GroupBy ( items, callbackfn, coercion ) + + 6. Repeat, + g. If coercion is property, then + i. Set key to Completion(ToPropertyKey(key)). + ii. IfAbruptCloseIterator(key, iteratorRecord). + + ... +includes: [compareArray.js] +features: [array-grouping] +---*/ + +let calls = 0; +const stringable = { + toString: function toString() { + return 1; + } +} + +const array = [1, '1', stringable]; + +const obj = Object.groupBy(array, function (v) { return v; }); + +assert.compareArray(Object.keys(obj), ['1']); +assert.compareArray(obj['1'], [1, '1', stringable]);