@gabelluardo/itertools@0.3.0Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
latest
gabelluardo/itertoolsitertools for Deno 🦕
This package works with Deno, BrowsersIt is unknown whether this package works with Cloudflare Workers, Node.js, Bun




JSR Score
100%
Published
4 months ago (0.3.0)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100/** * Creates an iterator that yields the same object repeatedly. * * @description * Returns a generator that yields the same object either indefinitely or * a specified number of times. If times is specified, the generator will * yield exactly that many times before completing. * * @example * ```ts * import { assertEquals } from "@std/assert"; * * const repeater = repeat('x', 3); * assertEquals(repeater.next().value, 'x'); * assertEquals(repeater.next().value, 'x'); * assertEquals(repeater.next().value, 'x'); * assertEquals(repeater.next().done, true); * ``` * * @param object - The object to repeat * @param times - Optional number of times to repeat */ export function* repeat<T>(object: T, times?: number): Generator<T> { if (times === undefined) { while (true) { yield object; } } else { for (let i = 0; i < times; i++) { yield object; } } } /** * Creates an iterator that returns evenly spaced values. * * @description * Generates an arithmetic sequence starting from the given number, * incrementing by the specified step size. The sequence continues * indefinitely. * * @example * ```ts * import { assertEquals } from "@std/assert"; * * const counter = count(10, 2); * assertEquals(counter.next().value, 10); * assertEquals(counter.next().value, 12); * assertEquals(counter.next().value, 14); * ``` * * @param start - Starting number (defaults to 0) * @param step - Step size (defaults to 1) */ export function* count(start = 0, step = 1): Generator<number> { let n = start; while (true) { yield n; n += step; } } /** * Creates an iterator that cycles through elements from an iterable. * * @description * Saves elements from the input iterable and cycles through them indefinitely. * Elements are yielded in the same order as they appear in the input. * * @example * ```ts * import { assertEquals } from "@std/assert"; * * const cycler = cycle(['a', 'b', 'c']); * assertEquals(cycler.next().value, 'a'); * assertEquals(cycler.next().value, 'b'); * assertEquals(cycler.next().value, 'c'); * assertEquals(cycler.next().value, 'a'); * ``` * * @param iterable - Input iterable to cycle through */ export function* cycle<T>(iterable: Iterable<T>): Generator<T> { const saved = []; for (const element of iterable) { yield element; saved.push(element); } if (saved.length === 0) { return; } while (saved) { for (const element of saved) { yield element; } } }