Generate a Range of Numbers

This function returns an array of all numbers between the start and end numbers.

js
1const range = (options) => {
2 const { start = 0, end, step = 1 } = options;
3
4 if (!end || end <= start) {
5 return [];
6 }
7
8 return Array.from(
9 { length: Math.ceil((end - start) / step) },
10 (_, i) => i + step + start,
11 );
12};
13
14// Usage
15const r1 = range({ end: 10 });
16// [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
17const r2 = range({ start: 10, end: 20 });
18// [ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
19const r3 = range({ start: 10, end: 20, step: 2 });
20// [ 12, 13, 14, 15, 16 ]