Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c49b518b8a | |||
| 5f49532fc2 | |||
| e7f06a7f44 | |||
| 491477da82 | |||
| 935109abb6 | |||
| ea26a1d8ef | |||
| c7324f0240 | |||
| be44a1354b |
@@ -1,5 +1,5 @@
|
|||||||
# gitzone standard
|
# gitzone ci_default
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
@@ -50,13 +50,13 @@ testLTS:
|
|||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
testSTABLE:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci node install stable
|
- npmci node install lts
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
@@ -65,7 +65,7 @@ testSTABLE:
|
|||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
script:
|
script:
|
||||||
- npmci node install stable
|
- npmci node install lts
|
||||||
- npmci npm publish
|
- npmci npm publish
|
||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
@@ -78,19 +78,11 @@ release:
|
|||||||
# ====================
|
# ====================
|
||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
image: docker:stable
|
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
services:
|
|
||||||
- docker:stable-dind
|
|
||||||
script:
|
script:
|
||||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
- npmci command npm install -g tslint typescript
|
||||||
- docker run
|
- npmci npm install
|
||||||
--env SOURCE_CODE="$PWD"
|
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||||
--volume "$PWD":/code
|
|
||||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
|
||||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
|
||||||
artifacts:
|
|
||||||
paths: [codeclimate.json]
|
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- priv
|
- priv
|
||||||
|
|||||||
910
package-lock.json
generated
910
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
29
package.json
29
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartcsv",
|
"name": "@pushrocks/smartcsv",
|
||||||
"version": "1.0.16",
|
"version": "1.0.20",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "handle csv data | gitzone standard compliant",
|
"description": "handle csv data | gitzone standard compliant",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@@ -13,14 +13,25 @@
|
|||||||
"format": "(gitzone format)"
|
"format": "(gitzone format)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.11",
|
"@gitzone/tsbuild": "^2.1.17",
|
||||||
"@gitzone/tsrun": "^1.2.6",
|
"@gitzone/tsrun": "^1.2.8",
|
||||||
"@gitzone/tstest": "^1.0.21",
|
"@gitzone/tstest": "^1.0.28",
|
||||||
"@pushrocks/smartfile": "^7.0.2",
|
"@pushrocks/smartfile": "^7.0.6",
|
||||||
"@pushrocks/tapbundle": "^3.0.9",
|
"@pushrocks/tapbundle": "^3.2.0",
|
||||||
"@types/node": "^12.0.2"
|
"@types/node": "^12.12.17"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartpromise": "^3.0.2"
|
"@pushrocks/smartpromise": "^3.0.6",
|
||||||
}
|
"@pushrocks/smartstring": "^3.0.17"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"ts/*",
|
||||||
|
"ts_web/*",
|
||||||
|
"dist/*",
|
||||||
|
"dist_web/*",
|
||||||
|
"assets/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
Header 1;Header 2; Header3
|
Header 1;Header 2;"Header 3"
|
||||||
row1data1; row1data2; row1data3
|
"row1data1";"row1data2";"row1data3"
|
||||||
row2data1; row2data2; row2data3
|
"row2data1";row2data2;"row2data3"
|
||||||
|
|||||||
|
@@ -13,7 +13,7 @@ tap.test('should read a file', async tools => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
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, unquote: true });
|
||||||
const result = await testCsv.exportAsObject();
|
const result = await testCsv.exportAsObject();
|
||||||
console.log(result);
|
console.log(result);
|
||||||
});
|
});
|
||||||
|
|||||||
63
ts/index.ts
63
ts/index.ts
@@ -2,6 +2,7 @@ import * as plugins from './smartcsv.plugins';
|
|||||||
|
|
||||||
export interface ICsvConstructorOptions {
|
export interface ICsvConstructorOptions {
|
||||||
headers: boolean;
|
headers: boolean;
|
||||||
|
unquote?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Csv {
|
export class Csv {
|
||||||
@@ -17,6 +18,7 @@ export class Csv {
|
|||||||
optionsArg: ICsvConstructorOptions
|
optionsArg: ICsvConstructorOptions
|
||||||
): Promise<Csv> {
|
): Promise<Csv> {
|
||||||
const csvInstance = new Csv(csvStringArg, optionsArg);
|
const csvInstance = new Csv(csvStringArg, optionsArg);
|
||||||
|
await csvInstance.exportAsObject();
|
||||||
return csvInstance;
|
return csvInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,21 +53,40 @@ export class Csv {
|
|||||||
public csvString: string;
|
public csvString: string;
|
||||||
public headers: string[];
|
public headers: string[];
|
||||||
public keyFrame: string = null;
|
public keyFrame: string = null;
|
||||||
|
public data: any[];
|
||||||
|
|
||||||
|
private base64RecognitionPrefix = '####12345####';
|
||||||
|
|
||||||
public options: ICsvConstructorOptions = {
|
public options: ICsvConstructorOptions = {
|
||||||
headers: true
|
headers: true,
|
||||||
|
unquote: true
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(csvStringArg: string, optionsArg: ICsvConstructorOptions) {
|
constructor(csvStringArg: string, optionsArg: ICsvConstructorOptions) {
|
||||||
this.csvString = csvStringArg;
|
this.options = {
|
||||||
this.options = optionsArg;
|
...this.options,
|
||||||
|
...optionsArg
|
||||||
|
};
|
||||||
|
|
||||||
|
let csvStringToParse = csvStringArg;
|
||||||
|
if (this.options.unquote) {
|
||||||
|
csvStringToParse = csvStringToParse.replace(
|
||||||
|
/"(.*?)"/gi,
|
||||||
|
(match, p1, offset, originalString) => {
|
||||||
|
return this.base64RecognitionPrefix + plugins.smartstring.base64.encode(match);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.csvString = csvStringToParse;
|
||||||
|
|
||||||
this.determineKeyframe();
|
this.determineKeyframe();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* determines the keyframe of the csv string
|
* determines the keyframe of the csv string
|
||||||
*/
|
*/
|
||||||
public determineKeyframe() {
|
private determineKeyframe() {
|
||||||
let commaLength = 0;
|
let commaLength = 0;
|
||||||
let semicolonLength = 0;
|
let semicolonLength = 0;
|
||||||
const commaRegexResult = this.csvString.match(/,/g);
|
const commaRegexResult = this.csvString.match(/,/g);
|
||||||
@@ -87,7 +108,7 @@ export class Csv {
|
|||||||
/**
|
/**
|
||||||
* serializes the csv string
|
* serializes the csv string
|
||||||
*/
|
*/
|
||||||
public serializeCsvString() {
|
private serializeCsvString() {
|
||||||
const rowArray = this.getRows();
|
const rowArray = this.getRows();
|
||||||
const resultArray = [];
|
const resultArray = [];
|
||||||
if (this.options.headers) {
|
if (this.options.headers) {
|
||||||
@@ -100,7 +121,10 @@ export class Csv {
|
|||||||
return resultArray;
|
return resultArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getRows(): string[] {
|
/**
|
||||||
|
* gets the rows of the csv
|
||||||
|
*/
|
||||||
|
private getRows(): string[] {
|
||||||
const rowsArray = this.csvString.split('\n');
|
const rowsArray = this.csvString.split('\n');
|
||||||
if (rowsArray[rowsArray.length - 1] === '') {
|
if (rowsArray[rowsArray.length - 1] === '') {
|
||||||
rowsArray.pop();
|
rowsArray.pop();
|
||||||
@@ -108,20 +132,40 @@ export class Csv {
|
|||||||
return rowsArray;
|
return rowsArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getHeaders() {
|
private 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.keyFrame);
|
this.headers = headerRow.split(this.keyFrame);
|
||||||
|
if (this.options.unquote) {
|
||||||
|
const unquotedHeaders: string[] = [];
|
||||||
|
for (let header of this.headers) {
|
||||||
|
if (header.startsWith(this.base64RecognitionPrefix)) {
|
||||||
|
header = header.replace(this.base64RecognitionPrefix, '');
|
||||||
|
unquotedHeaders.push(plugins.smartstring.base64.decode(header).replace(/['"]+/g, ''));
|
||||||
|
} else {
|
||||||
|
unquotedHeaders.push(header);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.headers = unquotedHeaders;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return this.headers;
|
return this.headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public createDataObject(dataArray: string[]) {
|
private 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];
|
let value = dataArray[i];
|
||||||
|
if (this.options.unquote && value.startsWith(this.base64RecognitionPrefix)) {
|
||||||
|
value = value.replace(this.base64RecognitionPrefix, '');
|
||||||
|
value = plugins.smartstring.base64
|
||||||
|
.decode(value)
|
||||||
|
.replace(/['"]+/g, '')
|
||||||
|
.replace(/['"]+/g, '');
|
||||||
|
}
|
||||||
|
resultJson[this.headers[i]] = value;
|
||||||
}
|
}
|
||||||
return resultJson;
|
return resultJson;
|
||||||
}
|
}
|
||||||
@@ -132,6 +176,7 @@ export class Csv {
|
|||||||
for (const dataArray of serializedData) {
|
for (const dataArray of serializedData) {
|
||||||
dataObjects.push(this.createDataObject(dataArray));
|
dataObjects.push(this.createDataObject(dataArray));
|
||||||
}
|
}
|
||||||
|
this.data = dataObjects;
|
||||||
return dataObjects;
|
return dataObjects;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
import * as smartq from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
import * as smartstring from '@pushrocks/smartstring';
|
||||||
|
|
||||||
export { smartq };
|
export { smartpromise, smartstring };
|
||||||
|
|||||||
Reference in New Issue
Block a user