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 Jun 11, 2022
1 parent e1a0d39 commit 19a7298
Show file tree
Hide file tree
Showing 36 changed files with 331 additions and 52 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
## Changelog
##### Unreleased
- [`Array` find from last](https://github.com/tc39/proposal-array-find-from-last) moved to the stable ES, per June 2022 TC39 meeting
- [`Array` find from last](https://github.com/tc39/proposal-array-find-from-last) moved to the stable ES, per June 2022 TC39 meeting:
- `Array.prototype.findLast`
- `Array.prototype.findLastIndex`
- `%TypedArray%.prototype.findLast`
- `%TypedArray%.prototype.findLastIndex`
- Methods from [the `Array` grouping proposal](https://github.com/tc39/proposal-array-grouping) [renamed](https://github.com/tc39/proposal-array-grouping/pull/39), per June 2022 TC39 meeting:
- `Array.prototype.groupBy` -> `Array.prototype.group`
- `Array.prototype.groupByToMap` -> `Array.prototype.groupToMap`
- Changed the order of operations in `%TypedArray%.prototype.with` following [proposal-change-array-by-copy/86](https://github.com/tc39/proposal-change-array-by-copy/issues/86), per June 2022 TC39 meeting
- [Decorator Metadata proposal](https://github.com/tc39/proposal-decorator-metadata) extracted from [Decorators proposal](https://github.com/tc39/proposal-decorators) as a separate stage 2 proposal, `Symbol.metadataKey` replaces `Symbol.metadata`
- Added `Array.prototype.push` polyfill with some fixes for modern engines
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2103,24 +2103,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*](https://t.ly/xEqc):
[*Examples*](https://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
Original file line number Diff line number Diff line change
Expand Up @@ -1606,10 +1606,16 @@ export const data = {
'esnext.array.find-last': null,
// TODO: Remove from `core-js@4`
'esnext.array.find-last-index': null,
'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
2 changes: 2 additions & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ export default {
'es.array.unshift',
'es.typed-array.find-last',
'es.typed-array.find-last-index',
'esnext.array.group',
'esnext.array.group-to-map',
'esnext.symbol.metadata-key',
],
};
6 changes: 6 additions & 0 deletions packages/core-js/actual/array/group-to-map.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -595,19 +595,35 @@ for (PATH of ['core-js-pure', 'core-js']) {
}

for (const NS of ['actual', 'full', 'features']) {
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));
ok(load(NS, 'array/to-reversed')([1, 2, 3])[0] === 3);
ok(load(NS, 'array/to-sorted')([3, 2, 1])[0] === 1);
ok(load(NS, 'array/to-spliced')([3, 2, 1], 1, 1, 4, 5).length === 4);
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));
ok(load(NS, 'array/virtual/to-reversed').call([1, 2, 3])[0] === 3);
ok(load(NS, 'array/virtual/to-sorted').call([3, 2, 1])[0] === 1);
ok(load(NS, 'array/virtual/to-spliced').call([3, 2, 1], 1, 1, 4, 5).length === 4);

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 @@ -825,6 +841,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
Loading

0 comments on commit 19a7298

Please sign in to comment.