data-forge
Version:
JavaScript data transformation and analysis toolkit inspired by Pandas and LINQ.
47 lines • 1.93 kB
JavaScript
"use strict";
//
// Iterates an underlying iterable in the 'windows'.
//
Object.defineProperty(exports, "__esModule", { value: true });
var series_1 = require("../series");
var SeriesRollingWindowIterator = /** @class */ (function () {
function SeriesRollingWindowIterator(iterable, period, whichIndex) {
this.iterable = iterable;
this.period = period;
this.whichIndex = whichIndex;
}
SeriesRollingWindowIterator.prototype.next = function () {
if (!this.curWindow) {
this.curWindow = [];
this.iterator = this.iterable[Symbol.iterator]();
for (var i = 0; i < this.period; ++i) {
var curPos = this.iterator.next();
if (curPos.done) {
// Underlying iterator doesn't have required number of elements.
return { done: true };
}
this.curWindow.push(curPos.value);
}
}
else {
this.curWindow.shift(); // Remove first item from window.
var curPos = this.iterator.next();
if (curPos.done) {
// Underlying iterator doesn't have enough elements left.
return { done: true };
}
this.curWindow.push(curPos.value); // Add next item to window.
}
var window = new series_1.Series({
pairs: this.curWindow
});
return {
//TODO: The way the index is figured out could have much better performance.
value: [this.whichIndex === series_1.WhichIndex.Start ? window.getIndex().first() : window.getIndex().last(), window],
done: false,
};
};
return SeriesRollingWindowIterator;
}());
exports.SeriesRollingWindowIterator = SeriesRollingWindowIterator;
//# sourceMappingURL=series-rolling-window-iterator.js.map