diff --git a/iterator/ts/aggregator.ts b/iterator/ts/aggregator.ts new file mode 100644 index 0000000..c1d371b --- /dev/null +++ b/iterator/ts/aggregator.ts @@ -0,0 +1,6 @@ +import {Iterator} from "./iterator"; + +export interface Aggregator { + // Get an external iterator. + getIterator(): Iterator; +} \ No newline at end of file diff --git a/iterator/ts/collection.ts b/iterator/ts/collection.ts new file mode 100644 index 0000000..5d1306f --- /dev/null +++ b/iterator/ts/collection.ts @@ -0,0 +1,33 @@ +import {Aggregator} from "./aggregator"; +import {CollectionIterator} from "./collectionIterator"; +import {Iterator} from "./iterator"; + +/** + * Concrete Collections provide one or more methods to get + * new iterator instances compatible with the collection class. + */ +export class Collection implements Aggregator { + + private items: any[] = []; + + //Get all items + public getItems(): any[] { + return this.items; + } + + //Get length + public getCount(): number { + return this.items.length; + } + + //Add item to collection + public addItem(item: any): void { + this.items.push(item); + } + + //Get iterator + public getIterator(): Iterator { + return new CollectionIterator(this); + } + +} \ No newline at end of file diff --git a/iterator/ts/collectionIterator.ts b/iterator/ts/collectionIterator.ts new file mode 100644 index 0000000..3821f95 --- /dev/null +++ b/iterator/ts/collectionIterator.ts @@ -0,0 +1,45 @@ +import {Collection} from "./collection"; +import {Iterator} from "./iterator"; + +/** + * Concrete Iterators implement different traversal algorithms. These classes + * permanently store the current bypass position. + */ +export class CollectionIterator implements Iterator { + private collection: Collection; + + + //Stores the current bypass position + private position: number = 0; + + constructor(collection: Collection) { + this.collection = collection; + } + + //Updates the position + public rewind() { + this.position = 0; + } + + //Return current position + public current(): any { + return this.collection.getItems()[this.position]; + } + + //Return key of position + public key(): number { + return this.position; + } + + //Return next item + public next(): any { + const item = this.collection.getItems()[this.position]; + this.position += 1; + return item; + } + + //Check for next item + public hasNext(): boolean { + return this.position < this.collection.getCount(); + } +} \ No newline at end of file diff --git a/iterator/ts/index.ts b/iterator/ts/index.ts new file mode 100644 index 0000000..2e38356 --- /dev/null +++ b/iterator/ts/index.ts @@ -0,0 +1,18 @@ +import {Collection} from "./collection"; + +/** + * Iterator pattern + * + * Purpose: Allows you to sequentially bypass the elements of the composite + * objects without revealing their internal representation. + */ +const collection = new Collection(); +collection.addItem(3); +collection.addItem('String'); +collection.addItem([4, 6]); + +const iterator = collection.getIterator(); + +while (iterator.hasNext()) { + console.log(iterator.next()); +} diff --git a/iterator/ts/iterator.ts b/iterator/ts/iterator.ts new file mode 100644 index 0000000..87fb9d2 --- /dev/null +++ b/iterator/ts/iterator.ts @@ -0,0 +1,16 @@ +export interface Iterator { + // Returns the current item + current(): T; + + // Returns the current item and moves to the next item + next(): T; + + // Returns the key of the current item + key(): number; + + // Checks the correctness of the current position + hasNext(): boolean; + + // Rewind the Iterator to the first item + rewind(): void; +} \ No newline at end of file