feat(parser): now handles comma and semicolon seperated csv strings
This commit is contained in:
45
ts/index.ts
45
ts/index.ts
@ -5,19 +5,43 @@ export interface ICsvConstructorOptions {
|
||||
}
|
||||
|
||||
export class Csv {
|
||||
public static async createCsvFromString (csvStringArg: string, options: ICsvConstructorOptions): Promise<Csv> {
|
||||
public static async createCsvFromString(
|
||||
csvStringArg: string,
|
||||
options: ICsvConstructorOptions
|
||||
): Promise<Csv> {
|
||||
const csvInstance = new Csv();
|
||||
csvInstance.csvString = csvStringArg;
|
||||
csvInstance.determineKeyframe();
|
||||
return csvInstance;
|
||||
}
|
||||
public csvString: string;
|
||||
public headers: string[];
|
||||
public keyFrame: string = null;
|
||||
|
||||
public options: ICsvConstructorOptions = {
|
||||
headers: true
|
||||
};
|
||||
|
||||
public serializeCsvString () {
|
||||
public determineKeyframe() {
|
||||
let commaLength = 0;
|
||||
let semicolonLength = 0;
|
||||
const commaRegexResult = this.csvString.match(/,/g);
|
||||
const semicolonRegexResult = this.csvString.match(/;/g);
|
||||
if (commaRegexResult) {
|
||||
commaLength = commaRegexResult.length;
|
||||
}
|
||||
if (semicolonRegexResult) {
|
||||
semicolonLength = semicolonRegexResult.length;
|
||||
}
|
||||
// tslint:disable-next-line:prefer-conditional-expression
|
||||
if (commaLength < semicolonLength) {
|
||||
this.keyFrame = ';';
|
||||
} else {
|
||||
this.keyFrame = ',';
|
||||
}
|
||||
}
|
||||
|
||||
public serializeCsvString() {
|
||||
const rowArray = this.getRows();
|
||||
const resultArray = [];
|
||||
if (this.options.headers) {
|
||||
@ -25,34 +49,34 @@ export class Csv {
|
||||
rowArray.shift();
|
||||
}
|
||||
for (const row of rowArray) {
|
||||
resultArray.push(row.split(';'));
|
||||
resultArray.push(row.split(this.keyFrame));
|
||||
}
|
||||
return resultArray;
|
||||
}
|
||||
|
||||
public getRows () {
|
||||
public getRows() {
|
||||
return this.csvString.split('\n');
|
||||
}
|
||||
|
||||
public getHeaders () {
|
||||
public getHeaders() {
|
||||
const rowArray = this.getRows();
|
||||
if (this.options.headers) {
|
||||
let headerRow = rowArray[ 0 ];
|
||||
this.headers = headerRow.split(';');
|
||||
let headerRow = rowArray[0];
|
||||
this.headers = headerRow.split(this.keyFrame);
|
||||
}
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
public createDataObject (dataArray: string[]) {
|
||||
public createDataObject(dataArray: string[]) {
|
||||
const neededIterations = dataArray.length;
|
||||
let resultJson: any = {};
|
||||
for(let i = 0; i < neededIterations; i++) {
|
||||
for (let i = 0; i < neededIterations; i++) {
|
||||
resultJson[this.headers[i]] = dataArray[i];
|
||||
}
|
||||
return resultJson;
|
||||
}
|
||||
|
||||
public async exportAsObject (): Promise<any> {
|
||||
public async exportAsObject(): Promise<any> {
|
||||
const serializedData = this.serializeCsvString();
|
||||
const dataObjects = [];
|
||||
for (const dataArray of serializedData) {
|
||||
@ -60,5 +84,4 @@ export class Csv {
|
||||
}
|
||||
return dataObjects;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,3 @@
|
||||
import * as smartq from 'smartq';
|
||||
|
||||
export {
|
||||
smartq
|
||||
};
|
||||
export { smartq };
|
||||
|
Reference in New Issue
Block a user