UNPKG

geninq

Version:

A JavaScript version of the Linq library for Generators

45 lines (39 loc) 946 B
import { Build, Promised } from "./utils"; import "./count"; declare global { interface Generator<T = unknown, TReturn = any, TNext = unknown> { take_while( predicate: (item: T, index: number) => boolean ): Generator<T, TReturn, TNext>; } interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> { take_while( predicate: (item: T, index: number) => Promised<boolean> ): AsyncGenerator<T, TReturn, TNext>; } } Build( "take_while", function* (predicate) { let index = 0; let taking = true; for (const item of this) { if (taking && predicate(item, index)) { yield item; taking = false; } index++; } }, async function* (predicate) { let index = 0; let taking = true; for await (const item of this) { if (taking && predicate(item, index)) { yield item; taking = false; } index++; } } );