From 6794d34e21302b2e102f67240434fc22cd943c49 Mon Sep 17 00:00:00 2001
From: "francois.grand" <francois.grand@irstea.fr>
Date: Fri, 9 Feb 2018 17:23:58 +0100
Subject: [PATCH] =?UTF-8?q?=20#36=20:=20am=C3=A9lioration=20de=20MapIterat?=
 =?UTF-8?q?or,=20ajout=20d'un=20getter=20=5Fiterator=5F=20=C3=A0=20ParamsE?=
 =?UTF-8?q?quation?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 spec/iterator/map_iterator.spec.ts   | 28 +++++++++++++++++++++++++++-
 spec/iterator/param_equation.spec.ts |  3 +--
 src/param.ts                         |  4 ++++
 src/util/iterator.ts                 | 11 +++++++++--
 4 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/spec/iterator/map_iterator.spec.ts b/spec/iterator/map_iterator.spec.ts
index 7bbcef4e..eb6eacad 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 4e4fb7ed..5b6a26d3 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 5b615e99..77a641c1 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 f7f27b52..31a4b942 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 {
-- 
GitLab