license import { LazyIterator , OneToManyIterator } from './lazy_iterator' ; export abstract class StringIterator extends LazyIterator < string > { param upstream param separator split ( separator ) { return new SplitIterator ( this , separator ) ; } } class SplitIterator extends StringIterator { private impl : SplitIteratorImpl ; constructor ( protected upstream , separator ) { super ( ) ; this . impl = new SplitIteratorImpl ( upstream , separator ) ; } summary ( ) { return this . impl . summary ( ) ; } async next ( ) { return this . impl . next ( ) ; } } class SplitIteratorImpl extends OneToManyIterator < string > { carryover = '' ; constructor ( protected upstream , protected separator ) { super ( ) ; } summary ( ) { return `${ this . upstream . summary ( ) } -> Split('${ this . separator }')` ; } async pump ( ) { const chunkResult = await this . upstream . next ( ) ; if ( chunkResult . done ) { if ( this . carryover === '' ) { return false ; } this . outputQueue . push ( this . carryover ) ; this . carryover = '' ; return true ; } const lines = chunkResult . value . split ( this . separator ) ; lines [ 0 ] = this . carryover + lines [ 0 ] ; for ( const line of lines . slice ( 0 , - 1 ) ) { this . outputQueue . push ( line ) ; } this . carryover = lines [ lines . length - 1 ] ; return true ; } }