UNPKG

jalhyd

Version:

JaLHyd, a Javascript Library for Hydraulics

76 lines 2.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MirrorIterator = void 0; /** * Itérateur miroir sur les (ou la) valeurs prises par un ParamValues. * Différent d'un ParamValuesIterator avec reverse = true. * * ex: min = 0, max = 10, step = 3 * ParamValuesIterator: [ 0, 3, 6, 9, 10 ] * ParamValuesIterator reverse: [ 10, 7, 4, 1, 0 ] * MirrorIterator: [ 10, 9, 6, 3, 0 ] */ class MirrorIterator { constructor(prm) { prm.check(); this._param = prm; this.reset(); } get currentValue() { return this._current; } /** * Returns the number of elements in the iterator without altering it, * by counting a copy of the iterator */ count() { return this._param.valuesIterator.count(); } /** * Reinits the value iterator */ reset() { this._index = 0; this._current = undefined; // get original values list this._valuesList = []; const it = this._param.valuesIterator; while (it.hasNext) { this._valuesList.push(it.next().value); } // mirror it this._valuesList.reverse(); } next() { if (this.hasNext) { this._current = this._valuesList[this._index]; this._index++; return { done: false, value: this._current }; } else { // end of iterator return { done: true, value: undefined }; } } /** trick method - use .next() instead, unless you are explicitely in jalhyd#222 case */ nextValue() { return this.next(); } // interface IterableIterator [Symbol.iterator]() { return this; } /** * Returns true if iterator has at least one more value */ get hasNext() { return this._index < this._valuesList.length; } } exports.MirrorIterator = MirrorIterator; //# sourceMappingURL=mirror-iterator.js.map