Last active
December 14, 2025 22:22
-
-
Save graffhyrum/014cde2044ce6acf00e5fa1031e473d9 to your computer and use it in GitHub Desktop.
TS Range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // No idea why this isn't stdlib | |
| export function range(start: number, stop: number, step: number = 1): number[] { | |
| if (!Number.isFinite(start) || !Number.isFinite(stop) || !Number.isFinite(step)) { | |
| throw new TypeError("range(start, stop, step): start/stop/step must be finite numbers"); | |
| } | |
| if (!Number.isInteger(start) || !Number.isInteger(stop) || !Number.isInteger(step)) { | |
| throw new TypeError("range(start, stop, step): start/stop/step must be integers"); | |
| } | |
| if (step === 0) { | |
| throw new RangeError("range(start, stop, step): step must not be 0"); | |
| } | |
| const result: number[] = []; | |
| if (step > 0) { | |
| for (let n = start; n <= stop; n += step) result.push(n); | |
| } else { | |
| for (let n = start; n >= stop; n += step) result.push(n); | |
| } | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment