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 d3d49a2 commit 8d41a87
Show file tree
Hide file tree
Showing 36 changed files with 350 additions and 72 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
60 changes: 30 additions & 30 deletions README.md
Expand Up @@ -33,7 +33,7 @@
import 'core-js/actual'; // <- at the top of your entry point

Array.from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
[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] }
Promise.resolve(42).then(x => console.log(x)); // => 42
structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3])
queueMicrotask(() => console.log('called as microtask'));
Expand All @@ -42,14 +42,14 @@ queueMicrotask(() => console.log('called as microtask'));
*You can load only required features*:
```js
import 'core-js/actual/array/from'; // <- at the top of your entry point
import 'core-js/actual/array/group-by'; // <- at the top of your entry point
import 'core-js/actual/array/group'; // <- at the top of your entry point
import 'core-js/actual/set'; // <- at the top of your entry point
import 'core-js/actual/promise'; // <- at the top of your entry point
import 'core-js/actual/structured-clone'; // <- at the top of your entry point
import 'core-js/actual/queue-microtask'; // <- at the top of your entry point

Array.from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
[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] }
Promise.resolve(42).then(x => console.log(x)); // => 42
structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3])
queueMicrotask(() => console.log('called as microtask'));
Expand All @@ -58,14 +58,14 @@ queueMicrotask(() => console.log('called as microtask'));
*Or use it without global namespace pollution*:
```js
import from from 'core-js-pure/actual/array/from';
import groupBy from 'core-js-pure/actual/array/group-by';
import group from 'core-js-pure/actual/array/group';
import Set from 'core-js-pure/actual/set';
import Promise from 'core-js-pure/actual/promise';
import structuredClone from 'core-js-pure/actual/structured-clone';
import queueMicrotask from 'core-js-pure/actual/queue-microtask';

