Skip to content

Commit 65c36d8

Browse files
authored
Merge pull request #1001 from zloirock/tc39-october-2021
2 parents 4d91951 + bcda388 commit 65c36d8

24 files changed

+158
-26
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Changelog
22
##### Unreleased
3+
- [`Array` grouping proposal](https://github.com/tc39/proposal-array-grouping):
4+
- Moved to the stage 2
5+
- Added `Array.prototype.groupByMap` method
6+
- Removed `@@species` support
37
- Added [change `Array` by copy stage 2 proposal](https://github.com/tc39/proposal-change-array-by-copy):
48
- `Array.prototype.toReversed`
59
- `Array.prototype.toSorted`

README.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Promise.resolve(32).then(x => console.log(x)); // => 32
101101
- [`Iterator` helpers](#iterator-helpers)
102102
- [New `Set` methods](#new-set-methods)
103103
- [`Map.prototype.emplace`](#mapprototypeemplace)
104+
- [`Array` grouping](#array-grouping)
104105
- [Change `Array` by copy](#change-array-by-copy)
105106
- [`Array.isTemplateObject`](#arrayistemplateobject)
106107
- [`Symbol.{ asyncDispose, dispose }` for `using` statement](#symbol-asyncdispose-dispose--for-using-statement)
@@ -112,7 +113,6 @@ Promise.resolve(32).then(x => console.log(x)); // => 32
112113
- [`compositeKey` and `compositeSymbol`](#compositekey-and-compositesymbol)
113114
- [`Array.fromAsync`](#arrayfromasync)
114115
- [`Array` filtering](#array-filtering)
115-
- [`Array` grouping](#array-grouping)
116116
- [`Array` deduplication](#array-deduplication)
117117
- [Getting last item from `Array`](#getting-last-item-from-array)
118118
- [`Number.range`](#numberrange)
@@ -2353,21 +2353,16 @@ console.log(compositeSymbol(1, a, 2, b) === compositeSymbol(1, a, 2, b)); // =>
23532353
console.log(compositeSymbol(a, a) === compositeSymbol(a, a)); // => true
23542354
```
23552355
##### [`Array.fromAsync`](https://github.com/tc39/proposal-array-from-async)[⬆](#index)
2356-
Modules [`esnext.array.from-async`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.from-async.js) and [`esnext.typed-array.from-async`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.typed-array.from-async.js)
2356+
Modules [`esnext.array.from-async`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.from-async.js).
23572357
```js
23582358
class Array {
23592359
static fromAsync(asyncItems: AsyncIterable | Iterable | ArrayLike, mapfn?: (value: any, index: number) => any, thisArg?: any): Array;
23602360
}
2361-
2362-
class %TypedArray% {
2363-
static fromAsync(asyncItems: AsyncIterable | Iterable | ArrayLike, mapfn?: (value: number, index: number, target) => number, thisArg?: any): %TypedArray%;
2364-
}
23652361
```
23662362
[*CommonJS entry points:*](#commonjs-api)
23672363
```js
23682364
core-js/proposals/array-from-async
23692365
core-js(-pure)/features/array/from-async
2370-
core-js/features/typed-array/from-async
23712366
```
23722367
[*Example*](https://goo.gl/Jt7SsD):
23732368
```js
@@ -2395,26 +2390,25 @@ core-js/features/typed-array/filter-reject
23952390
[1, 2, 3, 4, 5].filterReject(it => it % 2); // => [2, 4]
23962391
````
23972392
##### [`Array` grouping](#https://github.com/tc39/proposal-array-grouping)[⬆](#index)
2398-
Modules [`esnext.array.group-by`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.group-by.js) and [`esnext.typed-array.group-by`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.typed-array.group-by.js).
2393+
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-map`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.group-by-map.js).
23992394
```js
24002395
class Array {
24012396
groupBy(callbackfn: (value: any, index: number, target: any) => key, thisArg?: any): { [key]: Array<mixed> };
24022397
groupByMap(callbackfn: (value: any, index: number, target: any) => key, thisArg?: any): Map<key, Array<mixed>>;
24032398
}
2404-
2405-
class %TypedArray% {
2406-
groupBy(callbackfn: (value: number, index: number, target: %TypedArray%) => key, thisArg?: any): { [key]: %TypedArray% };
2407-
}
24082399
```
24092400
[*CommonJS entry points:*](#commonjs-api)
24102401
```
24112402
core-js/proposals/array-grouping
24122403
core-js(-pure)/features/array(/virtual)/group-by
2413-
core-js/features/typed-array/group-by
2404+
core-js(-pure)/features/array(/virtual)/group-by-map
24142405
```
2415-
[*Examples*](t.ly/VggI):
2406+
[*Examples*](t.ly/xEqc):
24162407
```js
24172408
[1, 2, 3, 4, 5].groupBy(it => it % 2); // => { 1: [1, 3, 5], 0: [2, 4] }
2409+
const map = [1, 2, 3, 4, 5].groupByMap(it => it % 2);
2410+
map.get(1); // => [1, 3, 5]
2411+
map.get(0); // => [2, 4]
24182412
````
24192413
##### [Array deduplication](https://github.com/tc39/proposal-array-unique)[⬆](#index)
24202414
Modules [`esnext.array.unique-by`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.unique-by.js) and [`esnext.typed-array.unique-by`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.typed-array.unique-by.js)
@@ -3086,4 +3080,4 @@ console.log(getIteratorMethod({})); // undefined
30863080
- ES `Proxy` can't be polyfilled, you can try to use [`proxy-polyfill`](https://github.com/GoogleChrome/proxy-polyfill) which provides a very little subset of features.
30873081
- ES `String#normalize` is not a very useful feature, but this polyfill will be very large. If you need it, you can use [unorm](https://github.com/walling/unorm/).
30883082
- ECMA-402 `Intl` is missed because of the size. You can use [those polyfills](https://formatjs.io/docs/polyfills).
3089-
- `window.fetch` is not a cross-platform feature, in some environments, it makes no sense. For this reason, I don't think it should be in `core-js`. Looking at a large number of requests it *might be* added in the future. Now you can use, for example, [this polyfill](https://github.com/github/fetch).
3083+
- `window.fetch` is not a cross-platform feature, in some environments, it makes no sense. For this reason, I don't think it should be in `core-js`. Looking at a large number of requests it *might be* added in the future. Now you can use, for example, [this polyfill](https://github.com/github/fetch).

packages/core-js-compat/src/data.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,8 @@ export const data = {
14511451
},
14521452
'esnext.array.group-by': {
14531453
},
1454+
'esnext.array.group-by-map': {
1455+
},
14541456
'esnext.array.is-template-object': {
14551457
},
14561458
// TODO: Remove from `core-js@4`
@@ -1728,6 +1730,7 @@ export const data = {
17281730
// TODO: Remove from `core-js@4`
17291731
'esnext.symbol.replace-all': {
17301732
},
1733+
// TODO: Remove from `core-js@4`
17311734
'esnext.typed-array.from-async': {
17321735
},
17331736
// TODO: Remove from `core-js@4`
@@ -1745,6 +1748,7 @@ export const data = {
17451748
chrome: '97',
17461749
safari: '15.4',
17471750
},
1751+
// TODO: Remove from `core-js@4`
17481752
'esnext.typed-array.group-by': {
17491753
},
17501754
'esnext.typed-array.to-reversed': {

packages/core-js-compat/src/modules-by-versions.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export default {
112112
'esnext.typed-array.from-async',
113113
],
114114
'3.20': [
115+
'esnext.array.group-by-map',
115116
'esnext.array.to-reversed',
116117
'esnext.array.to-sorted',
117118
'esnext.array.to-spliced',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require('../../modules/es.map');
2+
require('../../modules/esnext.array.group-by-map');
3+
var entryUnbind = require('../../internals/entry-unbind');
4+
5+
module.exports = entryUnbind('Array', 'groupByMap');

packages/core-js/features/array/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require('../../modules/esnext.array.filter-reject');
1010
require('../../modules/esnext.array.find-last');
1111
require('../../modules/esnext.array.find-last-index');
1212
require('../../modules/esnext.array.group-by');
13+
require('../../modules/esnext.array.group-by-map');
1314
require('../../modules/esnext.array.is-template-object');
1415
require('../../modules/esnext.array.last-item');
1516
require('../../modules/esnext.array.last-index');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require('../../../modules/es.map');
2+
require('../../../modules/esnext.array.group-by-map');
3+
var entryVirtual = require('../../../internals/entry-virtual');
4+
5+
module.exports = entryVirtual('Array').groupByMap;

packages/core-js/features/array/virtual/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require('../../../modules/esnext.array.filter-reject');
88
require('../../../modules/esnext.array.find-last');
99
require('../../../modules/esnext.array.find-last-index');
1010
require('../../../modules/esnext.array.group-by');
11+
require('../../../modules/esnext.array.group-by-map');
1112
require('../../../modules/esnext.array.to-reversed');
1213
require('../../../modules/esnext.array.to-sorted');
1314
require('../../../modules/esnext.array.to-spliced');

packages/core-js/internals/array-group-by.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module.exports = function ($this, callbackfn, that, specificConstructor) {
2727
if (key in target) push(target[key], value);
2828
else target[key] = [value];
2929
}
30+
// TODO: Remove this block from `core-js@4`
3031
if (specificConstructor) {
3132
Constructor = specificConstructor(O);
3233
if (Constructor !== Array) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict';
2+
var $ = require('../internals/export');
3+
var getBuiltIn = require('../internals/get-built-in');
4+
var bind = require('../internals/function-bind-context');
5+
var uncurryThis = require('../internals/function-uncurry-this');
6+
var IndexedObject = require('../internals/indexed-object');
7+
var toObject = require('../internals/to-object');
8+
var lengthOfArrayLike = require('../internals/length-of-array-like');
9+
var addToUnscopables = require('../internals/add-to-unscopables');
10+
11+
var Map = getBuiltIn('Map');
12+
var MapPrototype = Map.prototype;
13+
var mapGet = uncurryThis(MapPrototype.get);
14+
var mapHas = uncurryThis(MapPrototype.has);
15+
var mapSet = uncurryThis(MapPrototype.set);
16+
var push = uncurryThis([].push);
17+
18+
// `Array.prototype.groupByMap` method
19+
// https://github.com/tc39/proposal-array-grouping
20+
$({ target: 'Array', proto: true }, {
21+
groupByMap: function groupByMap(callbackfn /* , thisArg */) {
22+
var O = toObject(this);
23+
var self = IndexedObject(O);
24+
var boundFunction = bind(callbackfn, arguments.length > 1 ? arguments[1] : undefined);
25+
var map = new Map();
26+
var length = lengthOfArrayLike(self);
27+
var index = 0;
28+
var key, value;
29+
for (;length > index; index++) {
30+
value = self[index];
31+
key = boundFunction(value, index, O);
32+
if (mapHas(map, key)) push(mapGet(map, key), value);
33+
else mapSet(map, key, [value]);
34+
} return map;
35+
}
36+
});
37+
38+
addToUnscopables('groupByMap');

0 commit comments

Comments
 (0)