Skip to content

Commit

Permalink
Change Range function: force start and end values to be defined (#1967)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Sep 25, 2023
1 parent de21da0 commit 056d854
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
13 changes: 12 additions & 1 deletion __tests__/Range.ts
Expand Up @@ -24,8 +24,19 @@ describe('Range', () => {
expect(v.toArray()).toEqual([1, 4, 7]);
});

it('range should contain start and end values', () => {
// @ts-expect-error -- test that runtime error is thrown
expect(() => Range()).toThrow(
'You must define a start value when using Range'
);
// @ts-expect-error -- test that runtime error is thrown
expect(() => Range(1)).toThrow(
'You must define an end value when using Range'
);
});

it('open range', () => {
const v = Range(10);
const v = Range(10, Infinity);
expect(v.size).toBe(Infinity);
expect(v.first()).toBe(10);
expect(v.rest().first()).toBe(11);
Expand Down
4 changes: 2 additions & 2 deletions __tests__/count.ts
Expand Up @@ -64,13 +64,13 @@ describe('count', () => {
});

it('with infinitely long sequences of known length', () => {
const seq = Range();
const seq = Range(0, Infinity);
expect(seq.size).toBe(Infinity);
expect(seq.isEmpty()).toBe(false);
});

it('with infinitely long sequences of unknown length', () => {
const seq = Range().filter(x => x % 2 === 0);
const seq = Range(0, Infinity).filter(x => x % 2 === 0);
expect(seq.size).toBe(undefined);
expect(seq.isEmpty()).toBe(false);
expect(seq.size).toBe(undefined);
Expand Down
4 changes: 2 additions & 2 deletions __tests__/zip.ts
Expand Up @@ -36,7 +36,7 @@ describe('zip', () => {

it('zips with infinite lists', () => {
expect(
Range()
Range(0, Infinity)
.zip(Seq(['A', 'B', 'C']))
.toArray()
).toEqual([
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('zip', () => {
});

it('with infinite lists', () => {
const r: Seq.Indexed<any> = Range();
const r: Seq.Indexed<any> = Range(0, Infinity);
const i = r.interleave(Seq(['A', 'B', 'C']));
expect(i.size).toBe(6);
expect(i.toArray()).toEqual([0, 'A', 1, 'B', 2, 'C']);
Expand Down
17 changes: 11 additions & 6 deletions src/Range.js
Expand Up @@ -11,16 +11,21 @@ import deepEqual from './utils/deepEqual';
* infinity. When start is equal to end, returns empty list.
*/
export class Range extends IndexedSeq {
constructor(start, end, step) {
constructor(start, end, step = 1) {
if (!(this instanceof Range)) {
return new Range(start, end, step);
}
invariant(step !== 0, 'Cannot step a Range by 0');
start = start || 0;
if (end === undefined) {
end = Infinity;
}
step = step === undefined ? 1 : Math.abs(step);
invariant(
start !== undefined,
'You must define a start value when using Range'
);
invariant(
end !== undefined,
'You must define an end value when using Range'
);

step = Math.abs(step);
if (end < start) {
step = -step;
}
Expand Down
4 changes: 2 additions & 2 deletions type-definitions/immutable.d.ts
Expand Up @@ -2475,8 +2475,8 @@ declare namespace Immutable {
* ```
*/
function Range(
start?: number,
end?: number,
start: number,
end: number,
step?: number
): Seq.Indexed<number>;

Expand Down
8 changes: 7 additions & 1 deletion type-definitions/ts-tests/range.ts
Expand Up @@ -4,8 +4,14 @@ import { Range } from 'immutable';
// #constructor

// $ExpectType Indexed<number>
Range(0, 0, 0);
Range(0, 0, 1);

// $ExpectError
Range('a', 0, 0);

// $ExpectError
Range();

// $ExpectError
Range(1);
}

0 comments on commit 056d854

Please sign in to comment.