2018-05-10 17:47:14 +00:00
|
|
|
import * as plugins from './smartcsv.plugins';
|
|
|
|
|
|
|
|
export interface ICsvConstructorOptions {
|
|
|
|
headers: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Csv {
|
2018-05-10 22:53:27 +00:00
|
|
|
public static async createCsvFromString(
|
|
|
|
csvStringArg: string,
|
|
|
|
options: ICsvConstructorOptions
|
|
|
|
): Promise<Csv> {
|
2018-05-10 17:47:14 +00:00
|
|
|
const csvInstance = new Csv();
|
|
|
|
csvInstance.csvString = csvStringArg;
|
2018-05-10 22:53:27 +00:00
|
|
|
csvInstance.determineKeyframe();
|
2018-05-10 17:47:14 +00:00
|
|
|
return csvInstance;
|
|
|
|
}
|
|
|
|
public csvString: string;
|
|
|
|
public headers: string[];
|
2018-05-10 22:53:27 +00:00
|
|
|
public keyFrame: string = null;
|
2018-05-10 17:47:14 +00:00
|
|
|
|
|
|
|
public options: ICsvConstructorOptions = {
|
|
|
|
headers: true
|
|
|
|
};
|
|
|
|
|
2018-05-10 22:53:27 +00:00
|
|
|
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() {
|
2018-05-10 17:47:14 +00:00
|
|
|
const rowArray = this.getRows();
|
|
|
|
const resultArray = [];
|
|
|
|
if (this.options.headers) {
|
|
|
|
this.getHeaders();
|
|
|
|
rowArray.shift();
|
|
|
|
}
|
|
|
|
for (const row of rowArray) {
|
2018-05-10 22:53:27 +00:00
|
|
|
resultArray.push(row.split(this.keyFrame));
|
2018-05-10 17:47:14 +00:00
|
|
|
}
|
|
|
|
return resultArray;
|
|
|
|
}
|
|
|
|
|
2018-06-10 12:29:09 +00:00
|
|
|
public getRows(): string[] {
|
|
|
|
const rowsArray = this.csvString.split('\n');
|
|
|
|
if (rowsArray[rowsArray.length - 1] === '') {
|
|
|
|
rowsArray.pop();
|
|
|
|
}
|
|
|
|
return rowsArray;
|
2018-05-10 17:47:14 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 22:53:27 +00:00
|
|
|
public getHeaders() {
|
2018-05-10 17:47:14 +00:00
|
|
|
const rowArray = this.getRows();
|
|
|
|
if (this.options.headers) {
|
2018-05-10 22:53:27 +00:00
|
|
|
let headerRow = rowArray[0];
|
|
|
|
this.headers = headerRow.split(this.keyFrame);
|
2018-05-10 17:47:14 +00:00
|
|
|
}
|
|
|
|
return this.headers;
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:53:27 +00:00
|
|
|
public createDataObject(dataArray: string[]) {
|
2018-05-10 17:47:14 +00:00
|
|
|
const neededIterations = dataArray.length;
|
|
|
|
let resultJson: any = {};
|
2018-05-10 22:53:27 +00:00
|
|
|
for (let i = 0; i < neededIterations; i++) {
|
2018-05-10 17:47:14 +00:00
|
|
|
resultJson[this.headers[i]] = dataArray[i];
|
|
|
|
}
|
|
|
|
return resultJson;
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:53:27 +00:00
|
|
|
public async exportAsObject(): Promise<any> {
|
2018-05-10 17:47:14 +00:00
|
|
|
const serializedData = this.serializeCsvString();
|
|
|
|
const dataObjects = [];
|
|
|
|
for (const dataArray of serializedData) {
|
|
|
|
dataObjects.push(this.createDataObject(dataArray));
|
|
|
|
}
|
|
|
|
return dataObjects;
|
|
|
|
}
|
|
|
|
}
|