jalhyd
Version:
JaLHyd, a Javascript Library for Hydraulics
69 lines • 1.77 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MapIterator = void 0;
/**
* itérateur sur un map string<->any
*
* utilisation :
* class MaClasseAIterer implements Iterable<Machin> {
* private _machinMap: { [key: string]: Machin } = {};
* [Symbol.iterator](): Iterator<Machin> {
* return new MapIterator(this._machinMap);
* }
* }
* const mc = new MaClasseAIterer();
* for ( const m of mc ) {
* ...
* }
*
* avec IterableIterator au lieu de Iterator, on peut faire :
* const m = {"a":0, "b":1}
* const it = new MapIterator<number>();
* for (let e of it) {
* ...
* }
*/
class MapIterator {
constructor(m) {
this._lastIndex = -1; // index of last element returned; -1 if no such
this._index = 0;
this._map = m;
if (this._map !== undefined) {
this._keys = Object.keys(this._map);
}
else {
this._keys = [];
}
}
next() {
const i = this._index;
if (this._index < this._keys.length) {
this._currentKey = this._keys[this._index++];
this._lastIndex = i;
return {
done: false,
value: this._map[this._currentKey]
};
}
else {
this._currentKey = undefined;
this._lastIndex = undefined;
return {
done: true,
value: undefined
};
}
}
get index() {
return this._lastIndex;
}
get key() {
return this._currentKey;
}
// interface IterableIterator
[Symbol.iterator]() {
return this;
}
}
exports.MapIterator = MapIterator;
//# sourceMappingURL=map_iterator.js.map