8000 Change Range function: force start and end values to be defined (#1967) · immutable-js/immutable-js@056d854 · GitHub
[go: up one dir, main page]

Skip to content

Commit 056d854

Browse files
authored
Change Range function: force start and end values to be defined (#1967)
1 parent de21da0 commit 056d854

File tree

6 files changed

+36
-14
lines changed

6 files changed

+36
-14
lines changed

__tests__/Range.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,19 @@ describe('Range', () => {
2424
expect(v 10000 .toArray()).toEqual([1, 4, 7]);
2525
});
2626

27+
it('range should contain start and end values', () => {
28+
// @ts-expect-error -- test that runtime error is thrown
29+
expect(() => Range()).toThrow(
30+
'You must define a start value when using Range'
31+
);
32+
// @ts-expect-error -- test that runtime error is thrown
33+
expect(() => Range(1)).toThrow(
34+
'You must define an end value when using Range'
35+
);
36+
});
37+
2738
it('open range', () => {
28-
const v = Range(10);
39+
const v = Range(10, Infinity);
2940
expect(v.size).toBe(Infinity);
3041
expect(v.first()).toBe(10);
3142
expect(v.rest().first()).toBe(11);

__tests__/count.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ describe('count', () => {
6464
});
6565

6666
it('with infinitely long sequences of known length', () => {
67-
const seq = Range();
67+
const seq = Range(0, Infinity);
6868
expect(seq.size).toBe(Infinity);
6969
expect(seq.isEmpty()).toBe(false);
7070
});
7171

7272
it('with infinitely long sequences of unknown length', () => {
73-
const seq = Range().filter(x => x % 2 === 0);
73+
const seq = Range(0, Infinity).filter(x => x % 2 === 0);
7474
expect(seq.size).toBe(undefined);
7575
expect(seq.isEmpty()).toBe(false);
7676
expect(seq.size).toBe(undefined);

__tests__/zip.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('zip', () => {
3636

3737
it('zips with infinite lists', () => {
3838
expect(
39-
Range()
39+
Range(0, Infinity)
4040
.zip(Seq(['A', 'B', 'C']))
4141
.toArray()
4242
).toEqual([
@@ -135,7 +135,7 @@ describe('zip', () => {
135135
});
136136

137137
it('with infinite lists', () => {
138-
const r: Seq.Indexed<any> = Range();
138+
const r: Seq.Indexed<any> = Range(0, Infinity);
139139
const i = r.interleave(Seq(['A', 'B', 'C']));
140140
expect(i.size).toBe(6);
141141
expect(i.toArray()).toEqual([0, 'A', 1, 'B', 2, 'C']);

src/Range.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@ import deepEqual from './utils/deepEqual';
1111
* infinity. When start is equal to end, returns empty list.
1212
*/
1313
export class Range extends IndexedSeq {
14-
constructor(start, end, step) {
14+
constructor(start, end, step = 1) {
1515
if (!(this instanceof Range)) {
1616
return new Range(start, end, step);
1717
}
1818
invariant(step !== 0, 'Cannot step a Range by 0');
19-
start = start || 0;
20-
if (end === undefined) {
21-
end = Infinity;
22-
}
23-
step = step === undefined ? 1 : Math.abs(step);
19+
invariant(
20+
start !== undefined,
21+
'You must define a start value when using Range'
22+
);
23+
invariant(
24+
end !== undefined,
25+
'You must define an end value when using Range'
26+
);
27+
28+
step = Math.abs(step);
2429
if (end < start) {
2530
step = -step;
2631
}

type-definitions/immutable.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,8 +2475,8 @@ declare namespace Immutable {
24752475
* ```
24762476
*/
24772477
function Range(
2478-
start?: number,
2479-
end?: number,
2478+
start: number,
2479+
end: number,
24802480
step?: number
24812481
): Seq.Indexed<number>;
24822482

type-definitions/ts-tests/range.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ import { Range } from 'immutable';
44
// #constructor
55

66
// $ExpectType Indexed<number>
7-
Range(0, 0, 0);
7+
Range(0, 0, 1);
88

99
// $ExpectError
1010
Range('a', 0, 0);
11+
12+
// $ExpectError
13+
Range();
14+
15+
// $ExpectError
16+
Range(1);
1117
}

0 commit comments

Comments
 (0)
0