Skip to content

Instantly share code, notes, and snippets.

@mmyoji
Created December 11, 2023 09:46
Show Gist options
  • Save mmyoji/b1ec5f4af5ad82eea8505785f68390c4 to your computer and use it in GitHub Desktop.
Save mmyoji/b1ec5f4af5ad82eea8505785f68390c4 to your computer and use it in GitHub Desktop.
`range()` function for TypeScript
/**
* ref: https://www.freecodecamp.org/news/javascript-range-create-an-array-of-numbers-with-the-from-method/
*
* # Example
*
* ```ts
* import { range } from "./main.ts";
*
* range(1, 5);
* // [1, 2, 3, 4, 5]
*
* range(0, 4, 2);
* // [0, 2, 4]
* ```
*/
export function range(start: number, end: number, step = 1): number[] {
return Array.from(
{ length: (end - start) / step + 1 },
(_, i) => start + i * step,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment