Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions spec.emu
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ copyright: false
1. If _windowSize_ is not an integral Number in the inclusive interval from *1*<sub>𝔽</sub> to 𝔽(2<sup>32</sup> - 1), then
1. Let _error_ be ThrowCompletion(a newly created *RangeError* object).
1. Return ? IteratorClose(_iterated_, _error_).
1. If _undersized_ is *undefined*, set _undersized_ to *"only full"*.
1. If _undersized_ is neither *"only full"* nor *"allow partial"*, then
1. If _undersized_ is *undefined*, set _undersized_ to *"only-full"*.
1. If _undersized_ is neither *"only-full"* nor *"allow-partial"*, then
1. Let _error_ be ThrowCompletion(a newly created *TypeError* object).
1. Return ? IteratorClose(_iterated_, _error_).
1. Set _iterated_ to ? GetIteratorDirect(_O_).
Expand All @@ -59,7 +59,7 @@ copyright: false
1. Repeat,
1. Let _value_ be ? IteratorStepValue(_iterated_).
1. If _value_ is ~done~, then
1. If _undersized_ is *"allow partial"*, _buffer_ is not empty, and the number of elements in _buffer_ &lt; ℝ(_windowSize_), then
1. If _undersized_ is *"allow-partial"*, _buffer_ is not empty, and the number of elements in _buffer_ &lt; ℝ(_windowSize_), then
1. Perform Completion(Yield(CreateArrayFromList(_buffer_))).
1. Return ReturnCompletion(*undefined*).
1. If the number of elements in _buffer_ is ℝ(_windowSize_), then
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function chunks(this: unknown, chunkSize: unknown): Generator<unknown> {
return chunksImpl(this as Iterator<unknown>, chunkSize)
}

