diff --git a/spec.emu b/spec.emu index 1eeb8fa..9a449b5 100644 --- a/spec.emu +++ b/spec.emu @@ -16,7 +16,10 @@ copyright: false 1. Let _O_ be the *this* value. 1. If _O_ is not an Object, throw a *TypeError* exception. 1. Let _iterated_ be the Iterator Record { [[Iterator]]: _O_, [[NextMethod]]: *undefined*, [[Done]]: *false* }. - 1. If _chunkSize_ is not an integral Number in the inclusive interval from *1*𝔽 to 𝔽(232 - 1), then + 1. If _chunkSize_ is not an integral Number, then + 1. Let _error_ be ThrowCompletion(a newly created *TypeError* object). + 1. Return ? IteratorClose(_iterated_, _error_). + 1. If _chunkSize_ is not in the inclusive interval from *1*𝔽 to 𝔽(232 - 1), then 1. Let _error_ be ThrowCompletion(a newly created *RangeError* object). 1. Return ? IteratorClose(_iterated_, _error_). 1. Set _iterated_ to ? GetIteratorDirect(_O_). @@ -46,7 +49,10 @@ copyright: false 1. Let _O_ be the *this* value. 1. If _O_ is not an Object, throw a *TypeError* exception. 1. Let _iterated_ be the Iterator Record { [[Iterator]]: _O_, [[NextMethod]]: *undefined*, [[Done]]: *false* }. - 1. If _windowSize_ is not an integral Number in the inclusive interval from *1*𝔽 to 𝔽(232 - 1), then + 1. If _windowSize_ is not an integral Number, then + 1. Let _error_ be ThrowCompletion(a newly created *TypeError* object). + 1. Return ? IteratorClose(_iterated_, _error_). + 1. If _windowSize_ is not in the inclusive interval from *1*𝔽 to 𝔽(232 - 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"*. diff --git a/src/index.ts b/src/index.ts index 47ffc77..57a95d3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,12 +20,10 @@ function* chunksImpl(iter: Iterator, chunkSize: number): Generator(this: Iterator, chunkSize: number): Generator> function chunks(this: unknown, chunkSize: unknown): Generator { - if ( - typeof chunkSize !== 'number' - || chunkSize <= 0 - || Math.floor(chunkSize) !== chunkSize - || chunkSize >= Math.pow(2, 53) - ) { + if (typeof chunkSize !== 'number' || Math.floor(chunkSize) !== chunkSize) { + throw new TypeError; + } + if (chunkSize <= 0 || chunkSize >= Math.pow(2, 53)) { throw new RangeError; } return chunksImpl(this as Iterator, chunkSize) @@ -49,12 +47,10 @@ function* windowsImpl(iter: Iterator, windowSize: number, undersized: 'onl function windows(this: Iterator, windowSize: number, undersized?: 'only-full' | 'allow-partial'): Generator> function windows(this: unknown, windowSize: unknown, undersized?: unknown): Generator { - if ( - typeof windowSize !== 'number' - || windowSize <= 0 - || Math.floor(windowSize) !== windowSize - || windowSize >= Math.pow(2, 53) - ) { + if (typeof windowSize !== 'number' || Math.floor(windowSize) !== windowSize) { + throw new TypeError; + } + if (windowSize <= 0 || windowSize >= Math.pow(2, 53)) { throw new RangeError; } if (undersized === undefined) { diff --git a/test/index.mjs b/test/index.mjs index 5688e8b..80ff1f2 100644 --- a/test/index.mjs +++ b/test/index.mjs @@ -38,19 +38,25 @@ test('chunks', async t => { assert.throws(() => { nats(1).chunks(); - }, RangeError) + }, TypeError) assert.throws(() => { nats(1).chunks([2]); - }, RangeError) + },TypeError) + assert.throws(() => { + nats(1).chunks(1.5); + }, TypeError) + assert.throws(() => { + nats(1).chunks('1'); + }, TypeError) + assert.throws(() => { + nats(1).chunks(NaN); + }, TypeError) assert.throws(() => { nats(1).chunks(0); }, RangeError) assert.throws(() => { nats(1).chunks(-1); }, RangeError) - assert.throws(() => { - nats(1).chunks(1.5); - }, RangeError) assert.throws(() => { nats(1).chunks(Math.pow(2, 53)); }, RangeError) @@ -144,23 +150,62 @@ test('windows', async t => { assert.throws(() => { nats(1).windows() - }, RangeError); + }, TypeError); assert.throws(() => { nats(1).windows(undefined, "only-full") - }, RangeError); + }, TypeError); assert.throws(() => { nats(1).windows(undefined, "allow-partial") - }, RangeError); + }, TypeError); assert.throws(() => { nats(1).windows([2]); - }, RangeError); + }, TypeError); assert.throws(() => { nats(1).windows([2], "only-full"); - }, RangeError); + }, TypeError); assert.throws(() => { nats(1).windows([2], "allow-partial"); - }, RangeError); + }, TypeError); + + assert.throws(() => { + nats(1).windows(1.5); + }, TypeError); + assert.throws(() => { + nats(1).windows(1.5, undefined); + }, TypeError); + assert.throws(() => { + nats(1).windows(1.5, "only-full"); + }, TypeError); + assert.throws(() => { + nats(1).windows(1.5, "allow-partial"); + }, TypeError); + + assert.throws(() => { + nats(1).windows('1'); + }, TypeError); + assert.throws(() => { + nats(1).windows('1', undefined); + }, TypeError); + assert.throws(() => { + nats(1).windows('1', "only-full"); + }, TypeError); + assert.throws(() => { + nats(1).windows('1', "allow-partial"); + }, TypeError); + + assert.throws(() => { + nats(1).windows(NaN); + }, TypeError); + assert.throws(() => { + nats(1).windows(NaN, undefined); + }, TypeError); + assert.throws(() => { + nats(1).windows(NaN, "only-full"); + }, TypeError); + assert.throws(() => { + nats(1).windows(NaN, "allow-partial"); + }, TypeError); assert.throws(() => { nats(1).windows(0); @@ -188,19 +233,6 @@ test('windows', async t => { nats(1).windows(-1, "allow-partial"); }, RangeError); - assert.throws(() => { - nats(1).windows(1.5); - }, RangeError); - assert.throws(() => { - nats(1).windows(1.5, undefined); - }, RangeError); - assert.throws(() => { - nats(1).windows(1.5, "only-full"); - }, RangeError); - assert.throws(() => { - nats(1).windows(1.5, "allow-partial"); - }, RangeError); - assert.throws(() => { nats(1).windows(Math.pow(2, 53)); }, RangeError);