Skip to content

Commit a36b4e0

Browse files
author
Yannick Croissant
authored
fix(range): fix range calculation when step is set (#4398)
1 parent ee6a9c6 commit a36b4e0

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/lib/utils/__tests__/range-test.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,35 @@ describe('range', () => {
1313
expect(range({ end: 20, step: 5 })).toEqual([0, 5, 10, 15]);
1414
});
1515

16+
test('with start, end and step', () => {
17+
expect(range({ start: 1300, end: 1350, step: 5 })).toEqual([
18+
1300,
19+
1305,
20+
1310,
21+
1315,
22+
1320,
23+
1325,
24+
1330,
25+
1335,
26+
1340,
27+
1345,
28+
]);
29+
});
30+
1631
test('rounds decimal array lengths', () => {
1732
// (5000 - 1) / 500 = 9.998 so we want the array length to be rounded to 10.
1833
expect(range({ start: 1, end: 5000, step: 500 })).toHaveLength(10);
1934
expect(range({ start: 1, end: 5000, step: 500 })).toEqual([
20-
500,
21-
1000,
22-
1500,
23-
2000,
24-
2500,
25-
3000,
26-
3500,
27-
4000,
28-
4500,
29-
5000,
35+
1,
36+
501,
37+
1001,
38+
1501,
39+
2001,
40+
2501,
41+
3001,
42+
3501,
43+
4001,
44+
4501,
3045
]);
3146
});
3247
});

src/lib/utils/range.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function range({ start = 0, end, step = 1 }: RangeOptions): number[] {
1616
const arrayLength = Math.round((end - start) / limitStep);
1717

1818
return [...Array(arrayLength)].map(
19-
(_, current) => (start + current) * limitStep
19+
(_, current) => start + current * limitStep
2020
);
2121
}
2222

0 commit comments

Comments
 (0)