From bad497131288cded5c0456462635e06d74b7cd70 Mon Sep 17 00:00:00 2001 From: "francois.grand" <francois.grand@irstea.fr> Date: Thu, 8 Feb 2018 08:12:49 +0100 Subject: [PATCH] =?UTF-8?q?ticket=20#36=20:=20ajout=20d'un=20iterateur=20d?= =?UTF-8?q?e=20tableau=20commen=C3=A7ant=20par=20la=20fin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/iterator.spec.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/index.ts | 1 + src/util/iterator.ts | 25 +++++++++++++++++++++++++ tsconfig.json | 4 ++-- 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 spec/iterator.spec.ts create mode 100644 src/util/iterator.ts diff --git a/spec/iterator.spec.ts b/spec/iterator.spec.ts new file mode 100644 index 00000000..68dce8db --- /dev/null +++ b/spec/iterator.spec.ts @@ -0,0 +1,40 @@ +/// <reference path="../node_modules/@types/jasmine/index.d.ts" /> + +import { ArrayReverseIterator } from "../src/util/iterator"; + +describe('iterator : ', () => { + it("reverse ( undefined )", () => { + const arr: Object[] = undefined; + const it = new ArrayReverseIterator<Object>(arr); + expect(it.next().done).toBeTruthy(); + }); + + it("reverse( [] )", () => { + const arr: Object[] = []; + const it = new ArrayReverseIterator<Object>(arr); + expect(it.next().done).toBeTruthy(); + }); + + it("reverse( [1] )", () => { + const arr: number[] = [1]; + const it = new ArrayReverseIterator<number>(arr); + const v1 = it.next(); + expect(v1.done).toBeFalsy(); + expect(v1.value).toEqual(1); + const v2 = it.next(); + expect(v2.done).toBeTruthy(); + }); + + it("reverse( [1,2] )", () => { + const arr: number[] = [1, 2]; + const it = new ArrayReverseIterator<number>(arr); + const v1 = it.next(); + expect(v1.done).toBeFalsy(); + expect(v1.value).toEqual(2); + const v2 = it.next(); + expect(v2.done).toBeFalsy(); + expect(v2.value).toEqual(1); + const v3 = it.next(); + expect(v3.done).toBeTruthy(); + }); +}); diff --git a/src/index.ts b/src/index.ts index 3443109f..a939be42 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,3 +22,4 @@ export * from "./util/pair"; export * from "./util/interval"; export * from "./pab/pab_dimension"; export * from "./pab/pab_puissance"; +export * from "./util/iterator"; diff --git a/src/util/iterator.ts b/src/util/iterator.ts new file mode 100644 index 00000000..71a7cfa8 --- /dev/null +++ b/src/util/iterator.ts @@ -0,0 +1,25 @@ +export class ArrayReverseIterator<T> implements IterableIterator<T> { + private _index: number; + + constructor(private _arr: any[]) { + this._index = this._arr == undefined ? 0 : this._arr.length - 1; + } + + public next(): IteratorResult<T> { + if (this._arr != undefined && this._index >= 0) { + return { + done: false, + value: this._arr[this._index--] + } + } else { + return { + done: true, + value: null + } + } + } + + [Symbol.iterator](): IterableIterator<T> { + return this; + } +} diff --git a/tsconfig.json b/tsconfig.json index e5b05034..c345304b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "es5", + "target": "es6", "module": "commonjs", "outDir": "./build", "noImplicitAny": true, @@ -12,4 +12,4 @@ "exclude": [ "node_modules" ] -} +} \ No newline at end of file -- GitLab