import IFileExporter from '../core/IFileExporter' ; import BaseBuilder from '../core/BaseBuilder' ; export default class CsvBuilder extends BaseBuilder implements IFileExporter { private _FileName : string = '' ; private _Delimeter : string = ',' ; private _Columns : string [ ] = [ ] ; private _RowData : string [ ] [ ] = [ ] ; constructor ( fileName ) { super ( ) ; this . _FileName = fileName ; } public setColumns ( columns ) { this . _Columns = columns ; return this ; } public setDelimeter ( delimeter ) { this . _Delimeter = delimeter ; return this ; } public addRow ( row ) { this . _RowData . push ( row ) ; return this ; } public addRows ( rows ) { this . _RowData = [ ... this . _RowData , ... rows ] ; return this ; } private escapeCell ( cellData ) { if ( typeof cellData === 'string' ) { return '"' + cellData . replace ( /\"/g , '""' ) + '"' ; } return cellData ; } private getRowData ( row ) { return row . map ( this . escapeCell ) . join ( this . _Delimeter ) ; } public exportFile ( ) { let dataArray = [ ] ; if ( this . _Columns && this . _Columns . length > 0 ) { dataArray . push ( this . getRowData ( this . _Columns ) ) ; } this . _RowData . forEach ( row => { dataArray . push ( this . getRowData ( row ) ) ; } ) ; const csvContent = dataArray . join ( "\r\n" ) ; super . exportFile ( 'csv' , this . _FileName , csvContent ) ; } }