import { assertExists } from '@ulangi/assert' ; import { Vocabulary } from '@ulangi/ulangi-common/interfaces' ; import { shuffleImmutableMap } from '@ulangi/ulangi-common/utils' ; import { OrderedMap } from 'immutable' ; export class VocabularyIterator < T extends Vocabulary = Vocabulary > { private vocabularyList : OrderedMap < string , T > ; private currentIndex : number = 0 ; public constructor ( vocabularyList ? ) { this . vocabularyList = OrderedMap ( vocabularyList || [ ] ) ; } public next ( ) { this . currentIndex += 1 ; return this . current ( ) ; } public previous ( ) { this . currentIndex -= 1 ; return this . current ( ) ; } public current ( ) { return assertExists ( this . vocabularyList . valueSeq ( ) . get ( this . currentIndex || 0 ) , 'index out of bound' ) ; } public reset ( ) { this . currentIndex = 0 ; } public isDone ( ) { return this . currentIndex === this . vocabularyList . size - 1 ; } public getSize ( ) { return this . vocabularyList . size ; } public getRemainingSize ( ) { return this . vocabularyList . size - this . currentIndex - 1 ; } public update ( key , value ) { this . vocabularyList = this . vocabularyList . set ( key , value ) ; } public merge ( vocabularyList ) { this . vocabularyList = this . vocabularyList . merge ( vocabularyList ) ; } public shuffle ( ) { this . vocabularyList = shuffleImmutableMap ( this . vocabularyList ) ; } }