-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use generator for range utility (#8825)
* chore: use generator for range utility * chore: update doc examples * chore: fix spelling * chore: fix typo Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
a6941d5
commit 3160ab0
Showing
3 changed files
with
60 additions
and
14 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,61 @@ | ||
/** | ||
* Yields the numbers in the given range as an array | ||
* Options for creating a range | ||
*/ | ||
export interface RangeOptions { | ||
/** | ||
* The end of the range (exclusive) | ||
*/ | ||
end: number; | ||
/** | ||
* The start of the range (inclusive) | ||
*/ | ||
start: number; | ||
/** | ||
* The amount to increment by | ||
* | ||
* @defaultValue `1` | ||
*/ | ||
step?: number; | ||
} | ||
|
||
/** | ||
* A generator to yield numbers in a given range | ||
* | ||
* @param start - The start of the range | ||
* @param end - The end of the range (inclusive) | ||
* @param step - The amount to increment between each number | ||
* @remarks | ||
* This method is end-exclusive, for example the last number yielded by `range(5)` is 4. If you | ||
* prefer for the end to be included add 1 to the range or `end` option. | ||
* @param range - A number representing the the range to yield (exclusive) or an object with start, end and step | ||
* @example | ||
* Basic range | ||
* ```ts | ||
* range(3, 5); // [3, 4, 5] | ||
* for (const number of range(5)) { | ||
* console.log(number); | ||
* } | ||
* // Prints 0, 1, 2, 3, 4 | ||
* ``` | ||
* @example | ||
* Range with a step | ||
* ```ts | ||
* range(3, 10, 2); // [3, 5, 7, 9] | ||
* for (const number of range({ start: 3, end: 10, step: 2 })) { | ||
* console.log(number); | ||
* } | ||
* // Prints 3, 5, 7, 9 | ||
* ``` | ||
*/ | ||
export function range(start: number, end: number, step = 1): number[] { | ||
return Array.from({ length: (end - start) / step + 1 }, (_, index) => start + index * step); | ||
export function* range(range: RangeOptions | number) { | ||
let rangeEnd: number; | ||
let start = 0; | ||
let step = 1; | ||
|
||
if (typeof range === 'number') { | ||
rangeEnd = range; | ||
} else { | ||
start = range.start; | ||
rangeEnd = range.end; | ||
step = range.step ?? 1; | ||
} | ||
|
||
for (let index = start; index < rangeEnd; index += step) { | ||
yield index; | ||
} | ||
} |
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