Skip to content

Commit

Permalink
Fixes #6: don't default chunk/window size to 2 (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelficarra committed Sep 6, 2024
1 parent 1eba4ad commit 50e5209
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
6 changes: 2 additions & 4 deletions spec.emu
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ copyright: false
</pre>

<emu-clause id="sec-iterator.prototype.chunks">
<h1>Iterator.prototype.chunks ( [ _chunkSize_ ] )</h1>
<h1>Iterator.prototype.chunks ( _chunkSize_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _O_ be the *this* value.
1. If _O_ is not an Object, throw a *TypeError* exception.
1. If _chunkSize_ is not present, set _chunkSize_ to *2*<sub>𝔽</sub>.
1. If _chunkSize_ is not an integral Number in the inclusive interval from *1*<sub>𝔽</sub> to 𝔽(2<sup>32</sup> - 1), throw a *RangeError* exception.
1. Let _iterated_ be ? GetIteratorDirect(_O_).
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _chunkSize_ and performs the following steps when called:
Expand All @@ -35,12 +34,11 @@ copyright: false
</emu-clause>

<emu-clause id="sec-iterator.prototype.windows">
<h1>Iterator.prototype.windows ( [ _windowSize_ ] )</h1>
<h1>Iterator.prototype.windows ( _windowSize_ )</h1>
<p>This method performs the following steps when called:</p>
<emu-alg>
1. Let _O_ be the *this* value.
1. If _O_ is not an Object, throw a *TypeError* exception.
1. If _windowSize_ is not present, set _windowSize_ to *2*<sub>𝔽</sub>.
1. If _windowSize_ is not an integral Number in the inclusive interval from *1*<sub>𝔽</sub> to 𝔽(2<sup>32</sup> - 1), throw a *RangeError* exception.
1. Let _iterated_ be ? GetIteratorDirect(_O_).
1. Let _closure_ be a new Abstract Closure with no parameters that captures _iterated_ and _windowSize_ and performs the following steps when called:
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function* chunksImpl<A>(iter: Iterator<A>, chunkSize: number): Generator<Array<A
}

function chunks<A>(this: Iterator<A>, chunkSize: number): Generator<Array<A>>
function chunks(this: unknown, chunkSize: unknown = 2): Generator<unknown> {
function chunks(this: unknown, chunkSize: unknown): Generator<unknown> {
if (
typeof chunkSize !== 'number'
|| chunkSize <= 0
Expand All @@ -45,7 +45,7 @@ function* windowsImpl<A>(iter: Iterator<A>, windowSize: number): Generator<Array
}

function windows<A>(this: Iterator<A>, windowSize: number): Generator<Array<A>>
function windows(this: unknown, windowSize: unknown = 2): Generator<unknown> {
function windows(this: unknown, windowSize: unknown): Generator<unknown> {
if (
typeof windowSize !== 'number'
|| windowSize <= 0
Expand Down
12 changes: 9 additions & 3 deletions test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function* nats(limit) {

test('chunks', async t => {
assert.deepEqual(
Array.from(nats(5).chunks()),
Array.from(nats(5).chunks(2)),
[[0, 1], [2, 3], [4]],
);
assert.deepEqual(
Expand All @@ -36,6 +36,9 @@ test('chunks', async t => {
[],
);

assert.throws(() => {
nats(1).chunks();
}, RangeError)
assert.throws(() => {
nats(1).chunks([2]);
}, RangeError)
Expand All @@ -55,7 +58,7 @@ test('chunks', async t => {

test('windows', async t => {
assert.deepEqual(
Array.from(nats(5).windows()),
Array.from(nats(5).windows(2)),
[[0, 1], [1, 2], [2, 3], [3, 4]],
);
assert.deepEqual(
Expand All @@ -75,6 +78,9 @@ test('windows', async t => {
[],
);

assert.throws(() => {
nats(1).windows()
}, RangeError)
assert.throws(() => {
nats(1).windows([2]);
}, RangeError)
Expand All @@ -90,4 +96,4 @@ test('windows', async t => {
assert.throws(() => {
nats(1).windows(Math.pow(2, 53));
}, RangeError)
});
});

0 comments on commit 50e5209

Please sign in to comment.