Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions iterator/ts/aggregator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {Iterator} from "./iterator";

export interface Aggregator {
// Get an external iterator.
getIterator(): Iterator<any>;
}
33 changes: 33 additions & 0 deletions iterator/ts/collection.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
return new CollectionIterator(this);
}

}
45 changes: 45 additions & 0 deletions iterator/ts/collectionIterator.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
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();
}
}
18 changes: 18 additions & 0 deletions iterator/ts/index.ts
Original file line number Diff line number Diff line change
@@ -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());
}
16 changes: 16 additions & 0 deletions iterator/ts/iterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export interface Iterator<T> {
// 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;
}