All files / iterable/src generate.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 1/1
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25                                      1x 1x 4x      
import { Provider } from "@benneq/function";
 
/**
 * Creates an {@link Iterable} that emits an infinite sequence of values
 * generated by a {@link Provider} function.
 *
 * @example
 * ```ts
 * let counter = 0;
 * const numbers = generate(() => i++);
 * const [value1, value2] = numbers;
 * console.log(value1, value2); // 0, 1
 * ```
 *
 * @see {@link iterate} for similar operations.
 *
 * @typeParam T - the {@link Iterable} value type
 * @param provider - a {@link Function} that produces values
 */
export function* generate<T>(provider: Provider<T>): IterableIterator<T> {
  while (true) {
    yield provider();
  }
}