UNPKG

loop-controls

Version:

break/continue controls for loops and higher-order functions (sync, async, concurrent). Designed to compose with ts-pattern.

60 lines 1.59 kB
export function range(start, end, step = 1) { // Handle single argument case: range(10) means range(0, 10) if (end === undefined) { end = start; start = 0; } return { *[Symbol.iterator]() { if (step > 0) { for (let i = start; i < end; i += step) { yield i; } } else if (step < 0) { for (let i = start; i > end; i += step) { yield i; } } // If step === 0, yield nothing (infinite loop prevention) } }; } /** * Creates a lazy iterable that repeats a value * @param value - Value to repeat * @param count - Number of times to repeat (infinite if not provided) */ export function repeat(value, count) { return { *[Symbol.iterator]() { if (count === undefined) { while (true) { yield value; } } else { for (let i = 0; i < count; i++) { yield value; } } } }; } /** * Creates a lazy iterable that counts up from a starting value * @param start - Starting value (default: 0) * @param step - Step size (default: 1) */ export function count(start = 0, step = 1) { return { *[Symbol.iterator]() { let current = start; while (true) { yield current; current += step; } } }; } //# sourceMappingURL=iterables.js.map