Skip to content

Commit 627e897

Browse files
authored
Merge pull request #1443 from zloirock/sliding
2 parents 1f2c6ad + afc1ccf commit 627e897

File tree

17 files changed

+183
-37
lines changed

17 files changed

+183
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Fixed some cases of `Set.prototype.{ symmetricDifference, union }` detection
88
- Added missing dependencies to some entries of static `Iterator` methods
99
- Added missing `/full/{ instance, number/virtual }/clamp` entries
10+
- Added `sliding` method to `Iterator` chunking stage 2 proposal
1011
- Compat data improvements:
1112
- Added Electron 38 and 39 compat data mapping
1213
- Added Oculus Quest Browser 38 and 39 compat data mapping

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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
var anObject = require('../internals/an-object');
3+
var call = require('../internals/function-call');
4+
var createIteratorProxy = require('../internals/iterator-create-proxy');
5+
var createIterResultObject = require('../internals/create-iter-result-object');
6+
var getIteratorDirect = require('../internals/get-iterator-direct');
7+
var iteratorClose = require('../internals/iterator-close');
8+
var uncurryThis = require('../internals/function-uncurry-this');
9+
10+
var $RangeError = RangeError;
11+
var push = uncurryThis([].push);
12+
var slice = uncurryThis([].slice);
13+
14+
var IteratorProxy = createIteratorProxy(function () {
15+
var iterator = this.iterator;
16+
var next = this.next;
17+
var buffer = this.buffer;
18+
var windowSize = this.windowSize;
19+
var sliding = this.sliding;
20+
var result, done;
21+
while (true) {
22+
result = anObject(call(next, iterator));
23+
done = this.done = !!result.done;
24+
if (sliding && 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` and `Iterator.prototype.windows` methods
34+
// https://github.com/tc39/proposal-iterator-chunking
35+
module.exports = function (O, windowSize, sliding) {
36+
anObject(O);
37+
if (typeof windowSize != 'number' || !windowSize || windowSize >>> 0 !== windowSize) {
38+
return iteratorClose(O, 'throw', new $RangeError('windowSize must be integer in [1, 2^32-1]'));
39+
}
40+
return new IteratorProxy(getIteratorDirect(O), {
41+
windowSize: windowSize,
42+
buffer: [],
43+
sliding: sliding
44+
});
45+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
var $ = require('../internals/export');
3+
var iteratorWindow = require('../internals/iterator-window');
4+
5+
// `Iterator.prototype.sliding` method
6+
// https://github.com/tc39/proposal-iterator-chunking
7+
$({ target: 'Iterator', proto: true, real: true, forced: true }, {
8+
sliding: function sliding(windowSize) {
9+
return iteratorWindow(this, windowSize, true);
10+
}
11+
});
Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,11 @@
11
'use strict';
22
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 getIteratorDirect = require('../internals/get-iterator-direct');
7-
var iteratorClose = require('../internals/iterator-close');
8-
var uncurryThis = require('../internals/function-uncurry-this');
9-
10-
var $RangeError = RangeError;
11-
var push = uncurryThis([].push);
12-
var slice = uncurryThis([].slice);
13-
14-
var IteratorProxy = createIteratorProxy(function () {
15-
var iterator = this.iterator;
16-
var next = this.next;
17-
var buffer = this.buffer;
18-
var windowSize = this.windowSize;
19-
var result, done;
20-
while (true) {
21-
result = anObject(call(next, iterator));
22-
done = this.done = !!result.done;
23-
if (done) return;
24-
25-
if (buffer.length === windowSize) this.buffer = buffer = slice(buffer, 1);
26-
push(buffer, result.value);
27-
if (buffer.length === windowSize) return buffer;
28-
}
29-
});
3+
var iteratorWindow = require('../internals/iterator-window');
304

315
// `Iterator.prototype.windows` method
326
// https://github.com/tc39/proposal-iterator-chunking
337
$({ target: 'Iterator', proto: true, real: true, forced: true }, {
348
windows: function windows(windowSize) {
35-
var O = anObject(this);
36-
if (typeof windowSize != 'number' || !windowSize || windowSize >>> 0 !== windowSize) {
37-
return iteratorClose(O, 'throw', new $RangeError('windowSize must be integer in [1, 2^32-1]'));
38-
}
39-
return new IteratorProxy(getIteratorDirect(O), {
40-
windowSize: windowSize,
41-
buffer: []
42-
});
9+
return iteratorWindow(this, windowSize, false);
4310
}
4411
});

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');

0 commit comments

Comments
 (0)