Skip to content

Commit

Permalink
add Iterator sequencing stage 2.7 proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 30, 2024
1 parent dfc45d9 commit 751bbae
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
- `Math.sumPrecise`
- Moved to stage 3, [October 2024 TC39 meeting](https://github.com/tc39/proposal-math-sum/issues/19)
- Added `/actual/` namespace entries, unconditional forced replacement changed to feature detection
- Added [`Iterator` sequencing stage 2.7 proposal](https://github.com/tc39/proposal-iterator-sequencing):
- Added built-ins:
- `Iterator.concat`
- [`Map` upsert stage 2 proposal](https://github.com/tc39/proposal-upsert):
- [Updated to the new API following the October 2024 TC39 meeting](https://github.com/tc39/proposal-upsert/pull/58)
- Added built-ins:
Expand Down
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3])
- [`Math.sumPrecise`](#mathsumprecise)
- [`Symbol.metadata` for decorators metadata proposal](#symbolmetadata-for-decorators-metadata-proposal)
- [Stage 2.7 proposals](#stage-27-proposals)
- [`Iterator` sequencing](#iterator-sequencing)
- [Stage 2 proposals](#stage-2-proposals)
- [`AsyncIterator` helpers](#asynciterator-helpers)
- [`Iterator.range`](#iteratorrange)
Expand Down Expand Up @@ -2018,6 +2019,8 @@ globalThis.Array === Array; // => true
### ECMAScript proposals[](#index)
[The TC39 process.](https://tc39.github.io/process-document/)

`core-js/stage/3` entry point contains only stage 3 proposals, `core-js/stage/2.7` - stage 2.7 and stage 3, etc.

#### Finished proposals[](#index)

Finished (stage 4) proposals already marked in `core-js` as stable ECMAScript, they are available in `core-js/stable` and `core-js/es` namespace, you can find them in related sections of the README. However, even for finished proposals, `core-js` provides a way to include only features for a specific proposal like `core-js/proposals/proposal-name`.
Expand Down Expand Up @@ -2397,8 +2400,6 @@ core-js/proposals/set-methods-v2

#### Stage 3 proposals[](#index)

`core-js/stage/3` entry point contains only stage 3 proposals, `core-js/stage/2.7` - stage 2.7 and stage 3, etc.

[*CommonJS entry points:*](#commonjs-api)
```
core-js(-pure)/stage/3
Expand Down Expand Up @@ -2647,7 +2648,25 @@ core-js(-pure)/actual|full/function/metadata
core-js(-pure)/stage/2.7
```

Nothing.
##### [`Iterator` sequencing](https://github.com/tc39/proposal-iterator-sequencing)[](#index)
Module [`esnext.iterator.range`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.range.js)
```ts
class Iterator {
concat(...items: Array<IterableObject>): Iterator<any>;
}
```
[*CommonJS entry points:*](#commonjs-api)
```
core-js/proposals/iterator-sequencing
core-js(-pure)/full/iterator/concat
```
[*Example*](https://tinyurl.com/2522xjae):
```js
Iterator.concat(Iterator.from([0, 1]), [2, 3], function * () {
yield 4;
yield 5;
}()).toArray(); // => [0, 1, 2, 3, 4, 5]
```

#### Stage 2 proposals[](#index)
[*CommonJS entry points:*](#commonjs-api)
Expand Down
2 changes: 2 additions & 0 deletions packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2249,6 +2249,8 @@ export const data = {
// TODO: Remove from `core-js@4`
'esnext.iterator.as-indexed-pairs': {
},
'esnext.iterator.concat': {
},
'esnext.iterator.dispose': {
},
// TODO: Remove from `core-js@4`
Expand Down
1 change: 1 addition & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ export default {
'es.iterator.take',
'es.iterator.to-array',
'es.promise.try',
'esnext.iterator.concat',
'esnext.map.get-or-insert',
'esnext.map.get-or-insert-computed',
'esnext.weak-map.get-or-insert',
Expand Down
20 changes: 20 additions & 0 deletions packages/core-js/full/iterator/concat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
require('../../modules/es.array.iterator');
require('../../modules/es.object.to-string');
require('../../modules/es.string.iterator');
require('../../modules/es.iterator.constructor');
require('../../modules/es.iterator.drop');
require('../../modules/es.iterator.every');
require('../../modules/es.iterator.filter');
require('../../modules/es.iterator.find');
require('../../modules/es.iterator.flat-map');
require('../../modules/es.iterator.for-each');
require('../../modules/es.iterator.map');
require('../../modules/es.iterator.reduce');
require('../../modules/es.iterator.some');
require('../../modules/es.iterator.take');
require('../../modules/es.iterator.to-array');
require('../../modules/esnext.iterator.concat');
var path = require('../../internals/path');

module.exports = path.Iterator.concat;
1 change: 1 addition & 0 deletions packages/core-js/full/iterator/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var parent = require('../../actual/iterator');
require('../../modules/esnext.iterator.concat');
require('../../modules/esnext.iterator.range');
// TODO: Remove from `core-js@4`
require('../../modules/esnext.iterator.as-indexed-pairs');
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/internals/iterator-create-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var createIteratorProxyPrototype = function (IS_ITERATOR) {
} catch (error) {
return iteratorClose(iterator, 'throw', error);
}
iteratorClose(iterator, 'normal');
if (iterator) iteratorClose(iterator, 'normal');
return createIterResultObject(undefined, true);
}
});
Expand Down
56 changes: 56 additions & 0 deletions packages/core-js/modules/esnext.iterator.concat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';
var $ = require('../internals/export');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var getIteratorMethod = require('../internals/get-iterator-method');
var createIteratorProxy = require('../internals/iterator-create-proxy');

var $Array = Array;

var IteratorProxy = createIteratorProxy(function () {
while (true) {
var iterator = this.iterator;
if (!iterator) {
var iterableIndex = this.nextIterableIndex++;
var iterables = this.iterables;
if (iterableIndex >= iterables.length) {
this.done = true;
return;
}
var entry = iterables[iterableIndex];
this.iterables[iterableIndex] = null;
iterator = this.iterator = call(entry.method, entry.iterable);
this.next = iterator.next;
}
var result = anObject(call(this.next, iterator));
if (result.done) {
this.iterator = null;
this.next = null;
continue;
}
return result.value;
}
});

// `Iterator.concat` method
// https://github.com/tc39/proposal-iterator-sequencing
$({ target: 'Iterator', stat: true, forced: true }, {
concat: function concat() {
var length = arguments.length;
var iterables = $Array(length);
for (var index = 0; index < length; index++) {
var item = anObject(arguments[index]);
iterables[index] = {
iterable: item,
method: aCallable(getIteratorMethod(item))
};
}
return new IteratorProxy({
iterables: iterables,
nextIterableIndex: 0,
iterator: null,
next: null
});
}
});
3 changes: 3 additions & 0 deletions packages/core-js/proposals/iterator-sequencing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';
// https://github.com/tc39/proposal-iterator-sequencing
require('../modules/esnext.iterator.concat');
2 changes: 2 additions & 0 deletions packages/core-js/stage/2.7.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
'use strict';
var parent = require('./3');

require('../proposals/iterator-sequencing');

module.exports = parent;
3 changes: 3 additions & 0 deletions tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,9 @@ GLOBAL.tests = {
'esnext.function.metadata': function () {
return Function.prototype[Symbol.metadata] === null;
},
'esnext.iterator.concat': function () {
return Iterator.concat;
},
'esnext.iterator.dispose': function () {
return [].keys()[Symbol.dispose];
},
Expand Down
2 changes: 2 additions & 0 deletions tests/entries/unit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
ok(load(NS, 'function/virtual/un-this').call([].slice)([1, 2, 3], 1)[0] === 2);
ok(typeof load(NS, 'iterator/as-indexed-pairs') == 'function');
ok(typeof load(NS, 'iterator/indexed') == 'function');
ok(load(NS, 'iterator/concat')([2]).next().value === 2);
ok(load(NS, 'iterator/range')(1, 2).next().value === 1);
ok(load(NS, 'map/delete-all')(new Map(), 1, 2) === false);
ok(load(NS, 'map/emplace')(new Map([[1, 2]]), 1, { update: it => it ** 2 }) === 4);
Expand Down Expand Up @@ -938,6 +939,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
load('proposals/iterator-helpers-stage-3');
load('proposals/iterator-helpers-stage-3-2');
load('proposals/iterator-range');
load('proposals/iterator-sequencing');
load('proposals/json-parse-with-source');
load('proposals/keys-composition');
load('proposals/map-update-or-insert');
Expand Down

0 comments on commit 751bbae

Please sign in to comment.