Skip to content

Commit e6ee4f6

Browse files
committed
replace Iterator.prototype.sliding with an extra parameter of Iterator.prototype.windows
tc39/proposal-iterator-chunking#24
1 parent a0684d2 commit e6ee4f6

File tree

10 files changed

+44
-20
lines changed

10 files changed

+44
-20
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
## Changelog
22
##### Unreleased
3-
- [`Map` upsert proposal](https://github.com/tc39/proposal-upsert):
3+
- [`Map` upsert stage 3 proposal](https://github.com/tc39/proposal-upsert):
44
- Fixed [a FF `WeakMap.prototype.getOrInsertComputed` bug with callback calling before validation a key](https://bugzilla.mozilla.org/show_bug.cgi?id=1988369)
5+
- [`Iterator` chunking stage 2 proposal](https://github.com/tc39/proposal-iterator-chunking):
6+
- `Iterator.prototype.sliding` method replaced with an extra parameter of `Iterator.prototype.windows` method, [tc39/proposal-iterator-chunking/#24](https://github.com/tc39/proposal-iterator-chunking/pull/24)
57
- Compat data improvements:
68
- [`Map.prototype.{ getOrInsert, getOrInsertComputed }` and `WeakMap.prototype.getOrInsert`](https://github.com/tc39/proposal-upsert) marked as shipped from FF144
79
- Added [Deno 2.5](https://github.com/denoland/deno/releases/tag/v2.5.0) compat data mapping

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3068,31 +3068,31 @@ core-js(-pure)/full/symbol/custom-matcher
30683068
```
30693069

30703070
##### [`Iterator` chunking](https://github.com/tc39/proposal-iterator-chunking)[⬆](#index)
3071-
Modules [`esnext.iterator.chunks`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.chunks.js), [`esnext.iterator.sliding`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.sliding.js)
3071+
Modules [`esnext.iterator.chunks`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.chunks.js)
30723072
and [`esnext.iterator.windows`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.windows.js)
30733073
```ts
30743074
class Iterator {
30753075
chunks(chunkSize: number): Iterator<any>;
3076-
sliding(windowSize: number): Iterator<any>;
3077-
windows(windowSize: number): Iterator<any>;
3076+
windows(windowSize: number, undersized?: 'only full' | 'allow partial' | undefined): Iterator<any>;
30783077
}
30793078
```
30803079
[*CommonJS entry points:*](#commonjs-api)
30813080
```
3082-
core-js/proposals/iterator-chunking
3081+
core-js/proposals/iterator-chunking-v2
30833082
core-js(-pure)/full/iterator/chunks
3084-
core-js(-pure)/full/iterator/sliding
30853083
core-js(-pure)/full/iterator/windows
30863084
```
30873085
[*Examples*](https://tinyurl.com/24xnkcnn)
30883086
```js
30893087
const digits = () => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].values();
30903088
3091-
let chunksOf2 = Array.from(digits().chunks(2)); // [ [0, 1], [2, 3], [4, 5], [6, 7], [8, 9] ]
3089+
let chunks = Array.from(digits().chunks(2)); // [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
30923090
3093-
let slidingOf2 = Array.from(digits().sliding(2)); // [ [0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9] ]
3091+
let windows = Array.from(digits().windows(2)); // [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9]]
30943092
3095-
let windowsOf2 = Array.from(digits().windows(2)); // [ [0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9] ]
3093+
let windowsPartial = Array.from([0, 1].values().windows(3, 'allow partial')); // [[0, 1]]
3094+
3095+
let windowsFull = Array.from([0, 1].values().windows(3)); // []
30963096
```
30973097

30983098
#### Stage 1 proposals[⬆](#index)

packages/core-js/internals/iterator-window.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ var IteratorProxy = createIteratorProxy(function () {
1616
var next = this.next;
1717
var buffer = this.buffer;
1818
var windowSize = this.windowSize;
19-
var sliding = this.sliding;
19+
var allowPartial = this.allowPartial;
2020
var result, done;
2121
while (true) {
2222
result = anObject(call(next, iterator));
2323
done = this.done = !!result.done;
24-
if (sliding && done && buffer.length && buffer.length < windowSize) return createIterResultObject(buffer, false);
24+
if (allowPartial && done && buffer.length && buffer.length < windowSize) return createIterResultObject(buffer, false);
2525
if (done) return createIterResultObject(undefined, true);
2626

2727
if (buffer.length === windowSize) this.buffer = buffer = slice(buffer, 1);
@@ -30,16 +30,16 @@ var IteratorProxy = createIteratorProxy(function () {
3030
}
3131
}, false, true);
3232

33-
// `Iterator.prototype.sliding` and `Iterator.prototype.windows` methods
33+
// `Iterator.prototype.windows` and obsolete `Iterator.prototype.sliding` methods
3434
// https://github.com/tc39/proposal-iterator-chunking
35-
module.exports = function (O, windowSize, sliding) {
35+
module.exports = function (O, windowSize, allowPartial) {
3636
anObject(O);
3737
if (typeof windowSize != 'number' || !windowSize || windowSize >>> 0 !== windowSize) {
38-
return iteratorClose(O, 'throw', new $RangeError('windowSize must be integer in [1, 2^32-1]'));
38+
return iteratorClose(O, 'throw', new $RangeError('`windowSize` must be integer in [1, 2^32-1]'));
3939
}
4040
return new IteratorProxy(getIteratorDirect(O), {
4141
windowSize: windowSize,
4242
buffer: [],
43-
sliding: sliding
43+
allowPartial: allowPartial
4444
});
4545
};

packages/core-js/modules/esnext.iterator.windows.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
var $ = require('../internals/export');
33
var iteratorWindow = require('../internals/iterator-window');
44

5+
var $TypeError = TypeError;
6+
var ALLOW_PARTIAL = 'allow partial';
7+
58
// `Iterator.prototype.windows` method
69
// https://github.com/tc39/proposal-iterator-chunking
710
$({ target: 'Iterator', proto: true, real: true, forced: true }, {
8-
windows: function windows(windowSize) {
9-
return iteratorWindow(this, windowSize, false);
11+
windows: function windows(windowSize /* , undersized */) {
12+
var undersized = arguments.length < 2 ? undefined : arguments[1];
13+
if (undersized !== undefined && undersized !== 'only full' && undersized !== ALLOW_PARTIAL) {
14+
throw new $TypeError('Incorrect `undersized` argument');
15+
}
16+
return iteratorWindow(this, windowSize, undersized === ALLOW_PARTIAL);
1017
}
1118
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
// https://github.com/tc39/proposal-iterator-chunking
3+
require('../modules/esnext.iterator.chunks');
4+
require('../modules/esnext.iterator.windows');

tests/compat-data/tests-coverage.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const ignore = new Set([
2929
'esnext.function.un-this',
3030
'esnext.iterator.as-indexed-pairs',
3131
'esnext.iterator.indexed',
32+
'esnext.iterator.sliding',
3233
'esnext.map.emplace',
3334
'esnext.map.update-or-insert',
3435
'esnext.map.upsert',

tests/compat/tests.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,9 +1903,6 @@ GLOBAL.tests = {
19031903
'esnext.iterator.range': function () {
19041904
return Iterator.range;
19051905
},
1906-
'esnext.iterator.sliding': function () {
1907-
return Iterator.prototype.sliding;
1908-
},
19091906
'esnext.iterator.to-async': function () {
19101907
return Iterator.prototype.toAsync;
19111908
},

tests/entries/unit.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
955955
load('proposals/iterator-range');
956956
load('proposals/iterator-sequencing');
957957
load('proposals/iterator-chunking');
958+
load('proposals/iterator-chunking-v2');
958959
load('proposals/joint-iteration');
959960
load('proposals/json-parse-with-source');
960961
load('proposals/keys-composition');

tests/unit-global/esnext.iterator.windows.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ QUnit.test('Iterator#windows', assert => {
1717
assert.arrayEqual(from(windows.call(createIterator([1, 2]), 3)), [], 'basic functionality #3');
1818
assert.arrayEqual(from(windows.call(createIterator([]), 2)), [], 'basic functionality on empty iterable');
1919

20+
assert.arrayEqual(from(windows.call(createIterator([1, 2]), 3, 'only full')), [], 'undersized #1');
21+
assert.arrayEqual(from(windows.call(createIterator([1, 2]), 3, 'allow partial')), [[1, 2]], 'undersized #2');
22+
2023
const it = createIterator([1, 2, 3]);
2124
const result = windows.call(it, 3);
2225
assert.isIterable(result, 'returns iterable');
@@ -45,4 +48,7 @@ QUnit.test('Iterator#windows', assert => {
4548
const itObservable = createIterator([1, 2, 3], observableReturn);
4649
assert.throws(() => windows.call(itObservable, 0x100000000), RangeError, 'throws on argument more then 2^32 - 1');
4750
assert.true(itObservable.called, 'iterator closed on argument validation error');
51+
52+
assert.throws(() => windows.call(createIterator([1]), 2, null), TypeError, 'incorrect `undersized` argument #1');
53+
assert.throws(() => windows.call(createIterator([1]), 2, 'allowpartial'), TypeError, 'incorrect `undersized` argument #2');
4854
});

tests/unit-pure/esnext.iterator.windows.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ QUnit.test('Iterator#windows', assert => {
1616
assert.arrayEqual(from(windows.call(createIterator([1, 2]), 3)), [], 'basic functionality #3');
1717
assert.arrayEqual(from(windows.call(createIterator([]), 2)), [], 'basic functionality on empty iterable');
1818

19+
assert.arrayEqual(from(windows.call(createIterator([1, 2]), 3, 'only full')), [], 'undersized #1');
20+
assert.arrayEqual(from(windows.call(createIterator([1, 2]), 3, 'allow partial')), [[1, 2]], 'undersized #2');
21+
1922
const it = createIterator([1, 2, 3]);
2023
const result = windows.call(it, 3);
2124
assert.isIterable(result, 'returns iterable');
@@ -44,4 +47,7 @@ QUnit.test('Iterator#windows', assert => {
4447
const itObservable = createIterator([1, 2, 3], observableReturn);
4548
assert.throws(() => windows.call(itObservable, 0x100000000), RangeError, 'throws on argument more then 2^32 - 1');
4649
assert.true(itObservable.called, 'iterator closed on argument validation error');
50+
51+
assert.throws(() => windows.call(createIterator([1]), 2, null), TypeError, 'incorrect `undersized` argument #1');
52+
assert.throws(() => windows.call(createIterator([1]), 2, 'allowpartial'), TypeError, 'incorrect `undersized` argument #2');
4753
});

0 commit comments

Comments
 (0)