From 7b95fe8a971ab8de7e8f88b143def0d0d0a313aa Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Tue, 24 May 2022 01:19:49 +0700 Subject: [PATCH] rename methods from the `Array` grouping proposal https://github.com/tc39/proposal-array-grouping/pull/39 --- CHANGELOG.md | 4 +- README.md | 18 ++++----- packages/core-js-compat/src/data.mjs | 6 +++ .../src/modules-by-versions.mjs | 4 ++ packages/core-js/actual/array/group-to-map.js | 6 +++ packages/core-js/actual/array/group.js | 4 ++ packages/core-js/actual/array/index.js | 2 + .../actual/array/virtual/group-to-map.js | 6 +++ .../core-js/actual/array/virtual/group.js | 4 ++ .../core-js/actual/array/virtual/index.js | 2 + .../core-js/actual/instance/group-to-map.js | 9 +++++ packages/core-js/actual/instance/group.js | 9 +++++ packages/core-js/full/array/group-to-map.js | 3 ++ packages/core-js/full/array/group.js | 3 ++ .../full/array/virtual/group-to-map.js | 3 ++ packages/core-js/full/array/virtual/group.js | 3 ++ .../core-js/full/instance/group-to-map.js | 3 ++ packages/core-js/full/instance/group.js | 3 ++ .../core-js/internals/array-group-to-map.js | 32 +++++++++++++++ .../{array-group-by.js => array-group.js} | 0 .../modules/esnext.array.group-by-to-map.js | 34 ++-------------- .../core-js/modules/esnext.array.group-by.js | 5 ++- .../modules/esnext.array.group-to-map.js | 11 ++++++ .../core-js/modules/esnext.array.group.js | 15 +++++++ .../modules/esnext.typed-array.group-by.js | 4 +- .../proposals/array-grouping-stage-3-2.js | 3 ++ .../proposals/array-grouping-stage-3.js | 1 + packages/core-js/stage/3.js | 4 +- scripts/check-compat-tests.mjs | 2 + tests/commonjs.mjs | 17 ++++++++ tests/compat/tests.js | 12 +++--- tests/pure/esnext.array.group-to-map.js | 37 ++++++++++++++++++ tests/pure/esnext.array.group.js | 36 +++++++++++++++++ tests/tests/esnext.array.group-by-to-map.js | 1 - tests/tests/esnext.array.group-to-map.js | 39 +++++++++++++++++++ tests/tests/esnext.array.group.js | 39 +++++++++++++++++++ 36 files changed, 332 insertions(+), 52 deletions(-) create mode 100644 packages/core-js/actual/array/group-to-map.js create mode 100644 packages/core-js/actual/array/group.js create mode 100644 packages/core-js/actual/array/virtual/group-to-map.js create mode 100644 packages/core-js/actual/array/virtual/group.js create mode 100644 packages/core-js/actual/instance/group-to-map.js create mode 100644 packages/core-js/actual/instance/group.js create mode 100644 packages/core-js/full/array/group-to-map.js create mode 100644 packages/core-js/full/array/group.js create mode 100644 packages/core-js/full/array/virtual/group-to-map.js create mode 100644 packages/core-js/full/array/virtual/group.js create mode 100644 packages/core-js/full/instance/group-to-map.js create mode 100644 packages/core-js/full/instance/group.js create mode 100644 packages/core-js/internals/array-group-to-map.js rename packages/core-js/internals/{array-group-by.js => array-group.js} (100%) create mode 100644 packages/core-js/modules/esnext.array.group-to-map.js create mode 100644 packages/core-js/modules/esnext.array.group.js create mode 100644 packages/core-js/proposals/array-grouping-stage-3-2.js create mode 100644 tests/pure/esnext.array.group-to-map.js create mode 100644 tests/pure/esnext.array.group.js create mode 100644 tests/tests/esnext.array.group-to-map.js create mode 100644 tests/tests/esnext.array.group.js diff --git a/CHANGELOG.md b/CHANGELOG.md index e391210ee435..6382d999c17a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ## Changelog ##### Unreleased -- Nothing +- Methods from [the `Array` grouping proposal](https://github.com/tc39/proposal-array-grouping) [renamed](https://github.com/tc39/proposal-array-grouping/pull/39): + - `Array.prototype.groupBy` -> `Array.prototype.group` + - `Array.prototype.groupByToMap` -> `Array.prototype.groupToMap` ##### [3.22.6 - 2022.05.23](https://github.com/zloirock/core-js/releases/tag/v3.22.6) - Fixed possible double call of `ToNumber` conversion on arguments of `Math.{ fround, trunc }` polyfills diff --git a/README.md b/README.md index 8d1116f831ba..3af34ca9b515 100644 --- a/README.md +++ b/README.md @@ -2071,24 +2071,24 @@ core-js/proposals/well-formed-stringify core-js(-pure)/stage/3 ``` ##### [`Array` grouping](https://github.com/tc39/proposal-array-grouping)[⬆](#index) -Modules [`esnext.array.group-by`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.group-by.js), [`esnext.array.group-by-to-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.group-by-to-map.js). +Modules [`esnext.array.group`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.group.js), [`esnext.array.group-to-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.group-to-map.js). ```js class Array { - groupBy(callbackfn: (value: any, index: number, target: any) => key, thisArg?: any): { [key]: Array }; - groupByToMap(callbackfn: (value: any, index: number, target: any) => key, thisArg?: any): Map>; + group(callbackfn: (value: any, index: number, target: any) => key, thisArg?: any): { [key]: Array }; + groupToMap(callbackfn: (value: any, index: number, target: any) => key, thisArg?: any): Map>; } ``` [*CommonJS entry points:*](#commonjs-api) ``` -core-js/proposals/array-grouping-stage-3 -core-js(-pure)/actual|full/array(/virtual)/group-by -core-js(-pure)/actual|full/array(/virtual)/group-by-to-map +core-js/proposals/array-grouping-stage-3-2 +core-js(-pure)/actual|full/array(/virtual)/group +core-js(-pure)/actual|full/array(/virtual)/group-to-map ``` -[*Examples*](t.ly/xEqc): +[*Examples*](is.gd/3a0PbH): ```js -[1, 2, 3, 4, 5].groupBy(it => it % 2); // => { 1: [1, 3, 5], 0: [2, 4] } +[1, 2, 3, 4, 5].group(it => it % 2); // => { 1: [1, 3, 5], 0: [2, 4] } -const map = [1, 2, 3, 4, 5].groupByToMap(it => it % 2); +const map = [1, 2, 3, 4, 5].groupToMap(it => it % 2); map.get(1); // => [1, 3, 5] map.get(0); // => [2, 4] ```` diff --git a/packages/core-js-compat/src/data.mjs b/packages/core-js-compat/src/data.mjs index 8cd6dfefbe02..0b0704d6450b 100644 --- a/packages/core-js-compat/src/data.mjs +++ b/packages/core-js-compat/src/data.mjs @@ -1579,10 +1579,16 @@ export const data = { chrome: '97', safari: '15.4', }, + 'esnext.array.group': { + }, + // TODO: Remove from `core-js@4` 'esnext.array.group-by': { }, + // TODO: Remove from `core-js@4` 'esnext.array.group-by-to-map': { }, + 'esnext.array.group-to-map': { + }, 'esnext.array.is-template-object': { }, // TODO: Remove from `core-js@4` diff --git a/packages/core-js-compat/src/modules-by-versions.mjs b/packages/core-js-compat/src/modules-by-versions.mjs index 9d27f0fab9d2..81735a8ad962 100644 --- a/packages/core-js-compat/src/modules-by-versions.mjs +++ b/packages/core-js-compat/src/modules-by-versions.mjs @@ -139,4 +139,8 @@ export default { 'web.atob', 'web.btoa', ], + 3.23: [ + 'esnext.array.group', + 'esnext.array.group-to-map', + ], }; diff --git a/packages/core-js/actual/array/group-to-map.js b/packages/core-js/actual/array/group-to-map.js new file mode 100644 index 000000000000..fc0cbd4c839f --- /dev/null +++ b/packages/core-js/actual/array/group-to-map.js @@ -0,0 +1,6 @@ +require('../../modules/es.map'); +require('../../modules/es.object.to-string'); +require('../../modules/esnext.array.group-to-map'); +var entryUnbind = require('../../internals/entry-unbind'); + +module.exports = entryUnbind('Array', 'groupToMap'); diff --git a/packages/core-js/actual/array/group.js b/packages/core-js/actual/array/group.js new file mode 100644 index 000000000000..d2be21882fc8 --- /dev/null +++ b/packages/core-js/actual/array/group.js @@ -0,0 +1,4 @@ +require('../../modules/esnext.array.group'); +var entryUnbind = require('../../internals/entry-unbind'); + +module.exports = entryUnbind('Array', 'group'); diff --git a/packages/core-js/actual/array/index.js b/packages/core-js/actual/array/index.js index 230426a722e9..0f52231c3bf0 100644 --- a/packages/core-js/actual/array/index.js +++ b/packages/core-js/actual/array/index.js @@ -3,8 +3,10 @@ require('../../modules/es.map'); require('../../modules/es.object.to-string'); require('../../modules/esnext.array.find-last'); require('../../modules/esnext.array.find-last-index'); +require('../../modules/esnext.array.group'); require('../../modules/esnext.array.group-by'); require('../../modules/esnext.array.group-by-to-map'); +require('../../modules/esnext.array.group-to-map'); require('../../modules/esnext.array.to-reversed'); require('../../modules/esnext.array.to-sorted'); require('../../modules/esnext.array.to-spliced'); diff --git a/packages/core-js/actual/array/virtual/group-to-map.js b/packages/core-js/actual/array/virtual/group-to-map.js new file mode 100644 index 000000000000..e2c9c5a4ebaa --- /dev/null +++ b/packages/core-js/actual/array/virtual/group-to-map.js @@ -0,0 +1,6 @@ +require('../../../modules/es.map'); +require('../../../modules/es.object.to-string'); +require('../../../modules/esnext.array.group-to-map'); +var entryVirtual = require('../../../internals/entry-virtual'); + +module.exports = entryVirtual('Array').groupToMap; diff --git a/packages/core-js/actual/array/virtual/group.js b/packages/core-js/actual/array/virtual/group.js new file mode 100644 index 000000000000..a7baaccb7039 --- /dev/null +++ b/packages/core-js/actual/array/virtual/group.js @@ -0,0 +1,4 @@ +require('../../../modules/esnext.array.group'); +var entryVirtual = require('../../../internals/entry-virtual'); + +module.exports = entryVirtual('Array').group; diff --git a/packages/core-js/actual/array/virtual/index.js b/packages/core-js/actual/array/virtual/index.js index a0f9910fd585..a4579d29e524 100644 --- a/packages/core-js/actual/array/virtual/index.js +++ b/packages/core-js/actual/array/virtual/index.js @@ -3,8 +3,10 @@ require('../../../modules/es.map'); require('../../../modules/es.object.to-string'); require('../../../modules/esnext.array.find-last'); require('../../../modules/esnext.array.find-last-index'); +require('../../../modules/esnext.array.group'); require('../../../modules/esnext.array.group-by'); require('../../../modules/esnext.array.group-by-to-map'); +require('../../../modules/esnext.array.group-to-map'); require('../../../modules/esnext.array.to-reversed'); require('../../../modules/esnext.array.to-sorted'); require('../../../modules/esnext.array.to-spliced'); diff --git a/packages/core-js/actual/instance/group-to-map.js b/packages/core-js/actual/instance/group-to-map.js new file mode 100644 index 000000000000..6c49d5433d2e --- /dev/null +++ b/packages/core-js/actual/instance/group-to-map.js @@ -0,0 +1,9 @@ +var isPrototypeOf = require('../../internals/object-is-prototype-of'); +var method = require('../array/virtual/group-to-map'); + +var ArrayPrototype = Array.prototype; + +module.exports = function (it) { + var own = it.groupToMap; + return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.groupToMap) ? method : own; +}; diff --git a/packages/core-js/actual/instance/group.js b/packages/core-js/actual/instance/group.js new file mode 100644 index 000000000000..6053cd2865e2 --- /dev/null +++ b/packages/core-js/actual/instance/group.js @@ -0,0 +1,9 @@ +var isPrototypeOf = require('../../internals/object-is-prototype-of'); +var method = require('../array/virtual/group'); + +var ArrayPrototype = Array.prototype; + +module.exports = function (it) { + var own = it.group; + return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.group) ? method : own; +}; diff --git a/packages/core-js/full/array/group-to-map.js b/packages/core-js/full/array/group-to-map.js new file mode 100644 index 000000000000..45cbbbd8bfd7 --- /dev/null +++ b/packages/core-js/full/array/group-to-map.js @@ -0,0 +1,3 @@ +var parent = require('../../actual/array/group-to-map'); + +module.exports = parent; diff --git a/packages/core-js/full/array/group.js b/packages/core-js/full/array/group.js new file mode 100644 index 000000000000..615c24946cdb --- /dev/null +++ b/packages/core-js/full/array/group.js @@ -0,0 +1,3 @@ +var parent = require('../../actual/array/group'); + +module.exports = parent; diff --git a/packages/core-js/full/array/virtual/group-to-map.js b/packages/core-js/full/array/virtual/group-to-map.js new file mode 100644 index 000000000000..d0905abf5e38 --- /dev/null +++ b/packages/core-js/full/array/virtual/group-to-map.js @@ -0,0 +1,3 @@ +var parent = require('../../../actual/array/virtual/group-to-map'); + +module.exports = parent; diff --git a/packages/core-js/full/array/virtual/group.js b/packages/core-js/full/array/virtual/group.js new file mode 100644 index 000000000000..fa93f7a592de --- /dev/null +++ b/packages/core-js/full/array/virtual/group.js @@ -0,0 +1,3 @@ +var parent = require('../../../actual/array/virtual/group'); + +module.exports = parent; diff --git a/packages/core-js/full/instance/group-to-map.js b/packages/core-js/full/instance/group-to-map.js new file mode 100644 index 000000000000..4f4dc61b4941 --- /dev/null +++ b/packages/core-js/full/instance/group-to-map.js @@ -0,0 +1,3 @@ +var parent = require('../../actual/instance/group-to-map'); + +module.exports = parent; diff --git a/packages/core-js/full/instance/group.js b/packages/core-js/full/instance/group.js new file mode 100644 index 000000000000..12e088e9aab9 --- /dev/null +++ b/packages/core-js/full/instance/group.js @@ -0,0 +1,3 @@ +var parent = require('../../actual/instance/group'); + +module.exports = parent; diff --git a/packages/core-js/internals/array-group-to-map.js b/packages/core-js/internals/array-group-to-map.js new file mode 100644 index 000000000000..dc5cc90817f4 --- /dev/null +++ b/packages/core-js/internals/array-group-to-map.js @@ -0,0 +1,32 @@ +'use strict'; +var getBuiltIn = require('../internals/get-built-in'); +var bind = require('../internals/function-bind-context'); +var uncurryThis = require('../internals/function-uncurry-this'); +var IndexedObject = require('../internals/indexed-object'); +var toObject = require('../internals/to-object'); +var lengthOfArrayLike = require('../internals/length-of-array-like'); + +var Map = getBuiltIn('Map'); +var MapPrototype = Map.prototype; +var mapGet = uncurryThis(MapPrototype.get); +var mapHas = uncurryThis(MapPrototype.has); +var mapSet = uncurryThis(MapPrototype.set); +var push = uncurryThis([].push); + +// `Array.prototype.groupToMap` method +// https://github.com/tc39/proposal-array-grouping +module.exports = function groupToMap(callbackfn /* , thisArg */) { + var O = toObject(this); + var self = IndexedObject(O); + var boundFunction = bind(callbackfn, arguments.length > 1 ? arguments[1] : undefined); + var map = new Map(); + var length = lengthOfArrayLike(self); + var index = 0; + var key, value; + for (;length > index; index++) { + value = self[index]; + key = boundFunction(value, index, O); + if (mapHas(map, key)) push(mapGet(map, key), value); + else mapSet(map, key, [value]); + } return map; +}; diff --git a/packages/core-js/internals/array-group-by.js b/packages/core-js/internals/array-group.js similarity index 100% rename from packages/core-js/internals/array-group-by.js rename to packages/core-js/internals/array-group.js diff --git a/packages/core-js/modules/esnext.array.group-by-to-map.js b/packages/core-js/modules/esnext.array.group-by-to-map.js index a7756468d950..72bd89c4574f 100644 --- a/packages/core-js/modules/esnext.array.group-by-to-map.js +++ b/packages/core-js/modules/esnext.array.group-by-to-map.js @@ -1,40 +1,14 @@ -'use strict'; +// TODO: Remove from `core-js@4` var $ = require('../internals/export'); -var getBuiltIn = require('../internals/get-built-in'); -var bind = require('../internals/function-bind-context'); -var uncurryThis = require('../internals/function-uncurry-this'); -var IndexedObject = require('../internals/indexed-object'); -var toObject = require('../internals/to-object'); -var lengthOfArrayLike = require('../internals/length-of-array-like'); var arrayMethodIsStrict = require('../internals/array-method-is-strict'); var addToUnscopables = require('../internals/add-to-unscopables'); - -var Map = getBuiltIn('Map'); -var MapPrototype = Map.prototype; -var mapGet = uncurryThis(MapPrototype.get); -var mapHas = uncurryThis(MapPrototype.has); -var mapSet = uncurryThis(MapPrototype.set); -var push = uncurryThis([].push); +var $groupToMap = require('../internals/array-group-to-map'); // `Array.prototype.groupByToMap` method // https://github.com/tc39/proposal-array-grouping // https://bugs.webkit.org/show_bug.cgi?id=236541 -$({ target: 'Array', proto: true, forced: !arrayMethodIsStrict('groupByToMap') }, { - groupByToMap: function groupByToMap(callbackfn /* , thisArg */) { - var O = toObject(this); - var self = IndexedObject(O); - var boundFunction = bind(callbackfn, arguments.length > 1 ? arguments[1] : undefined); - var map = new Map(); - var length = lengthOfArrayLike(self); - var index = 0; - var key, value; - for (;length > index; index++) { - value = self[index]; - key = boundFunction(value, index, O); - if (mapHas(map, key)) push(mapGet(map, key), value); - else mapSet(map, key, [value]); - } return map; - } +$({ target: 'Array', proto: true, name: 'groupToMap', forced: !arrayMethodIsStrict('groupByToMap') }, { + groupByToMap: $groupToMap }); addToUnscopables('groupByToMap'); diff --git a/packages/core-js/modules/esnext.array.group-by.js b/packages/core-js/modules/esnext.array.group-by.js index c2977e3c153d..f5b9abf8ef76 100644 --- a/packages/core-js/modules/esnext.array.group-by.js +++ b/packages/core-js/modules/esnext.array.group-by.js @@ -1,6 +1,7 @@ 'use strict'; +// TODO: Remove from `core-js@4` var $ = require('../internals/export'); -var $groupBy = require('../internals/array-group-by'); +var $group = require('../internals/array-group'); var arrayMethodIsStrict = require('../internals/array-method-is-strict'); var addToUnscopables = require('../internals/add-to-unscopables'); @@ -10,7 +11,7 @@ var addToUnscopables = require('../internals/add-to-unscopables'); $({ target: 'Array', proto: true, forced: !arrayMethodIsStrict('groupBy') }, { groupBy: function groupBy(callbackfn /* , thisArg */) { var thisArg = arguments.length > 1 ? arguments[1] : undefined; - return $groupBy(this, callbackfn, thisArg); + return $group(this, callbackfn, thisArg); } }); diff --git a/packages/core-js/modules/esnext.array.group-to-map.js b/packages/core-js/modules/esnext.array.group-to-map.js new file mode 100644 index 000000000000..843f6c8c148e --- /dev/null +++ b/packages/core-js/modules/esnext.array.group-to-map.js @@ -0,0 +1,11 @@ +var $ = require('../internals/export'); +var addToUnscopables = require('../internals/add-to-unscopables'); +var $groupToMap = require('../internals/array-group-to-map'); + +// `Array.prototype.groupToMap` method +// https://github.com/tc39/proposal-array-grouping +$({ target: 'Array', proto: true }, { + groupToMap: $groupToMap +}); + +addToUnscopables('groupToMap'); diff --git a/packages/core-js/modules/esnext.array.group.js b/packages/core-js/modules/esnext.array.group.js new file mode 100644 index 000000000000..9afca8c998b4 --- /dev/null +++ b/packages/core-js/modules/esnext.array.group.js @@ -0,0 +1,15 @@ +'use strict'; +var $ = require('../internals/export'); +var $group = require('../internals/array-group'); +var addToUnscopables = require('../internals/add-to-unscopables'); + +// `Array.prototype.group` method +// https://github.com/tc39/proposal-array-grouping +$({ target: 'Array', proto: true }, { + group: function group(callbackfn /* , thisArg */) { + var thisArg = arguments.length > 1 ? arguments[1] : undefined; + return $group(this, callbackfn, thisArg); + } +}); + +addToUnscopables('group'); 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 5b43e558127d..c180bc20b519 100644 --- a/packages/core-js/modules/esnext.typed-array.group-by.js +++ b/packages/core-js/modules/esnext.typed-array.group-by.js @@ -1,7 +1,7 @@ 'use strict'; // TODO: Remove from `core-js@4` var ArrayBufferViewCore = require('../internals/array-buffer-view-core'); -var $groupBy = require('../internals/array-group-by'); +var $group = require('../internals/array-group'); var typedArraySpeciesConstructor = require('../internals/typed-array-species-constructor'); var aTypedArray = ArrayBufferViewCore.aTypedArray; @@ -11,5 +11,5 @@ var exportTypedArrayMethod = ArrayBufferViewCore.exportTypedArrayMethod; // https://github.com/tc39/proposal-array-grouping exportTypedArrayMethod('groupBy', function groupBy(callbackfn /* , thisArg */) { var thisArg = arguments.length > 1 ? arguments[1] : undefined; - return $groupBy(aTypedArray(this), callbackfn, thisArg, typedArraySpeciesConstructor); + return $group(aTypedArray(this), callbackfn, thisArg, typedArraySpeciesConstructor); }, true); diff --git a/packages/core-js/proposals/array-grouping-stage-3-2.js b/packages/core-js/proposals/array-grouping-stage-3-2.js new file mode 100644 index 000000000000..31bf43aee18c --- /dev/null +++ b/packages/core-js/proposals/array-grouping-stage-3-2.js @@ -0,0 +1,3 @@ +// https://github.com/tc39/proposal-array-grouping +require('../modules/esnext.array.group'); +require('../modules/esnext.array.group-to-map'); diff --git a/packages/core-js/proposals/array-grouping-stage-3.js b/packages/core-js/proposals/array-grouping-stage-3.js index 03df3eb18415..d60cab46d56e 100644 --- a/packages/core-js/proposals/array-grouping-stage-3.js +++ b/packages/core-js/proposals/array-grouping-stage-3.js @@ -1,3 +1,4 @@ // https://github.com/tc39/proposal-array-grouping +// TODO: Remove from `core-js@4` require('../modules/esnext.array.group-by'); require('../modules/esnext.array.group-by-to-map'); diff --git a/packages/core-js/stage/3.js b/packages/core-js/stage/3.js index 9aa51d79eb00..bbcfac5825f1 100644 --- a/packages/core-js/stage/3.js +++ b/packages/core-js/stage/3.js @@ -1,6 +1,8 @@ require('../proposals/array-find-from-last'); -require('../proposals/array-grouping-stage-3'); +require('../proposals/array-grouping-stage-3-2'); require('../proposals/change-array-by-copy'); +// TODO: Obsolete versions, remove from `core-js@4` +require('../proposals/array-grouping-stage-3'); var parent = require('./4'); module.exports = parent; diff --git a/scripts/check-compat-tests.mjs b/scripts/check-compat-tests.mjs index 8067fe327130..81b29188592e 100644 --- a/scripts/check-compat-tests.mjs +++ b/scripts/check-compat-tests.mjs @@ -15,6 +15,8 @@ const ignore = new Set([ 'es.weak-set', 'esnext.aggregate-error', 'esnext.array.filter-out', + 'esnext.array.group-by', + 'esnext.array.group-by-to-map', 'esnext.array.last-index', 'esnext.array.last-item', 'esnext.map.update-or-insert', diff --git a/tests/commonjs.mjs b/tests/commonjs.mjs index b65ef5c4d59e..8eeb70c1d078 100644 --- a/tests/commonjs.mjs +++ b/tests/commonjs.mjs @@ -565,6 +565,8 @@ for (PATH of ['core-js-pure', 'core-js']) { for (const NS of ['actual', 'full', 'features']) { ok(load(NS, 'array/find-last')([1, 2, 3], it => it % 2) === 3); ok(load(NS, 'array/find-last-index')([1, 2, 3], it => it % 2) === 2); + ok(typeof load(NS, 'array/group') == 'function'); + ok(typeof load(NS, 'array/group-to-map') == 'function'); ok(typeof load(NS, 'array/group-by') == 'function'); ok(typeof load(NS, 'array/group-by-to-map') == 'function'); ok(load(NS, 'array/with')([1, 2, 3], 1, 4)); @@ -573,6 +575,8 @@ for (PATH of ['core-js-pure', 'core-js']) { ok(load(NS, 'array/to-spliced')([3, 2, 1], 1, 1, 4, 5).length === 4); ok(load(NS, 'array/virtual/find-last').call([1, 2, 3], it => it % 2) === 3); ok(load(NS, 'array/virtual/find-last-index').call([1, 2, 3], it => it % 2) === 2); + ok(typeof load(NS, 'array/virtual/group') == 'function'); + ok(typeof load(NS, 'array/virtual/group-to-map') == 'function'); ok(typeof load(NS, 'array/virtual/group-by') == 'function'); ok(typeof load(NS, 'array/virtual/group-by-to-map') == 'function'); ok(load(NS, 'array/virtual/with').call([1, 2, 3], 1, 4)); @@ -592,6 +596,18 @@ for (PATH of ['core-js-pure', 'core-js']) { ok(typeof instanceFindLast([]) == 'function'); ok(instanceFindLast([]).call([1, 2, 3], it => it % 2) === 3); + const instanceGroup = load(NS, 'instance/group'); + ok(typeof instanceGroup == 'function'); + ok(instanceGroup({}) === undefined); + ok(typeof instanceGroup([]) == 'function'); + ok(instanceGroup([]).call([1, 2, 3], it => it % 2)[1].length === 2); + + const instanceGroupToMap = load(NS, 'instance/group-to-map'); + ok(typeof instanceGroupToMap == 'function'); + ok(instanceGroupToMap({}) === undefined); + ok(typeof instanceGroupToMap([]) == 'function'); + ok(instanceGroupToMap([]).call([1, 2, 3], it => it % 2).get(1).length === 2); + const instanceGroupBy = load(NS, 'instance/group-by'); ok(typeof instanceGroupBy == 'function'); ok(instanceGroupBy({}) === undefined); @@ -808,6 +824,7 @@ for (PATH of ['core-js-pure', 'core-js']) { load('proposals/array-from-async-stage-2'); load('proposals/array-grouping'); load('proposals/array-grouping-stage-3'); + load('proposals/array-grouping-stage-3-2'); load('proposals/array-includes'); load('proposals/array-is-template-object'); load('proposals/array-last'); diff --git a/tests/compat/tests.js b/tests/compat/tests.js index 15558e7c28ca..363551c3ce2c 100644 --- a/tests/compat/tests.js +++ b/tests/compat/tests.js @@ -1331,21 +1331,21 @@ GLOBAL.tests = { 'esnext.array.find-last-index': function () { return [].findLastIndex; }, - 'esnext.array.group-by': function () { + 'esnext.array.group': function () { try { // https://bugs.webkit.org/show_bug.cgi?id=236541 - Array.prototype.groupBy.call(null, function () { /* empty */ }); + Array.prototype.group.call(null, function () { /* empty */ }); return false; } catch (error) { /* empty */ } - return Array.prototype.groupBy; + return Array.prototype.group; }, - 'esnext.array.group-by-to-map': function () { + 'esnext.array.group-to-map': function () { try { // https://bugs.webkit.org/show_bug.cgi?id=236541 - Array.prototype.groupByToMap.call(null, function () { /* empty */ }); + Array.prototype.groupToMap.call(null, function () { /* empty */ }); return false; } catch (error) { /* empty */ } - return Array.prototype.groupByToMap; + return Array.prototype.groupToMap; }, 'esnext.array.is-template-object': function () { return Array.isTemplateObject; diff --git a/tests/pure/esnext.array.group-to-map.js b/tests/pure/esnext.array.group-to-map.js new file mode 100644 index 000000000000..7121f485a42e --- /dev/null +++ b/tests/pure/esnext.array.group-to-map.js @@ -0,0 +1,37 @@ +import { STRICT } from '../helpers/constants'; + +import Map from 'core-js-pure/es/map'; +import Symbol from 'core-js-pure/es/symbol'; +import from from 'core-js-pure/es/array/from'; +import groupToMap from 'core-js-pure/actual/array/group-to-map'; + +QUnit.test('Array#groupToMap', assert => { + assert.isFunction(groupToMap); + let array = [1]; + const context = {}; + groupToMap(array, function (value, key, that) { + assert.same(arguments.length, 3, 'correct number of callback arguments'); + assert.same(value, 1, 'correct value in callback'); + assert.same(key, 0, 'correct index in callback'); + assert.same(that, array, 'correct link to array in callback'); + assert.same(this, context, 'correct callback context'); + }, context); + assert.true(groupToMap([], it => it) instanceof Map, 'returns Map'); + assert.deepEqual(from(groupToMap([1, 2, 3], it => it % 2)), [[1, [1, 3]], [0, [2]]], '#1'); + assert.deepEqual( + from(groupToMap([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], it => `i${ it % 5 }`)), + [['i1', [1, 6, 11]], ['i2', [2, 7, 12]], ['i3', [3, 8]], ['i4', [4, 9]], ['i0', [5, 10]]], + '#2', + ); + assert.deepEqual(from(groupToMap(Array(3), it => it)), [[undefined, [undefined, undefined, undefined]]], '#3'); + if (STRICT) { + assert.throws(() => groupToMap(null, () => { /* empty */ }), TypeError, 'null this -> TypeError'); + assert.throws(() => groupToMap(undefined, () => { /* empty */ }), TypeError, 'undefined this -> TypeError'); + } + array = [1]; + // eslint-disable-next-line object-shorthand -- constructor + array.constructor = { [Symbol.species]: function () { + return { foo: 1 }; + } }; + assert.same(groupToMap(array, Boolean).get(true).foo, undefined, 'no @@species'); +}); diff --git a/tests/pure/esnext.array.group.js b/tests/pure/esnext.array.group.js new file mode 100644 index 000000000000..9a862525469d --- /dev/null +++ b/tests/pure/esnext.array.group.js @@ -0,0 +1,36 @@ +import { STRICT } from '../helpers/constants'; + +import Symbol from 'core-js-pure/es/symbol'; +import getPrototypeOf from 'core-js-pure/es/object/get-prototype-of'; +import group from 'core-js-pure/actual/array/group'; + +QUnit.test('Array#group', assert => { + assert.isFunction(group); + let array = [1]; + const context = {}; + group(array, function (value, key, that) { + assert.same(arguments.length, 3, 'correct number of callback arguments'); + assert.same(value, 1, 'correct value in callback'); + assert.same(key, 0, 'correct index in callback'); + assert.same(that, array, 'correct link to array in callback'); + assert.same(this, context, 'correct callback context'); + }, context); + assert.same(getPrototypeOf(group([], it => it)), null, 'null proto'); + assert.deepEqual(group([1, 2, 3], it => it % 2), { 1: [1, 3], 0: [2] }, '#1'); + assert.deepEqual( + group([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], it => `i${ it % 5 }`), + { i1: [1, 6, 11], i2: [2, 7, 12], i3: [3, 8], i4: [4, 9], i0: [5, 10] }, + '#2', + ); + assert.deepEqual(group(Array(3), it => it), { undefined: [undefined, undefined, undefined] }, '#3'); + if (STRICT) { + assert.throws(() => group(null, () => { /* empty */ }), TypeError, 'null this -> TypeError'); + assert.throws(() => group(undefined, () => { /* empty */ }), TypeError, 'undefined this -> TypeError'); + } + array = [1]; + // eslint-disable-next-line object-shorthand -- constructor + array.constructor = { [Symbol.species]: function () { + return { foo: 1 }; + } }; + assert.same(group(array, Boolean).true.foo, undefined, 'no @@species'); +}); diff --git a/tests/tests/esnext.array.group-by-to-map.js b/tests/tests/esnext.array.group-by-to-map.js index 38d4d1af38b4..25b72ccc3c61 100644 --- a/tests/tests/esnext.array.group-by-to-map.js +++ b/tests/tests/esnext.array.group-by-to-map.js @@ -6,7 +6,6 @@ QUnit.test('Array#groupByToMap', assert => { const { groupByToMap } = Array.prototype; assert.isFunction(groupByToMap); assert.arity(groupByToMap, 1); - assert.name(groupByToMap, 'groupByToMap'); assert.looksNative(groupByToMap); assert.nonEnumerable(Array.prototype, 'groupByToMap'); let array = [1]; diff --git a/tests/tests/esnext.array.group-to-map.js b/tests/tests/esnext.array.group-to-map.js new file mode 100644 index 000000000000..32470d5d46e4 --- /dev/null +++ b/tests/tests/esnext.array.group-to-map.js @@ -0,0 +1,39 @@ +import { STRICT } from '../helpers/constants'; + +const { from } = Array; + +QUnit.test('Array#groupToMap', assert => { + const { groupToMap } = Array.prototype; + assert.isFunction(groupToMap); + assert.arity(groupToMap, 1); + assert.name(groupToMap, 'groupToMap'); + assert.looksNative(groupToMap); + assert.nonEnumerable(Array.prototype, 'groupToMap'); + let array = [1]; + const context = {}; + array.groupToMap(function (value, key, that) { + assert.same(arguments.length, 3, 'correct number of callback arguments'); + assert.same(value, 1, 'correct value in callback'); + assert.same(key, 0, 'correct index in callback'); + assert.same(that, array, 'correct link to array in callback'); + assert.same(this, context, 'correct callback context'); + }, context); + assert.true([].groupToMap(it => it) instanceof Map, 'returns Map'); + assert.deepEqual(from([1, 2, 3].groupToMap(it => it % 2)), [[1, [1, 3]], [0, [2]]], '#1'); + assert.deepEqual( + from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].groupToMap(it => `i${ it % 5 }`)), + [['i1', [1, 6, 11]], ['i2', [2, 7, 12]], ['i3', [3, 8]], ['i4', [4, 9]], ['i0', [5, 10]]], + '#2', + ); + assert.deepEqual(from(Array(3).groupToMap(it => it)), [[undefined, [undefined, undefined, undefined]]], '#3'); + if (STRICT) { + assert.throws(() => groupToMap.call(null, () => { /* empty */ }), TypeError, 'null this -> TypeError'); + assert.throws(() => groupToMap.call(undefined, () => { /* empty */ }), TypeError, 'undefined this -> TypeError'); + } + array = [1]; + // eslint-disable-next-line object-shorthand -- constructor + array.constructor = { [Symbol.species]: function () { + return { foo: 1 }; + } }; + assert.same(array.groupToMap(Boolean).get(true).foo, undefined, 'no @@species'); +}); diff --git a/tests/tests/esnext.array.group.js b/tests/tests/esnext.array.group.js new file mode 100644 index 000000000000..df6d563a1b3c --- /dev/null +++ b/tests/tests/esnext.array.group.js @@ -0,0 +1,39 @@ +import { STRICT } from '../helpers/constants'; + +const { getPrototypeOf } = Object; + +QUnit.test('Array#group', assert => { + const { group } = Array.prototype; + assert.isFunction(group); + assert.arity(group, 1); + assert.name(group, 'group'); + assert.looksNative(group); + assert.nonEnumerable(Array.prototype, 'group'); + let array = [1]; + const context = {}; + array.group(function (value, key, that) { + assert.same(arguments.length, 3, 'correct number of callback arguments'); + assert.same(value, 1, 'correct value in callback'); + assert.same(key, 0, 'correct index in callback'); + assert.same(that, array, 'correct link to array in callback'); + assert.same(this, context, 'correct callback context'); + }, context); + assert.same(getPrototypeOf([].group(it => it)), null, 'null proto'); + assert.deepEqual([1, 2, 3].group(it => it % 2), { 1: [1, 3], 0: [2] }, '#1'); + assert.deepEqual( + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].group(it => `i${ it % 5 }`), + { i1: [1, 6, 11], i2: [2, 7, 12], i3: [3, 8], i4: [4, 9], i0: [5, 10] }, + '#2', + ); + assert.deepEqual(Array(3).group(it => it), { undefined: [undefined, undefined, undefined] }, '#3'); + if (STRICT) { + assert.throws(() => group.call(null, () => { /* empty */ }), TypeError, 'null this -> TypeError'); + assert.throws(() => group.call(undefined, () => { /* empty */ }), TypeError, 'undefined this -> TypeError'); + } + array = [1]; + // eslint-disable-next-line object-shorthand -- constructor + array.constructor = { [Symbol.species]: function () { + return { foo: 1 }; + } }; + assert.same(array.group(Boolean).true.foo, undefined, 'no @@species'); +});