Skip to content

Commit

Permalink
rename methods from the Array grouping proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed May 23, 2022
1 parent 3b9b0bf commit 7b95fe8
Show file tree
Hide file tree
Showing 36 changed files with 332 additions and 52 deletions.
4 changes: 3 additions & 1 deletion 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
Expand Down
18 changes: 9 additions & 9 deletions README.md
Expand Up @@ -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<mixed> };
groupByToMap(callbackfn: (value: any, index: number, target: any) => key, thisArg?: any): Map<key, Array<mixed>>;
group(callbackfn: (value: any, index: number, target: any) => key, thisArg?: any): { [key]: Array<mixed> };
groupToMap(callbackfn: (value: any, index: number, target: any) => key, thisArg?: any): Map<key, Array<mixed>>;
}
```
[*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]
````
Expand Down
6 changes: 6 additions & 0 deletions packages/core-js-compat/src/data.mjs
Expand Up @@ -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`
Expand Down
4 changes: 4 additions & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Expand Up @@ -139,4 +139,8 @@ export default {
'web.atob',
'web.btoa',
],
3.23: [
'esnext.array.group',
'esnext.array.group-to-map',
],
};
6 changes: 6 additions & 0 deletions 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');
4 changes: 4 additions & 0 deletions 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');
2 changes: 2 additions & 0 deletions packages/core-js/actual/array/index.js
Expand Up @@ -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');
Expand Down
6 changes: 6 additions & 0 deletions 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;
4 changes: 4 additions & 0 deletions 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;
2 changes: 2 additions & 0 deletions packages/core-js/actual/array/virtual/index.js
Expand Up @@ -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');
Expand Down
9 changes: 9 additions & 0 deletions 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;
};
9 changes: 9 additions & 0 deletions 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;
};
3 changes: 3 additions & 0 deletions packages/core-js/full/array/group-to-map.js
@@ -0,0 +1,3 @@
var parent = require('../../actual/array/group-to-map');

module.exports = parent;
3 changes: 3 additions & 0 deletions packages/core-js/full/array/group.js
@@ -0,0 +1,3 @@
var parent = require('../../actual/array/group');

module.exports = parent;
3 changes: 3 additions & 0 deletions 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;
3 changes: 3 additions & 0 deletions packages/core-js/full/array/virtual/group.js
@@ -0,0 +1,3 @@
var parent = require('../../../actual/array/virtual/group');

module.exports = parent;
3 changes: 3 additions & 0 deletions packages/core-js/full/instance/group-to-map.js
@@ -0,0 +1,3 @@
var parent = require('../../actual/instance/group-to-map');

module.exports = parent;
3 changes: 3 additions & 0 deletions packages/core-js/full/instance/group.js
@@ -0,0 +1,3 @@
var parent = require('../../actual/instance/group');

module.exports = parent;
32 changes: 32 additions & 0 deletions 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;
};
File renamed without changes.
34 changes: 4 additions & 30 deletions 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');
5 changes: 3 additions & 2 deletions 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');

Expand All @@ -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);
}
});

Expand Down
11 changes: 11 additions & 0 deletions 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');
15 changes: 15 additions & 0 deletions 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');
4 changes: 2 additions & 2 deletions 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;
Expand All @@ -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);
3 changes: 3 additions & 0 deletions 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');
1 change: 1 addition & 0 deletions 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');
4 changes: 3 additions & 1 deletion 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;
2 changes: 2 additions & 0 deletions scripts/check-compat-tests.mjs
Expand Up @@ -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',
Expand Down
17 changes: 17 additions & 0 deletions tests/commonjs.mjs
Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 7b95fe8

Please sign in to comment.