iteragain
Version:
Javascript Iterable/Iterator/Generator-function utilities.
27 lines • 995 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FunctionIterator = void 0;
/**
* 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.
*/
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 };
}
}
exports.FunctionIterator = FunctionIterator;
exports.default = FunctionIterator;
//# sourceMappingURL=FunctionIterator.js.map