feat(parser): now handles comma and semicolon seperated csv strings
This commit is contained in:
parent
2e23d959da
commit
0de7b75dca
@ -12,6 +12,9 @@ stages:
|
|||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# security stage
|
||||||
|
# ====================
|
||||||
mirror:
|
mirror:
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
@ -30,6 +33,9 @@ snyk:
|
|||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
|
# ====================
|
||||||
|
# test stage
|
||||||
|
# ====================
|
||||||
testLEGACY:
|
testLEGACY:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
@ -68,7 +74,6 @@ release:
|
|||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm prepare
|
|
||||||
- npmci npm publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
|
20
dist/index.d.ts
vendored
20
dist/index.d.ts
vendored
@ -1,6 +1,16 @@
|
|||||||
export declare class Csv {
|
export interface ICsvConstructorOptions {
|
||||||
static createCsvFromString(csvStringArg: string): Promise<Csv>;
|
headers: boolean;
|
||||||
csvString: string;
|
}
|
||||||
getHeaders(): void;
|
export declare class Csv {
|
||||||
exportAsJson(): void;
|
static createCsvFromString(csvStringArg: string, options: ICsvConstructorOptions): Promise<Csv>;
|
||||||
|
csvString: string;
|
||||||
|
headers: string[];
|
||||||
|
keyFrame: string;
|
||||||
|
options: ICsvConstructorOptions;
|
||||||
|
determineKeyframe(): void;
|
||||||
|
serializeCsvString(): any[];
|
||||||
|
getRows(): string[];
|
||||||
|
getHeaders(): string[];
|
||||||
|
createDataObject(dataArray: string[]): any;
|
||||||
|
exportAsObject(): Promise<any>;
|
||||||
}
|
}
|
||||||
|
74
dist/index.js
vendored
74
dist/index.js
vendored
@ -9,20 +9,80 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|||||||
};
|
};
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
class Csv {
|
class Csv {
|
||||||
static createCsvFromString(csvStringArg) {
|
constructor() {
|
||||||
|
this.keyFrame = null;
|
||||||
|
this.options = {
|
||||||
|
headers: true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
static createCsvFromString(csvStringArg, options) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
const csvInstance = new Csv();
|
const csvInstance = new Csv();
|
||||||
csvInstance.csvString = csvStringArg;
|
csvInstance.csvString = csvStringArg;
|
||||||
|
csvInstance.determineKeyframe();
|
||||||
return csvInstance;
|
return csvInstance;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
getHeaders() {
|
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 = ',';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
exportAsJson() {
|
serializeCsvString() {
|
||||||
let json = {
|
const rowArray = this.getRows();
|
||||||
[`key`]: 'hi'
|
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');
|
||||||
|
}
|
||||||
|
getHeaders() {
|
||||||
|
const rowArray = this.getRows();
|
||||||
|
if (this.options.headers) {
|
||||||
|
let headerRow = rowArray[0];
|
||||||
|
this.headers = headerRow.split(this.keyFrame);
|
||||||
|
}
|
||||||
|
return this.headers;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.Csv = Csv;
|
exports.Csv = Csv;
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBRUE7SUFDUyxNQUFNLENBQU8sbUJBQW1CLENBQUUsWUFBb0I7O1lBQzNELE1BQU0sV0FBVyxHQUFHLElBQUksR0FBRyxFQUFFLENBQUM7WUFDOUIsV0FBVyxDQUFDLFNBQVMsR0FBRyxZQUFZLENBQUM7WUFDckMsT0FBTyxXQUFXLENBQUM7UUFDckIsQ0FBQztLQUFBO0lBR00sVUFBVTtJQUVqQixDQUFDO0lBRU0sWUFBWTtRQUNqQixJQUFJLEl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0FBRyxTQUFTLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxRQUFRLENBQUMsQ0FBQztTQUMvQztRQUNELE9BQU8sSUFBSSxDQUFDLE9BQU8sQ0FBQztJQUN0QixDQUFDO0lBRU0sZ0JBQWdCLENBQUMsU0FBbUI7UUFDekMsTUFBTSxnQkFBZ0IsR0FBRyxTQUFTLENBQUMsTUFBTSxDQUFDO1FBQzFDLElBQUksVUFBVSxHQUFRLEVBQUUsQ0FBQztRQUN6QixLQUFLLElBQUksQ0FBQyxHQUFHLENBQUMsRUFBRSxDQUFDLEdBQUcsZ0JBQWdCLEVBQUUsQ0FBQyxFQUFFLEVBQUU7WUFDekMsVUFBVSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsQ0FBQyxDQUFDLENBQUMsR0FBRyxTQUFTLENBQUMsQ0FBQyxDQUFDLENBQUM7U0FDNUM7UUFDRCxPQUFPLFVBQVUsQ0FBQztJQUNwQixDQUFDO0lBRVksY0FBYzs7WUFDekIsTUFBTSxjQUFjLEdBQUcsSUFBSSxDQUFDLGtCQUFrQixFQUFFLENBQUM7WUFDakQsTUFBTSxXQUFXLEdBQUcsRUFBRSxDQUFDO1lBQ3ZCLEtBQUssTUFBTSxTQUFTLElBQUksY0FBYyxFQUFFO2dCQUN0QyxXQUFXLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxnQkFBZ0IsQ0FBQyxTQUFTLENBQUMsQ0FBQyxDQUFDO2FBQ3BEO1lBQ0QsT0FBTyxXQUFXLENBQUM7UUFDckIsQ0FBQztLQUFBO0NBQ0Y7QUFoRkQsa0JBZ0ZDIn0=
|
4
dist/smartcsv.plugins.d.ts
vendored
4
dist/smartcsv.plugins.d.ts
vendored
@ -1,2 +1,2 @@
|
|||||||
import * as csv from 'csv';
|
import * as smartq from 'smartq';
|
||||||
export { csv };
|
export { smartq };
|
||||||
|
6
dist/smartcsv.plugins.js
vendored
6
dist/smartcsv.plugins.js
vendored
@ -1,5 +1,5 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
const csv = require("csv");
|
const smartq = require("smartq");
|
||||||
exports.csv = csv;
|
exports.smartq = smartq;
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjc3YucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y3N2LnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSwyQkFBMkI7QUFHekIsa0JBQUcifQ==
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRjc3YucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0Y3N2LnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxpQ0FBaUM7QUFFeEIsd0JBQU0ifQ==
|
@ -8,12 +8,12 @@ import * as smartfile from 'smartfile';
|
|||||||
let fileString: string;
|
let fileString: string;
|
||||||
let testCsv: smartcsv.Csv;
|
let testCsv: smartcsv.Csv;
|
||||||
|
|
||||||
tap.test('should read a file', async (tools) => {
|
tap.test('should read a file', async tools => {
|
||||||
fileString = smartfile.fs.toStringSync('./test/sample.csv');
|
fileString = smartfile.fs.toStringSync('./test/sample.csv');
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should create a valid csv', async () => {
|
tap.test('should create a valid csv', async () => {
|
||||||
testCsv = await smartcsv.Csv.createCsvFromString(fileString, {headers: true});
|
testCsv = await smartcsv.Csv.createCsvFromString(fileString, { headers: true });
|
||||||
const result = await testCsv.exportAsObject();
|
const result = await testCsv.exportAsObject();
|
||||||
console.log(result);
|
console.log(result);
|
||||||
});
|
});
|
||||||
|
45
ts/index.ts
45
ts/index.ts
@ -5,19 +5,43 @@ export interface ICsvConstructorOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Csv {
|
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();
|
const csvInstance = new Csv();
|
||||||
csvInstance.csvString = csvStringArg;
|
csvInstance.csvString = csvStringArg;
|
||||||
|
csvInstance.determineKeyframe();
|
||||||
return csvInstance;
|
return csvInstance;
|
||||||
}
|
}
|
||||||
public csvString: string;
|
public csvString: string;
|
||||||
public headers: string[];
|
public headers: string[];
|
||||||
|
public keyFrame: string = null;
|
||||||
|
|
||||||
public options: ICsvConstructorOptions = {
|
public options: ICsvConstructorOptions = {
|
||||||
headers: true
|
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 rowArray = this.getRows();
|
||||||
const resultArray = [];
|
const resultArray = [];
|
||||||
if (this.options.headers) {
|
if (this.options.headers) {
|
||||||
@ -25,34 +49,34 @@ export class Csv {
|
|||||||
rowArray.shift();
|
rowArray.shift();
|
||||||
}
|
}
|
||||||
for (const row of rowArray) {
|
for (const row of rowArray) {
|
||||||
resultArray.push(row.split(';'));
|
resultArray.push(row.split(this.keyFrame));
|
||||||
}
|
}
|
||||||
return resultArray;
|
return resultArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getRows () {
|
public getRows() {
|
||||||
return this.csvString.split('\n');
|
return this.csvString.split('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
public getHeaders () {
|
public getHeaders() {
|
||||||
const rowArray = this.getRows();
|
const rowArray = this.getRows();
|
||||||
if (this.options.headers) {
|
if (this.options.headers) {
|
||||||
let headerRow = rowArray[ 0 ];
|
let headerRow = rowArray[0];
|
||||||
this.headers = headerRow.split(';');
|
this.headers = headerRow.split(this.keyFrame);
|
||||||
}
|
}
|
||||||
return this.headers;
|
return this.headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public createDataObject (dataArray: string[]) {
|
public createDataObject(dataArray: string[]) {
|
||||||
const neededIterations = dataArray.length;
|
const neededIterations = dataArray.length;
|
||||||
let resultJson: any = {};
|
let resultJson: any = {};
|
||||||
for(let i = 0; i < neededIterations; i++) {
|
for (let i = 0; i < neededIterations; i++) {
|
||||||
resultJson[this.headers[i]] = dataArray[i];
|
resultJson[this.headers[i]] = dataArray[i];
|
||||||
}
|
}
|
||||||
return resultJson;
|
return resultJson;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async exportAsObject (): Promise<any> {
|
public async exportAsObject(): Promise<any> {
|
||||||
const serializedData = this.serializeCsvString();
|
const serializedData = this.serializeCsvString();
|
||||||
const dataObjects = [];
|
const dataObjects = [];
|
||||||
for (const dataArray of serializedData) {
|
for (const dataArray of serializedData) {
|
||||||
@ -60,5 +84,4 @@ export class Csv {
|
|||||||
}
|
}
|
||||||
return dataObjects;
|
return dataObjects;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
import * as smartq from 'smartq';
|
import * as smartq from 'smartq';
|
||||||
|
|
||||||
export {
|
export { smartq };
|
||||||
smartq
|
|
||||||
};
|
|
||||||
|
Loading…
Reference in New Issue
Block a user