Skip to content

Commit 2a3efb8

Browse files
throw a TypeError for non-Numbers and non-integral Numbers
1 parent 8be227a commit 2a3efb8

3 files changed

Lines changed: 72 additions & 38 deletions

File tree

spec.emu

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ copyright: false
1616
1. Let _O_ be the *this* value.
1717
1. If _O_ is not an Object, throw a *TypeError* exception.
1818
1. Let _iterated_ be the Iterator Record { [[Iterator]]: _O_, [[NextMethod]]: *undefined*, [[Done]]: *false* }.
19-
1. If _chunkSize_ is not an integral Number in the inclusive interval from *1*<sub>𝔽</sub> to 𝔽(2<sup>32</sup> - 1), then
19+
1. If _chunkSize_ is not an integral Number, then
20+
1. Let _error_ be ThrowCompletion(a newly created *TypeError* object).
21+
1. Return ? IteratorClose(_iterated_, _error_).
22+
1. If _chunkSize_ is not in the inclusive interval from *1*<sub>𝔽</sub> to 𝔽(2<sup>32</sup> - 1), then
2023
1. Let _error_ be ThrowCompletion(a newly created *RangeError* object).
2124
1. Return ? IteratorClose(_iterated_, _error_).
2225
1. Set _iterated_ to ? GetIteratorDirect(_O_).
@@ -46,7 +49,10 @@ copyright: false
4649
1. Let _O_ be the *this* value.
4750
1. If _O_ is not an Object, throw a *TypeError* exception.
4851
1. Let _iterated_ be the Iterator Record { [[Iterator]]: _O_, [[NextMethod]]: *undefined*, [[Done]]: *false* }.
49-
1. If _windowSize_ is not an integral Number in the inclusive interval from *1*<sub>𝔽</sub> to 𝔽(2<sup>32</sup> - 1), then
52+
1. If _windowSize_ is not an integral Number, then
53+
1. Let _error_ be ThrowCompletion(a newly created *TypeError* object).
54+
1. Return ? IteratorClose(_iterated_, _error_).
55+
1. If _windowSize_ is not in the inclusive interval from *1*<sub>𝔽</sub> to 𝔽(2<sup>32</sup> - 1), then
5056
1. Let _error_ be ThrowCompletion(a newly created *RangeError* object).
5157
1. Return ? IteratorClose(_iterated_, _error_).
5258
1. If _undersized_ is *undefined*, set _undersized_ to *"only-full"*.

src/index.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ function* chunksImpl<A>(iter: Iterator<A>, chunkSize: number): Generator<Array<A
2020

2121
function chunks<A>(this: Iterator<A>, chunkSize: number): Generator<Array<A>>
2222
function chunks(this: unknown, chunkSize: unknown): Generator<unknown> {
23-
if (
24-
typeof chunkSize !== 'number'
25-
|| chunkSize <= 0
26-
|| Math.floor(chunkSize) !== chunkSize
27-
|| chunkSize >= Math.pow(2, 53)
28-
) {
23+
if (typeof chunkSize !== 'number' || Math.floor(chunkSize) !== chunkSize) {
24+
throw new TypeError;
25+
}
26+
if (chunkSize <= 0 || chunkSize >= Math.pow(2, 53)) {
2927
throw new RangeError;
3028
}
3129
return chunksImpl(this as Iterator<unknown>, chunkSize)
@@ -49,12 +47,10 @@ function* windowsImpl<A>(iter: Iterator<A>, windowSize: number, undersized: 'onl
4947

5048
function windows<A>(this: Iterator<A>, windowSize: number, undersized?: 'only-full' | 'allow-partial'): Generator<Array<A>>
5149
function windows(this: unknown, windowSize: unknown, undersized?: unknown): Generator<unknown> {
52-
if (
53-
typeof windowSize !== 'number'
54-
|| windowSize <= 0
55-
|| Math.floor(windowSize) !== windowSize
56-
|| windowSize >= Math.pow(2, 53)
57-
) {
50+
if (typeof windowSize !== 'number' || Math.floor(windowSize) !== windowSize) {
51+
throw new TypeError;
52+
}
53+
if (windowSize <= 0 || windowSize >= Math.pow(2, 53)) {
5854
throw new RangeError;
5955
}
6056
if (undersized === undefined) {

test/index.mjs

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,25 @@ test('chunks', async t => {
3838

3939
assert.throws(() => {
4040
nats(1).chunks();
41-
}, RangeError)
41+
}, TypeError)
4242
assert.throws(() => {
4343
nats(1).chunks([2]);
44-
}, RangeError)
44+
},TypeError)
45+
assert.throws(() => {
46+
nats(1).chunks(1.5);
47+
}, TypeError)
48+
assert.throws(() => {
49+
nats(1).chunks('1');
50+
}, TypeError)
51+
assert.throws(() => {
52+
nats(1).chunks(NaN);
53+
}, TypeError)
4554
assert.throws(() => {
4655
nats(1).chunks(0);
4756
}, RangeError)
4857
assert.throws(() => {
4958
nats(1).chunks(-1);
5059
}, RangeError)
51-
assert.throws(() => {
52-
nats(1).chunks(1.5);
53-
}, RangeError)
5460
assert.throws(() => {
5561
nats(1).chunks(Math.pow(2, 53));
5662
}, RangeError)
@@ -144,23 +150,62 @@ test('windows', async t => {
144150

145151
assert.throws(() => {
146152
nats(1).windows()
147-
}, RangeError);
153+
}, TypeError);
148154
assert.throws(() => {
149155
nats(1).windows(undefined, "only-full")
150-
}, RangeError);
156+
}, TypeError);
151157
assert.throws(() => {
152158
nats(1).windows(undefined, "allow-partial")
153-
}, RangeError);
159+
}, TypeError);
154160

