file import * as csv_parser from "csv-parse" ; import * as fs from "fs" ; import { CSVRow } from "./../../common/Objects/CSVRow" ; import { CSVConf , HTAccessConf } from "./../../culturize.conf" ; /**
 * This is a function type used by createArrayFromCSV which is
 * called when the function accepts a row and pushes it to the resulting array.
 */ type OnAcceptRow = ( row : CSVRow ) => void ; /**
 * This is a function type used by createArrayFromCSV which is
 * called when the function rejects a row (insufficiant/incompatible data)
 */ type OnRejectRow = ( row : CSVRow ) => void ; static returns param { string } filepath param { OnAcceptRow } rowAccept param { OnRejectRow } rowReject export async function createArrayFromCSV ( filepath , rowAccept , rowReject , allowDuplicates = false ) { return new Promise < CSVRow [ ] > ( ( resolve , reject ) => { const str = fs . readFileSync ( filepath , "utf8" ) . replace ( "\r\n" , "\n" ) ; const array = new Array < CSVRow > ( ) ; if ( str . length === 0 ) { reject ( "The file is empty" ) ; return ; } const parser = csv_parser ( { delimiter : [ "," , ";" ] , columns : true , relax_column_count : true , } ) ; parser . on ( "readable" , ( ) => { let data ; data = parser . read ( ) ; while ( data ) { const row = CSVRow . createRow ( data ) ; if ( row != null ) { if ( ! row . isValidAndEnabled ( ) ) { rowReject ( row ) ; if ( ! CSVConf . IGNORE_ON_INVALID_DATA ) { reject ( row . error ) ; break ; } } else { rowAccept ( row ) ; } array . push ( row ) ; } else { rowReject ( row ) ; } data = parser . read ( ) ; } } ) ; parser . on ( "error" , ( err ) => { reject ( "Error while parsing the CSV file" ) ; } ) ; parser . on ( "end" , ( ) => { if ( array . length === 0 ) { reject ( "No valid row found in the CSV File." ) ; return ; } else { if ( checkArrayForDuplicates ( array ) ) { if ( allowDuplicates ) { resolve ( array ) ; } else { reject ( "Duplicates found, validate CSV first!" ) ; } } else { resolve ( array ) ; } } } ) ; CSVRow . count = 2 ; parser . write ( str ) ; parser . end ( ) ; } ) ; } static returns param { CSVRow [ ] } array function checkArrayForDuplicates ( array ) { class Row { public name : string ; public csvRow : CSVRow ; constructor ( row ) { this . name = row . docType + "/" + row . pid ; this . csvRow = row ; } } const redirs = new Array < Row > ( ) ; for ( const element of array ) { if ( ! element . isValidAndEnabled ( ) ) { continue ; } const row = new Row ( element ) ; if ( HTAccessConf . caseInsensitiveRedirs ) { row . name = row . name . toUpperCase ( ) ; } redirs . push ( row ) ; } function compare ( firstEl , secondEl ) { if ( firstEl . name < secondEl . name ) { return - 1 ; } else if ( firstEl . name === secondEl . name ) { return 0 ; } else { return 1 ; } } redirs . sort ( compare ) ; const duplicates = new Array < Row > ( ) ; const redirsLength = redirs . length ; for ( let k = 0 ; k < redirsLength - 1 ; k ++ ) { if ( redirs [ k ] . name === redirs [ k + 1 ] . name ) { redirs [ k ] . csvRow . markAsDuplicateOf ( redirs [ k + 1 ] . csvRow . index ) ; redirs [ k + 1 ] . csvRow . markAsDuplicateOf ( redirs [ k ] . csvRow . index ) ; duplicates . push ( redirs [ k ] ) ; } } if ( duplicates . length !== 0 ) { return true ; } return false ; }