UNPKG

iteragain

Version:

Javascript Iterable/Iterator/Generator-function utilities.

30 lines 1.19 kB
/** * 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. */ var FunctionIterator = /** @class */ (function () { function FunctionIterator(func, sentinel) { this.func = func; this.sentinel = sentinel; } FunctionIterator.prototype[Symbol.iterator] = function () { return this; }; /** Works like any generator function `next` method */ FunctionIterator.prototype.next = function () { var _this = this; var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var result = this.func.apply(this, args); return result === this.sentinel ? ((this.func = (function () { return _this.sentinel; })), { done: true, value: undefined }) : { done: false, value: result }; }; return FunctionIterator; }()); export { FunctionIterator }; export default FunctionIterator; //# sourceMappingURL=FunctionIterator.js.map