155161
assert.throws(() => {
156162
nats(1).windows([2]);
157-
}, RangeError);
163+
}, TypeError);
158164
assert.throws(() => {
159165
nats(1).windows([2], "only-full");
160-
}, RangeError);
166+
}, TypeError);
161167
assert.throws(() => {
162168
nats(1).windows([2], "allow-partial");
163-
}, RangeError);
169+
}, TypeError);
170+
171+
assert.throws(() => {
172+
nats(1).windows(1.5);
173+
}, TypeError);
174+
assert.throws(() => {
175+
nats(1).windows(1.5, undefined);
176+
}, TypeError);
177+
assert.throws(() => {
178+
nats(1).windows(1.5, "only-full");
179+
}, TypeError);
180+
assert.throws(() => {
181+
nats(1).windows(1.5, "allow-partial");
182+
}, TypeError);
183+
184+
assert.throws(() => {
185+
nats(1).windows('1');
186+
}, TypeError);
187+
assert.throws(() => {
188+
nats(1).windows('1', undefined);
189+
}, TypeError);
190+
assert.throws(() => {
191+
nats(1).windows('1', "only-full");
192+
}, TypeError);
193+
assert.throws(() => {
194+
nats(1).windows('1', "allow-partial");
195+
}, TypeError);
196+
197+
assert.throws(() => {
198+
nats(1).windows(NaN);
199+
}, TypeError);
200+
assert.throws(() => {
201+
nats(1).windows(NaN, undefined);
202+
}, TypeError);
203+
assert.throws(() => {
204+
nats(1).windows(NaN, "only-full");
205+
}, TypeError);
206+
assert.throws(() => {
207+
nats(1).windows(NaN, "allow-partial");
208+
}, TypeError);
164209

165210
assert.throws(() => {
166211
nats(1).windows(0);
@@ -188,19 +233,6 @@ test('windows', async t => {
188233
nats(1).windows(-1, "allow-partial");
189234
}, RangeError);
190235

191-
assert.throws(() => {
192-
nats(1).windows(1.5);
193-
}, RangeError);
194-
assert.throws(() => {
195-
nats(1).windows(1.5, undefined);
196-
}, RangeError);
197-
assert.throws(() => {
198-
nats(1).windows(1.5, "only-full");
199-
}, RangeError);
200-
assert.throws(() => {
201-
nats(1).windows(1.5, "allow-partial");
202-
}, RangeError);
203-
204236
assert.throws(() => {
205237
nats(1).windows(Math.pow(2, 53));
206238
}, RangeError);

0 commit comments

Comments
 (0)