@lou.codes/iterables
Version:
🔁 Iterable and AsyncIterable utils
26 lines (25 loc) • 731 B
JavaScript
import { createIterableIterator } from "./createIterableIterator.js";
/**
* Range iterable generator (from `from` to `to`).
*
* @category Generators
* @example
* ```typescript
* [...range(1)(0)(5)]; // [0, 1, 2, 3, 4, 5]
* [...range(1)(5)(0)]; // [5, 4, 3, 2, 1, 0]
* ```
* @param step Step size.
* @returns Curried function with `step` set in context.
*/
export const range = step => from => to =>
createIterableIterator(function* () {
// eslint-disable-next-line functional/no-loop-statements
for (
let current = from;
from < to ? current <= to : current >= to;
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
current += from < to ? step : -step
) {
yield current;
}
});