data-forge
Version:
JavaScript data transformation and analysis toolkit inspired by Pandas and LINQ.
39 lines • 1.38 kB
JavaScript
;
//
// An iterable that captures an iterator (e.g. from a generator function)
// and caches it to make it resusable
// (iterators or the output of a generator is generally not reusable).
//
Object.defineProperty(exports, "__esModule", { value: true });
var cached_iterator_1 = require("../iterators/cached-iterator");
var CachedIteratorIterable = /** @class */ (function () {
function CachedIteratorIterable(iterator) {
this.iterator = iterator;
this.cached = [];
}
CachedIteratorIterable.prototype[Symbol.iterator] = function () {
return new cached_iterator_1.CachedIterator(this);
};
//
// Gets from the cache or populates the cache.
//
CachedIteratorIterable.prototype._next = function (index) {
if (index >= this.cached.length) {
// Beyond the cache.
var result = this.iterator.next();
if (result.done) {
// Finished.
return { done: true }; // <= explicit cast here!;
}
// Cache result and return it.
this.cached.push(result.value);
}
return {
done: false,
value: this.cached[index],
};
};
return CachedIteratorIterable;
}());
exports.CachedIteratorIterable = CachedIteratorIterable;
//# sourceMappingURL=cached-iterator-iterable.js.map