2018-05-10 17:47:14 +00:00
|
|
|
"use strict";
|
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
class Csv {
|
2018-05-10 22:53:27 +00:00
|
|
|
constructor() {
|
|
|
|
this.keyFrame = null;
|
|
|
|
this.options = {
|
|
|
|
headers: true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
static createCsvFromString(csvStringArg, options) {
|
2018-05-10 17:47:14 +00:00
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
2018-05-10 22:53:27 +00:00
|
|
|
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 = ',';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
serializeCsvString() {
|
|
|
|
const rowArray = this.getRows();
|
|
|
|
const resultArray = [];
|
|
|
|
if (this.options.headers) {
|
|
|
|
this.getHeaders();
|
|
|
|
rowArray.shift();
|
|
|
|
}
|
|
|
|
for (const row of rowArray) {
|
|
|
|
resultArray.push(row.split(this.keyFrame));
|
|
|
|
}
|
|
|
|
return resultArray;
|
|
|
|
}
|
|
|
|
getRows() {
|
|
|
|
return this.csvString.split('\n');
|
|
|
|
}
|
2018-05-10 17:47:14 +00:00
|
|
|
getHeaders() {
|
2018-05-10 22:53:27 +00:00
|
|
|
const rowArray = this.getRows();
|
|
|
|
if (this.options.headers) {
|
|
|
|
let headerRow = rowArray[0];
|
|
|
|
this.headers = headerRow.split(this.keyFrame);
|
|
|
|
}
|
|
|
|
return this.headers;
|
2018-05-10 17:47:14 +00:00
|
|
|
}
|
2018-05-10 22:53:27 +00:00
|
|
|
createDataObject(dataArray) {
|
|
|
|
const neededIterations = dataArray.length;
|
|
|
|
let resultJson = {};
|
|
|
|
for (let i = 0; i < neededIterations; i++) {
|
|
|
|
resultJson[this.headers[i]] = dataArray[i];
|
|
|
|
}
|
|
|
|
return resultJson;
|
|
|
|
}
|
|
|
|
exportAsObject() {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
const serializedData = this.serializeCsvString();
|
|
|
|
const dataObjects = [];
|
|
|
|
for (const dataArray of serializedData) {
|
|
|
|
dataObjects.push(this.createDataObject(dataArray));
|
|
|
|
}
|
|
|
|
return dataObjects;
|
|
|
|
});
|
2018-05-10 17:47:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.Csv = Csv;
|
2018-05-10 22:53:27 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmR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