/**
 * Optimized tokenizer of a single CSV line.
 */ export class LineSplitter { /** returned value when reused  */ reusedValues : string [ ] ; /** last length of elements in */ lastSplitLength : number ; private _reuse = false ; /**
     * Reuse returned array between consecutive calls.
     */ get reuse ( ) : boolean { return this . _reuse ; } set reuse ( val ) { if ( val && ! this . reusedValues ) { this . reusedValues = new Array ( 10 ) ; } this . _reuse = val ; } /**
     * Sets the reuse flag and returns this.
     */ withReuse ( ) { this . reuse = true ; return this ; } param line returns splitLine ( line ) { if ( line === null || line === undefined ) { this . lastSplitLength = 0 ; return [ ] ; } let quoteCount = 0 ; let startIndex = 0 ; const values = this . _reuse ? this . reusedValues : [ ] ; let count = 0 ; for ( let i = 0 ; i < line . length ; i ++ ) { const c = line [ i ] ; if ( c === ',' ) { if ( quoteCount % 2 === 0 ) { const val = this . getValue ( line , startIndex , i , quoteCount ) ; if ( this . _reuse ) { values [ count ++ ] = val ; } else { values . push ( val ) ; } startIndex = i + 1 ; quoteCount = 0 ; } } else if ( c === '"' ) { quoteCount ++ ; } } const val = this . getValue ( line , startIndex , line . length , quoteCount ) ; if ( this . _reuse ) { values [ count ] = val ; this . lastSplitLength = count + 1 ; } else { values . push ( val ) ; this . lastSplitLength = values . length ; } return values ; } private getValue ( line , start , end , quoteCount ) { if ( start === line . length ) { return '' ; } else if ( quoteCount === 0 ) { return line . substring ( start , end ) ; } else if ( quoteCount === 2 ) { return line . substring ( start + 1 , end - 1 ) ; } else { return line . substring ( start + 1 , end - 1 ) . replace ( /""/gi , '"' ) ; } } }