Skip to content

Instantly share code, notes, and snippets.

@graffhyrum
Last active December 14, 2025 22:22
Show Gist options
  • Select an option

  • Save graffhyrum/014cde2044ce6acf00e5fa1031e473d9 to your computer and use it in GitHub Desktop.

Select an option

Save graffhyrum/014cde2044ce6acf00e5fa1031e473d9 to your computer and use it in GitHub Desktop.
TS Range
// 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