Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Range function: force start and end values to be defined #1967

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion __tests__/Range.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
Loading