diff options
Diffstat (limited to 'frontend/src/app/_services/csv-parse.service.ts')
-rw-r--r-- | frontend/src/app/_services/csv-parse.service.ts | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/frontend/src/app/_services/csv-parse.service.ts b/frontend/src/app/_services/csv-parse.service.ts new file mode 100644 index 00000000..d53f504e --- /dev/null +++ b/frontend/src/app/_services/csv-parse.service.ts @@ -0,0 +1,53 @@ +import { Injectable } from "@angular/core"; +@Injectable({ providedIn: 'root' }) +export class CsvParseService { + + csvToArray(strData: string, strDelimiter: string): string[][] { + strDelimiter = (strDelimiter || ","); + + let objPattern = new RegExp( + ( + // Delimiters. + "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" + + + // Quoted fields. + "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" + + + // Standard fields. + "([^\"\\" + strDelimiter + "\\r\\n]*))" + ), + "gi" + ); + + let arrData: string[][] = [[]]; + + let arrMatches = null; + + while (arrMatches = objPattern.exec(strData)) { + + let strMatchedDelimiter = arrMatches[1]; + + if ( + strMatchedDelimiter.length && + strMatchedDelimiter !== strDelimiter + ) { + arrData.push([]); + } + + let strMatchedValue; + + if (arrMatches[2]) { + strMatchedValue = arrMatches[2].replace( + new RegExp("\"\"", "g"), + "\"" + ); + } else { + strMatchedValue = arrMatches[3]; + } + + arrData[arrData.length - 1].push(strMatchedValue); + } + + return (arrData); + } +}
\ No newline at end of file |