function* windowsImpl<A>(iter: Iterator<A>, windowSize: number, undersized: 'only full' | 'allow partial'): Generator<Array<A>> {
function* windowsImpl<A>(iter: Iterator<A>, windowSize: number, undersized: 'only-full' | 'allow-partial'): Generator<Array<A>> {
let buffer = [];
for (const elem of liftIterator(iter)) {
if (buffer.length === windowSize) {
Expand All @@ -42,12 +42,12 @@ function* windowsImpl<A>(iter: Iterator<A>, windowSize: number, undersized: 'onl
yield buffer.slice();
}
}
if (undersized === 'allow partial' && 0 < buffer.length && buffer.length < windowSize) {
if (undersized === 'allow-partial' && 0 < buffer.length && buffer.length < windowSize) {
yield buffer;
}
}

function windows<A>(this: Iterator<A>, windowSize: number, undersized?: 'only full' | 'allow partial'): Generator<Array<A>>
function windows<A>(this: Iterator<A>, windowSize: number, undersized?: 'only-full' | 'allow-partial'): Generator<Array<A>>
function windows(this: unknown, windowSize: unknown, undersized?: unknown): Generator<unknown> {
if (
typeof windowSize !== 'number'
Expand All @@ -58,9 +58,9 @@ function windows(this: unknown, windowSize: unknown, undersized?: unknown): Gene
throw new RangeError;
}
if (undersized === undefined) {
undersized = 'only full';
undersized = 'only-full';
}
if (undersized !== 'only full' && undersized !== 'allow partial') {
if (undersized !== 'only-full' && undersized !== 'allow-partial') {
throw new TypeError;
}
return windowsImpl(this as Iterator<unknown>, windowSize, undersized);
Expand Down
44 changes: 22 additions & 22 deletions test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ test('windows', async t => {
[[0, 1], [1, 2], [2, 3], [3, 4]],
);
assert.deepEqual(
Array.from(nats(5).windows(2, "only full")),
Array.from(nats(5).windows(2, "only-full")),
[[0, 1], [1, 2], [2, 3], [3, 4]],
);
assert.deepEqual(
Array.from(nats(5).windows(2, "allow partial")),
Array.from(nats(5).windows(2, "allow-partial")),
[[0, 1], [1, 2], [2, 3], [3, 4]],
);

Expand All @@ -83,11 +83,11 @@ test('windows', async t => {
[[0], [1], [2], [3], [4]],
);
assert.deepEqual(
Array.from(nats(5).windows(1, "only full")),
Array.from(nats(5).windows(1, "only-full")),
[[0], [1], [2], [3], [4]],
);
assert.deepEqual(
Array.from(nats(5).windows(1, "allow partial")),
Array.from(nats(5).windows(1, "allow-partial")),
[[0], [1], [2], [3], [4]],
);

Expand All @@ -100,11 +100,11 @@ test('windows', async t => {
[[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5]],
);
assert.deepEqual(
Array.from(nats(6).windows(3, "only full")),
Array.from(nats(6).windows(3, "only-full")),
[[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5]],
);
assert.deepEqual(
Array.from(nats(6).windows(3, "allow partial")),
Array.from(nats(6).windows(3, "allow-partial")),
[[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5]],
);

Expand All @@ -117,11 +117,11 @@ test('windows', async t => {
[],
);
assert.deepEqual(
Array.from(nats(6).windows(100, "only full")),
Array.from(nats(6).windows(100, "only-full")),
[],
);
assert.deepEqual(
Array.from(nats(6).windows(100, "allow partial")),
Array.from(nats(6).windows(100, "allow-partial")),
[[0, 1, 2, 3, 4, 5]],
);

Expand All @@ -134,32 +134,32 @@ test('windows', async t => {
[],
);
assert.deepEqual(
Array.from(nats(0).windows(2, "only full")),
Array.from(nats(0).windows(2, "only-full")),
[],
);
assert.deepEqual(
Array.from(nats(0).windows(2, "allow partial")),
Array.from(nats(0).windows(2, "allow-partial")),
[],
);

assert.throws(() => {
nats(1).windows()
}, RangeError);
assert.throws(() => {
nats(1).windows(undefined, "only full")
nats(1).windows(undefined, "only-full")
}, RangeError);
assert.throws(() => {
nats(1).windows(undefined, "allow partial")
nats(1).windows(undefined, "allow-partial")
}, RangeError);

assert.throws(() => {
nats(1).windows([2]);
}, RangeError);
assert.throws(() => {
nats(1).windows([2], "only full");
nats(1).windows([2], "only-full");
}, RangeError);
assert.throws(() => {
nats(1).windows([2], "allow partial");
nats(1).windows([2], "allow-partial");
}, RangeError);

assert.throws(() => {
Expand All @@ -169,10 +169,10 @@ test('windows', async t => {
nats(1).windows(0, undefined);
}, RangeError);
assert.throws(() => {
nats(1).windows(0, "only full");
nats(1).windows(0, "only-full");
}, RangeError);
assert.throws(() => {
nats(1).windows(0, "allow partial");
nats(1).windows(0, "allow-partial");
}, RangeError);

assert.throws(() => {
Expand All @@ -182,10 +182,10 @@ test('windows', async t => {
nats(1).windows(-1, undefined);
}, RangeError);
assert.throws(() => {
nats(1).windows(-1, "only full");
nats(1).windows(-1, "only-full");
}, RangeError);
assert.throws(() => {
nats(1).windows(-1, "allow partial");
nats(1).windows(-1, "allow-partial");
}, RangeError);

assert.throws(() => {
Expand All @@ -195,10 +195,10 @@ test('windows', async t => {
nats(1).windows(1.5, undefined);
}, RangeError);
assert.throws(() => {
nats(1).windows(1.5, "only full");
nats(1).windows(1.5, "only-full");
}, RangeError);
assert.throws(() => {
nats(1).windows(1.5, "allow partial");
nats(1).windows(1.5, "allow-partial");
}, RangeError);

assert.throws(() => {
Expand All @@ -208,10 +208,10 @@ test('windows', async t => {
nats(1).windows(Math.pow(2, 53), undefined);
}, RangeError);
assert.throws(() => {
nats(1).windows(Math.pow(2, 53), "only full");
nats(1).windows(Math.pow(2, 53), "only-full");
}, RangeError);
assert.throws(() => {
nats(1).windows(Math.pow(2, 53), "allow partial");
nats(1).windows(Math.pow(2, 53), "allow-partial");
}, RangeError);

assert.throws(() => {
Expand Down