Skip to content

Commit 2e84cd6

Browse files
committed
Iterator#sliding implementation
1 parent 2516b41 commit 2e84cd6

File tree

15 files changed

+171
-2
lines changed

15 files changed

+171
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Fixed some cases of `Set.prototype.{ symmetricDifference, union }` detection
66
- Added missing dependencies to some entries of static `Iterator` methods
77
- Added missing `/full/{ instance, number/virtual }/clamp` entries
8+
- Added `sliding` method to `Iterator` chunking stage 2 proposal
89
- Compat data improvements:
910
- Added Electron 38 compat data mapping
1011
- `Iterator` helpers marked as fixed and updated following the latest spec changes in Safari 26.0

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3059,25 +3059,30 @@ core-js(-pure)/full/symbol/custom-matcher
30593059
```
30603060

30613061
##### [`Iterator` chunking](https://github.com/tc39/proposal-iterator-chunking)[⬆](#index)
3062-
Modules [`esnext.iterator.chunks`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.chunks.js) and [`esnext.iterator.windows`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.windows.js)
3062+
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)
3063+
and [`esnext.iterator.windows`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.iterator.windows.js)
30633064
```ts
30643065
class Iterator {
30653066
chunks(chunkSize: number): Iterator<any>;
3067+
sliding(windowSize: number): Iterator<any>;
30663068
windows(windowSize: number): Iterator<any>;
30673069
}
30683070
```
30693071
[*CommonJS entry points:*](#commonjs-api)
30703072
```
30713073
core-js/proposals/iterator-chunking
30723074
core-js(-pure)/full/iterator/chunks
3075+
core-js(-pure)/full/iterator/sliding
30733076
core-js(-pure)/full/iterator/windows
30743077
```
3075-
[*Examples*](https://tinyurl.com/ypmzafjc)
3078+
[*Examples*](https://tinyurl.com/24xnkcnn)
30763079
```js
30773080
const digits = () => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].values();
30783081
30793082
let chunksOf2 = Array.from(digits().chunks(2)); // [ [0, 1], [2, 3], [4, 5], [6, 7], [8, 9] ]
30803083
3084+
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] ]
3085+
30813086
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] ]
30823087
```
30833088

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,6 +2484,8 @@ export const data = {
24842484
},
24852485
// TODO: Remove from `core-js@4`
24862486
'esnext.iterator.reduce': null,
2487+
'esnext.iterator.sliding': {
2488+
},
24872489
// TODO: Remove from `core-js@4`
24882490
'esnext.iterator.some': null,
24892491
// TODO: Remove from `core-js@4`

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,7 @@ export default {
299299
'esnext.iterator.zip-keyed',
300300
'esnext.number.clamp',
301301
],
302+
3.44: [
303+
'esnext.iterator.sliding',
304+
],
302305
};

packages/core-js/full/iterator/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var parent = require('../../actual/iterator');
33
require('../../modules/esnext.iterator.chunks');
44
require('../../modules/esnext.iterator.concat');
55
require('../../modules/esnext.iterator.range');
6+
require('../../modules/esnext.iterator.sliding');
67
require('../../modules/esnext.iterator.windows');
78
require('../../modules/esnext.iterator.zip');
89
require('../../modules/esnext.iterator.zip-keyed');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
require('../../modules/es.object.to-string');
3+
require('../../modules/es.iterator.constructor');
4+
require('../../modules/esnext.iterator.sliding');
5+
6+
var entryUnbind = require('../../internals/entry-unbind');
7+
8+
module.exports = entryUnbind('Iterator', 'sliding');
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
var $ = require('../internals/export');
3+
var anObject = require('../internals/an-object');
4+
var call = require('../internals/function-call');
5+
var createIteratorProxy = require('../internals/iterator-create-proxy');
6+
var createIterResultObject = require('../internals/create-iter-result-object');
7+
var getIteratorDirect = require('../internals/get-iterator-direct');
8+
var iteratorClose = require('../internals/iterator-close');
9+
var uncurryThis = require('../internals/function-uncurry-this');
10+
11+
var $RangeError = RangeError;
12+
var push = uncurryThis([].push);
13+
var slice = uncurryThis([].slice);
14+
15+
var IteratorProxy = createIteratorProxy(function () {
16+
var iterator = this.iterator;
17+
var next = this.next;
18+
var buffer = this.buffer;
19+
var windowSize = this.windowSize;
20+
var result, done;
21+
while (true) {
22+
result = anObject(call(next, iterator));
23+
done = this.done = !!result.done;
24+
if (done && buffer.length && buffer.length < windowSize) return createIterResultObject(buffer, false);
25+
if (done) return createIterResultObject(undefined, true);
26+
27+
if (buffer.length === windowSize) this.buffer = buffer = slice(buffer, 1);
28+
push(buffer, result.value);
29+
if (buffer.length === windowSize) return createIterResultObject(buffer, false);
30+
}
31+
}, false, true);
32+
33+
// `Iterator.prototype.sliding` method
34+
// https://github.com/tc39/proposal-iterator-chunking/pull/21
35+
$({ target: 'Iterator', proto: true, real: true, forced: true }, {
36+
sliding: function sliding(windowSize) {
37+
var O = anObject(this);
38+
if (typeof windowSize != 'number' || !windowSize || windowSize >>> 0 !== windowSize) {
39+
return iteratorClose(O, 'throw', new $RangeError('windowSize must be integer in [1, 2^32-1]'));
40+
}
41+
return new IteratorProxy(getIteratorDirect(O), {
42+
windowSize: windowSize,
43+
buffer: []
44+
});
45+
}
46+
});

packages/core-js/proposals/iterator-chunking.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
// https://github.com/tc39/proposal-iterator-chunking
33
require('../modules/esnext.iterator.chunks');
44
require('../modules/esnext.iterator.windows');
5+
require('../modules/esnext.iterator.sliding');

tests/compat/tests.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,6 +1844,9 @@ GLOBAL.tests = {
18441844
'esnext.iterator.range': function () {
18451845
return Iterator.range;
18461846
},
1847+
'esnext.iterator.sliding': function () {
1848+
return Iterator.prototype.sliding;
1849+
},
18471850
'esnext.iterator.to-async': function () {
18481851
return Iterator.prototype.toAsync;
18491852
},

tests/entries/unit.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@ for (PATH of ['core-js-pure', 'core-js']) {
774774
ok(load(NS, 'iterator/concat')([2]).next().value === 2);
775775
ok(load(NS, 'iterator/range')(1, 2).next().value === 1);
776776
ok(typeof load(NS, 'iterator/chunks') == 'function');
777+
ok(typeof load(NS, 'iterator/sliding') == 'function');
777778
ok(typeof load(NS, 'iterator/windows') == 'function');
778779
ok(typeof load(NS, 'iterator/zip') == 'function');
779780
ok(typeof load(NS, 'iterator/zip-keyed') == 'function');

0 commit comments

Comments
 (0)