diff --git a/spec/iterator/map_iterator.spec.ts b/spec/iterator/map_iterator.spec.ts index 7bbcef4ee77bb2ec85473e1c478771e6d107c17c..eb6eacad19a4368759a4eab371e678f1c5b210bb 100644 --- a/spec/iterator/map_iterator.spec.ts +++ b/spec/iterator/map_iterator.spec.ts @@ -5,15 +5,34 @@ import { MapIterator } from "../../src/util/iterator"; describe('param map iterator : ', () => { it("sur undefined", () => { const it: MapIterator<any> = new MapIterator(undefined); + const v1 = it.next(); expect(v1.done).toBeTruthy(); + expect(v1.value).toBeUndefined(); + expect(it.key).toBeUndefined(); + expect(it.index).toBeUndefined(); + + const v2 = it.next(); + expect(v2.done).toBeTruthy(); + expect(v2.value).toBeUndefined(); + expect(it.key).toBeUndefined(); + expect(it.index).toBeUndefined(); }); it("sur {}", () => { const it: MapIterator<any> = new MapIterator({}); + const v1 = it.next(); expect(v1.done).toBeTruthy(); - expect(it.index).toEqual(0); + expect(v1.value).toBeUndefined(); + expect(it.key).toBeUndefined(); + expect(it.index).toBeUndefined(); + + const v2 = it.next(); + expect(v2.done).toBeTruthy(); + expect(v2.value).toBeUndefined(); + expect(it.key).toBeUndefined(); + expect(it.index).toBeUndefined(); }); it("sur {'a':1}", () => { @@ -24,9 +43,13 @@ describe('param map iterator : ', () => { expect(v1.done).toBeTruthy(); expect(v1.value).toEqual(1); expect(it.key).toEqual("a"); + expect(it.index).toEqual(0); const v2 = it.next(); expect(v2.done).toBeTruthy(); + expect(v2.value).toBeUndefined(); + expect(it.key).toBeUndefined(); + expect(it.index).toBeUndefined(); }); it("sur {'a':1, 'b':2}", () => { @@ -45,5 +68,8 @@ describe('param map iterator : ', () => { const v3 = it.next(); expect(v3.done).toBeTruthy(); + expect(v3.value).toBeUndefined(); + expect(it.key).toBeUndefined(); + expect(it.index).toBeUndefined(); }); }); diff --git a/spec/iterator/param_equation.spec.ts b/spec/iterator/param_equation.spec.ts index 4e4fb7ed33595f67cd31646e31338bf9ed5f495b..5b6a26d3ceb96eb847fce8cbdbb7e5ce017b433f 100644 --- a/spec/iterator/param_equation.spec.ts +++ b/spec/iterator/param_equation.spec.ts @@ -1,6 +1,5 @@ /// <reference path="../../node_modules/@types/jasmine/index.d.ts" /> -//import { MapIterator } from "../../src/util/iterator"; import { ConduiteDistribParams, ConduiteDistrib } from "../../src/cond_distri"; import { ParamDefinition } from "../../src/index"; @@ -21,7 +20,7 @@ describe("iterator ConduiteDistribParams : ", () => { const symbs = ["Q", "D", "J", "Lg", "Nu"]; let n = 0; - const it = cdp[Symbol.iterator](); + const it = cdp.iterator; do { var itnext = it.next(); const p: ParamDefinition = itnext.value; diff --git a/src/param.ts b/src/param.ts index 5b615e9969619421d7ad1c926caf473b11a37606..77a641c1fa70aa771cfdc1990efbd4b8be9e866e 100644 --- a/src/param.ts +++ b/src/param.ts @@ -436,6 +436,10 @@ export abstract class ParamsEquation implements Iterable<ParamDefinition> { } [Symbol.iterator](): Iterator<ParamDefinition> { + return this.iterator; + } + + public get iterator() { return new MapIterator(this._paramMap); } } diff --git a/src/util/iterator.ts b/src/util/iterator.ts index f7f27b52ea284f25f42de972552570dcfb74265b..31a4b942c704ffb378a44806a7009a02982d4272 100644 --- a/src/util/iterator.ts +++ b/src/util/iterator.ts @@ -56,6 +56,8 @@ export class MapIterator<T> implements Iterator<T> { private _keys: string[]; + private _lastIndex: number = -1; // index of last element returned; -1 if no such + private _index: number = 0; private _currentKey: string; @@ -69,22 +71,27 @@ export class MapIterator<T> implements Iterator<T> { } public next(): IteratorResult<T> { + const i = this._index; if (this._index < this._keys.length) { this._currentKey = this._keys[this._index++]; + this._index = i + 1; + this._lastIndex = i; return { done: this._index >= this._keys.length, value: this._map[this._currentKey] } } else { + this._currentKey = undefined; + this._lastIndex = undefined; return { done: true, - value: null + value: undefined } } } public get index() { - return this._index; + return this._lastIndex; } public get key(): string {