diff --git a/spec/iterator/map_iterator.spec.ts b/spec/iterator/map_iterator.spec.ts index eb6eacad19a4368759a4eab371e678f1c5b210bb..dbe5188d00f111ff475e89c65f5c175d9b061d46 100644 --- a/spec/iterator/map_iterator.spec.ts +++ b/spec/iterator/map_iterator.spec.ts @@ -3,7 +3,7 @@ import { MapIterator } from "../../src/util/iterator"; describe('param map iterator : ', () => { - it("sur undefined", () => { + it("sur undefined (1)", () => { const it: MapIterator<any> = new MapIterator(undefined); const v1 = it.next(); @@ -19,6 +19,15 @@ describe('param map iterator : ', () => { expect(it.index).toBeUndefined(); }); + it("sur undefined (2)", () => { + const it: MapIterator<any> = new MapIterator(undefined); + let n = 0; + for (let e of it) + n++; + + expect(n).toEqual(0); + }); + it("sur {}", () => { const it: MapIterator<any> = new MapIterator({}); @@ -35,12 +44,21 @@ describe('param map iterator : ', () => { expect(it.index).toBeUndefined(); }); - it("sur {'a':1}", () => { + it("sur {} (2)", () => { + const it: MapIterator<any> = new MapIterator({}); + let n = 0; + for (let e of it) + n++; + + expect(n).toEqual(0); + }); + + it("sur {'a':1} (1)", () => { const o = { "a": 1 }; const it: MapIterator<number> = new MapIterator(o); const v1 = it.next(); - expect(v1.done).toBeTruthy(); + expect(v1.done).toBeFalsy(); expect(v1.value).toEqual(1); expect(it.key).toEqual("a"); expect(it.index).toEqual(0); @@ -52,7 +70,19 @@ describe('param map iterator : ', () => { expect(it.index).toBeUndefined(); }); - it("sur {'a':1, 'b':2}", () => { + it("sur {'a':1} (2)", () => { + const o = { "a": 1 }; + const it: MapIterator<any> = new MapIterator(o); + let n = 0; + for (let e of it) { + n++; + expect(e).toEqual(n); + } + + expect(n).toEqual(1); + }); + + it("sur {'a':1, 'b':2} (1)", () => { const o = { "a": 1, "b": 2 }; const it: MapIterator<number> = new MapIterator(o); @@ -62,7 +92,7 @@ describe('param map iterator : ', () => { expect(it.key).toEqual("a"); const v2 = it.next(); - expect(v2.done).toBeTruthy(); + expect(v2.done).toBeFalsy(); expect(v2.value).toEqual(2); expect(it.key).toEqual("b"); @@ -72,4 +102,16 @@ describe('param map iterator : ', () => { expect(it.key).toBeUndefined(); expect(it.index).toBeUndefined(); }); + + it("sur {'a':1, 'b':2} (2)", () => { + const o = { 'a': 1, 'b': 2 }; + const it: MapIterator<any> = new MapIterator(o); + let n = 0; + for (let e of it) { + n++; + expect(e).toEqual(n); + } + + expect(n).toEqual(2); + }); }); diff --git a/spec/iterator/param_equation.spec.ts b/spec/iterator/param_equation.spec.ts index 5b6a26d3ceb96eb847fce8cbdbb7e5ce017b433f..3ae53d1be734414913dafdacfd4e3ec77127273a 100644 --- a/spec/iterator/param_equation.spec.ts +++ b/spec/iterator/param_equation.spec.ts @@ -1,7 +1,7 @@ /// <reference path="../../node_modules/@types/jasmine/index.d.ts" /> import { ConduiteDistribParams, ConduiteDistrib } from "../../src/cond_distri"; -import { ParamDefinition } from "../../src/index"; +import { ParamDefinition } from "../../src/param"; describe("iterator ConduiteDistribParams : ", () => { it("test 1", () => { @@ -20,14 +20,11 @@ describe("iterator ConduiteDistribParams : ", () => { const symbs = ["Q", "D", "J", "Lg", "Nu"]; let n = 0; - const it = cdp.iterator; - do { - var itnext = it.next(); - const p: ParamDefinition = itnext.value; + for (const p of cdp.iterator) { expect(p.v == n + 1); expect(p.symbol == symbs[n]); n++; - } while (!itnext.done) - expect(n === 5); + } + expect(n === 5).toBeTruthy(); }); }); diff --git a/src/util/iterator.ts b/src/util/iterator.ts index 72949ac86224eb8f27ab868f7940f00722365861..190813773fb9fe94a8acd78edd9221ddf80a50a9 100644 --- a/src/util/iterator.ts +++ b/src/util/iterator.ts @@ -47,10 +47,17 @@ export class ArrayReverseIterator<T> implements IterableIterator<T> { * 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) { + * ... + * } */ // tslint:disable-next-line:max-classes-per-file -export class MapIterator<T> implements Iterator<T> { - private _map: { [key: string]: T } = {}; +export class MapIterator<T> implements IterableIterator<T> { + private _map: { [key: string]: T }; private _keys: string[]; @@ -73,10 +80,9 @@ export class MapIterator<T> implements Iterator<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, + done: false, value: this._map[this._currentKey] }; } else { @@ -96,4 +102,10 @@ export class MapIterator<T> implements Iterator<T> { public get key(): string { return this._currentKey; } + + // interface IterableIterator + + [Symbol.iterator](): IterableIterator<T> { + return this; + } }