from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
groupBy([1, 2, 3, 4, 5], it => it % 2); // => { 1: [1, 3, 5], 0: [2, 4] }
group([1, 2, 3, 4, 5], it => it % 2); // => { 1: [1, 3, 5], 0: [2, 4] }
Promise.resolve(42).then(x => console.log(x)); // => 42
structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3])
queueMicrotask(() => console.log('called as microtask'));
Expand Down Expand Up @@ -501,7 +501,7 @@ core-js(-pure)/es|stable|actual|full/object/define-setter
core-js(-pure)/es|stable|actual|full/object/lookup-getter
core-js(-pure)/es|stable|actual|full/object/lookup-setter
```
[*Examples*](t.ly/j1Uw):
[*Examples*](is.gd/udzZq0):
```js
let foo = { q: 1, w: 2 };
let bar = { e: 3, r: 4 };
Expand Down Expand Up @@ -738,7 +738,7 @@ core-js(-pure)/es|stable|actual|full/array/virtual/sort
core-js(-pure)/es|stable|actual|full/array/virtual/splice
core-js(-pure)/es|stable|actual|full/array/virtual/values
```
[*Examples*](t.ly/0qoU):
[*Examples*](is.gd/i8mOSe):
```js
Array.from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
Array.from({ 0: 1, 1: 2, 2: 3, length: 3 }); // => [1, 2, 3]
Expand Down Expand Up @@ -906,7 +906,7 @@ core-js/es|stable|actual|full/regexp/to-string
core-js/es|stable|actual|full/escape
core-js/es|stable|actual|full/unescape
```
[*Examples*](t.ly/ex9y):
[*Examples*](is.gd/Q8eRhG):
```js
for (let value of 'a𠮷b') {
console.log(value); // => 'a', '𠮷', 'b'
Expand Down Expand Up @@ -1700,7 +1700,7 @@ core-js/es|stable|actual|full/typed-array/to-locale-string
core-js/es|stable|actual|full/typed-array/to-string
core-js/es|stable|actual|full/typed-array/values
```
[*Examples*](t.ly/w98j):
[*Examples*](is.gd/Eo7ltU):
```js
new Int32Array(4); // => [0, 0, 0, 0]
new Uint8ClampedArray([1, 2, 3, 666]); // => [1, 2, 3, 255]
Expand Down Expand Up @@ -1805,7 +1805,7 @@ namespace JSON {
core-js(-pure)/es|stable|actual|full/json/stringify
core-js(-pure)/es|stable|actual|full/json/to-string-tag
```
[*Examples*](t.ly/YJeG):
[*Examples*](is.gd/izZqKn):
```js
JSON.stringify({ '𠮷': ['\uDF06\uD834'] }); // => '{"𠮷":["\\udf06\\ud834"]}'
```
Expand Down 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 All @@ -2113,7 +2113,7 @@ core-js(-pure)/actual|full(/virtual)/array/find-last-index
core-js/actual|full/typed-array/find-last
core-js/actual|full/typed-array/find-last-index
```
[*Examples*](t.ly/TwKr):
[*Examples*](is.gd/GVqNFY):
```js
[1, 2, 3, 4].findLast(it => it % 2); // => 3
[1, 2, 3, 4].findLastIndex(it => it % 2); // => 2
Expand Down Expand Up @@ -2147,7 +2147,7 @@ core-js/actual|full/typed-array/to-sorted
core-js/actual|full/typed-array/to-spliced
core-js/actual|full/typed-array/with
```
[*Examples*](t.ly/wcvY):
[*Examples*](is.gd/tVkbY3):
```js
const sequence = [1, 2, 3];
sequence.toReversed(); // => [3, 2, 1]
Expand Down Expand Up @@ -2242,7 +2242,7 @@ core-js(-pure)/full/iterator/take
core-js(-pure)/full/iterator/to-array
core-js(-pure)/full/iterator/to-async
```
[Examples](t.ly/FEp1):
[Examples](is.gd/P7YLCq):
```js
[1, 2, 3, 4, 5, 6, 7].values()
.drop(1)
Expand Down Expand Up @@ -2328,7 +2328,7 @@ core-js/proposals/map-upsert-stage-2
core-js(-pure)/full/map/emplace
core-js(-pure)/full/weak-map/emplace
```
[*Examples*](t.ly/1PXW):
[*Examples*](is.gd/ty5I2v):
```js
const map = new Map([['a', 2]]);

Expand Down Expand Up @@ -2588,7 +2588,7 @@ core-js/proposals/array-filtering-stage-1
core-js(-pure)/full/array(/virtual)/filter-reject
core-js/full/typed-array/filter-reject
```
[*Examples*](t.ly/OOO2):
[*Examples*](is.gd/jJcoWw):
```js
[1, 2, 3, 4, 5].filterReject(it => it % 2); // => [2, 4]
````
Expand All @@ -2609,7 +2609,7 @@ core-js/proposals/array-unique
core-js(-pure)/full/array(/virtual)/unique-by
core-js/full/typed-array/unique-by
```
[*Examples*](t.ly/cEFZ):
[*Examples*](is.gd/lilNPu):
```js
[1, 2, 3, 2, 1].uniqueBy(); // [1, 2, 3]
Expand Down Expand Up @@ -2660,7 +2660,7 @@ core-js/proposals/number-range
core-js(-pure)/full/bigint/range
core-js(-pure)/full/number/range
```
[*Example*](t.ly/KvQ9):
[*Example*](is.gd/caCKSb):
```js
for (const i of Number.range(1, 10)) {
console.log(i); // => 1, 2, 3, 4, 5, 6, 7, 8, 9
Expand Down Expand Up @@ -2738,7 +2738,7 @@ class String {
core-js/proposals/string-cooked
core-js(-pure)/full/string/cooked
```
[*Example*](https://t.ly/OikE):
[*Example*](is.gd/7QPnss):
```js
function safePath(strings, ...subs) {
return String.cooked(strings, ...subs.map(sub => encodeURIComponent(sub)));
Expand Down Expand Up @@ -2818,7 +2818,7 @@ core-js(-pure)/full/object/iterate-keys
core-js(-pure)/full/object/iterate-values
core-js(-pure)/full/object/iterate-entries
```
[*Example*](t.ly/3pb0):
[*Example*](is.gd/Wnm2tD):
```js
const obj = { foo: 'bar', baz: 'blah' };
Expand Down Expand Up @@ -2894,7 +2894,7 @@ core-js/proposals/function-is-callable-is-constructor
core-js(-pure)/full/function/is-callable
core-js(-pure)/full/function/is-constructor
```
[*Examples*](t.ly/ai3f):
[*Examples*](is.gd/Kof1he):
```js
Function.isCallable(null); // => false
Function.isCallable({}); // => false
Expand Down Expand Up @@ -3154,7 +3154,7 @@ core-js(-pure)/stable|actual|full/url
core-js/stable|actual|full/url/to-json
core-js(-pure)/stable|actual|full/url-search-params
```
[*Examples*](t.ly/ZiA6):
[*Examples*](is.gd/AfIwve):
```js
const url = new URL('https://login:password@example.com:8080/foo/bar?a=1&b=2&a=3#fragment');

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.

0 comments on commit 8d41a87

Please sign in to comment.