UNPKG

iteragain

Version:

Javascript Iterable/Iterator/Generator-function utilities.

23 lines 842 B
/** * Creates a iterator from any function. `func` will be called once per `next()` call and will stop once the value of * `sentinel` (default: undefined) is returned from `func`. Behaves similarly to a generator function, except without * the use of `yield` statements. */ export class FunctionIterator { constructor(func, sentinel) { this.func = func; this.sentinel = sentinel; } [Symbol.iterator]() { return this; } /** Works like any generator function `next` method */ next(...args) { const result = this.func(...args); return result === this.sentinel ? ((this.func = (() => this.sentinel)), { done: true, value: undefined }) : { done: false, value: result }; } } export default FunctionIterator; //# sourceMappingURL=FunctionIterator.js.map