import { BehaviorError } from './ErrorModels' ; import { IterableProtocol } from './types' ; export class PrimitiveIterator implements IterableProtocol { value : number | string | Function ; done : boolean = false ; constructor ( value ) { this . value = value ; } [ Symbol . iterator ] ( ) { return this ; } public next ( value ? ) { if ( this . done ) { return { value : undefined , done : true } ; } this . done = true ; return { value : this . value , done : false } ; } public clone ( ) { return new PrimitiveIterator ( this . value ) ; } } export class ArrayIterator implements IterableProtocol { arr : Array < any > ; arrIterator : Iterator < any > ; constructor ( arr ) { this . arr = arr ; this . arrIterator = arr [ Symbol . iterator ] ( ) ; } [ Symbol . iterator ] ( ) { return this ; } public next ( value ? ) { return this . arrIterator . next ( ) ; } public clone ( ) { return new ArrayIterator ( this . arr ) ; } } export class RepeatIterator implements IterableProtocol { static FOREVER : number = - 1 ; iterator : IterableProtocol ; repeatLimit : number ; repeatCount : number = 1 ; constructor ( iterator , repeatLimit ) { this . iterator = iterator ; this . repeatLimit = repeatLimit ; } [ Symbol . iterator ] ( ) { return this ; } public next ( value ? ) { let next = this . iterator . next ( ) ; if ( ! next . done ) { return next ; } if ( this . repeatLimit !== RepeatIterator . FOREVER && ! ( this . repeatCount < this . repeatLimit ) ) { return { value : undefined , done : true } ; } this . iterator = this . iterator . clone ( ) ; next = this . iterator . next ( ) ; if ( next . done ) { throw new BehaviorError ( 'Iterator should iterable more than once.' ) ; } this . repeatCount ++ ; return next ; } public clone ( ) { return new RepeatIterator ( this . iterator , this . repeatLimit ) ; } } export class RangeIterator implements IterableProtocol { start : number ; end : number ; step : Function ; current : number ; constructor ( start , end , step ) { this . start = start ; this . end = end ; this . step = step ; this . current = start ; } [ Symbol . iterator ] ( ) { return this ; } public next ( value ? ) { if ( this . hasNext ( ) ) { let value = this . current ; this . current = this . step ( this . current ) ; return { value : value , done : false } ; } return { value : undefined , done : true } ; } hasNext ( ) { if ( this . start < this . end ) { return this . current < this . end ; } else { return this . current > this . end ; } } public clone ( ) { return new RangeIterator ( this . start , this . end , this . step ) ; } }