Skip to content

Commit e443735

Browse files
🚧 progress: Importing existing sources, tests.
Also added a code sample and reversed argument order.
1 parent 42bec32 commit e443735

File tree

7 files changed

+10199
-11
lines changed

7 files changed

+10199
-11
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
Iterable windowed iteration for JavaScript.
55
See [docs](https://iterable-iterator.github.io/window/index.html).
66

7-
> :building_construction: Caveat emptor! This is work in progress. Code may be
8-
> working. Documentation may be present. Coherence may be. Maybe.
9-
107
> :warning: Depending on your environment, the code may require
118
> `regeneratorRuntime` to be defined, for instance by importing
129
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
1310
11+
```js
12+
import {window} from '@iterable-iterator/window';
13+
14+
for (const [x, y] of window(2, 'abcde')) ... ; // ab bc cd de
15+
```
16+
1417
[![License](https://img.shields.io/github/license/iterable-iterator/window.svg)](https://raw.githubusercontent.com/iterable-iterator/window/main/LICENSE)
1518
[![Version](https://img.shields.io/npm/v/@iterable-iterator/window.svg)](https://www.npmjs.org/package/@iterable-iterator/window)
1619
[![Tests](https://img.shields.io/github/workflow/status/iterable-iterator/window/ci:test?event=push&label=tests)](https://github.com/iterable-iterator/window/actions/workflows/ci:test.yml?query=branch:main)

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,17 @@
5959
"release": "np --message ':hatching_chick: release: Bumping to v%s.'",
6060
"test": "ava"
6161
},
62-
"dependencies": {},
62+
"dependencies": {
63+
"@iterable-iterator/iter": "^0.0.2",
64+
"@iterable-iterator/range": "^1.0.0"
65+
},
6366
"devDependencies": {
6467
"@babel/core": "7.14.0",
6568
"@babel/preset-env": "7.14.0",
6669
"@babel/register": "7.13.16",
6770
"@commitlint/cli": "12.1.1",
71+
"@iterable-iterator/list": "^0.0.2",
72+
"@iterable-iterator/zip": "^0.0.2",
6873
"@js-library/commitlint-config": "0.0.4",
6974
"ava": "3.15.0",
7075
"babel-plugin-transform-remove-console": "6.9.4",

src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
const answer = 42;
2-
export default answer;
1+
export {default as window} from './window.js';

src/window.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {iter} from '@iterable-iterator/iter';
2+
import {range} from '@iterable-iterator/range';
3+
4+
/**
5+
* Yields tuples that contain the current element of the input iterable and the
6+
* next <code>n-1</code> elements of the input iterable.
7+
*
8+
* @example
9+
* window( range( 100 ) , 2 ) ;
10+
* // is equivalent to
11+
* zip( range( 100 ) , range( 1 , 100 ) ) ;
12+
*
13+
* @param {number} n - The window size.
14+
* @param {Iterable} iterable - The input iterable.
15+
* @returns {IterableIterator<Array>}
16+
*/
17+
export default function* window(n, iterable) {
18+
// Could have an implementation using a deque
19+
// that doesn't slice (and thus allocate a new
20+
// vector everytime). Though the yield object
21+
// could not be modified by the caller in that case.
22+
23+
const iterator = iter(iterable);
24+
25+
let tuple = [];
26+
27+
// eslint-disable-next-line no-unused-vars
28+
for (const i of range(n)) {
29+
const current = iterator.next();
30+
31+
if (current.done) {
32+
return;
33+
}
34+
35+
tuple.push(current.value);
36+
}
37+
38+
yield tuple;
39+
40+
for (const value of iterator) {
41+
tuple = tuple.slice(1);
42+
tuple.push(value);
43+
yield tuple;
44+
}
45+
}

test/src/api.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/src/window.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import test from 'ava';
2+
3+
import {range} from '@iterable-iterator/range';
4+
import {zip} from '@iterable-iterator/zip';
5+
import {list} from '@iterable-iterator/list';
6+
7+
import {window} from '../../src/index.js';
8+
9+
test('compare to zip output', (t) => {
10+
const A = window(2, range(100));
11+
const B = zip(range(100), range(1, 100));
12+
t.deepEqual(list(A), list(B));
13+
});
14+
15+
test('n !| N', (t) => {
16+
const C = window(3, range(4));
17+
t.deepEqual(list(C), [
18+
[0, 1, 2],
19+
[1, 2, 3],
20+
]);
21+
});
22+
23+
test('window larger than input', (t) => {
24+
const D = window(3, range(2));
25+
t.deepEqual(list(D), [], 'empty');
26+
});

0 commit comments

Comments
 (0)