Created
December 11, 2023 09:46
-
-
Save mmyoji/b1ec5f4af5ad82eea8505785f68390c4 to your computer and use it in GitHub Desktop.
`range()` function for TypeScript
This file contains 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
/** | |
* 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