Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e7f06a7f44 | |||
| 491477da82 | |||
| 935109abb6 | |||
| ea26a1d8ef | |||
| c7324f0240 | |||
| be44a1354b | |||
| 13f3f7b2f6 | |||
| 94f2620161 | |||
| bdebb3308c | |||
| cb5ac7618b | |||
| 00aed79e18 | |||
| 438053a2f9 | |||
| 7479328e13 | |||
| 56d7ae8952 | |||
| feb41df3e2 | |||
| 1474cb1665 | |||
| 6e289a01b1 | |||
| c08aa1959f |
18
.gitignore
vendored
18
.gitignore
vendored
@@ -1,6 +1,22 @@
|
|||||||
.nogit/
|
.nogit/
|
||||||
node_modules/
|
|
||||||
|
# artifacts
|
||||||
coverage/
|
coverage/
|
||||||
public/
|
public/
|
||||||
pages/
|
pages/
|
||||||
|
|
||||||
|
# installs
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# caches
|
||||||
.yarn/
|
.yarn/
|
||||||
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_web/
|
||||||
|
dist_serve/
|
||||||
|
dist_ts_web/
|
||||||
|
|
||||||
|
# custom
|
||||||
@@ -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:
|
||||||
@@ -26,6 +26,7 @@ mirror:
|
|||||||
snyk:
|
snyk:
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci command npm install -g snyk
|
- npmci command npm install -g snyk
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command snyk test
|
||||||
@@ -36,21 +37,11 @@ snyk:
|
|||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
testLEGACY:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci node install legacy
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
testLTS:
|
testLTS:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci node install lts
|
- npmci node install lts
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
@@ -58,13 +49,14 @@ testLTS:
|
|||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
testSTABLE:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci node install stable
|
- npmci npm prepare
|
||||||
|
- 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
|
||||||
@@ -73,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
|
||||||
@@ -86,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
|
||||||
@@ -117,9 +101,10 @@ pages:
|
|||||||
image: hosttoday/ht-docker-node:npmci
|
image: hosttoday/ht-docker-node:npmci
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g typedoc typescript
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
- npmci command tsdoc
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
@@ -130,13 +115,3 @@ pages:
|
|||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
|
||||||
windowsCompatibility:
|
|
||||||
image: stefanscherer/node-windows:10-build-tools
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npm install & npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- windows
|
|
||||||
allow_failure: true
|
|
||||||
|
|||||||
4
.snyk
Normal file
4
.snyk
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
|
||||||
|
version: v1.12.0
|
||||||
|
ignore: {}
|
||||||
|
patch: {}
|
||||||
@@ -1,8 +1,16 @@
|
|||||||
{
|
{
|
||||||
"npmci": {
|
"npmci": {
|
||||||
"npmGlobalTools": [
|
"npmGlobalTools": [],
|
||||||
"@gitzone/npmts"
|
|
||||||
],
|
|
||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "pushrocks",
|
||||||
|
"gitrepo": "smartcsv",
|
||||||
|
"shortDescription": "handle csv data | gitzone standard compliant",
|
||||||
|
"npmPackagename": "@pushrocks/smartcsv",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
1671
package-lock.json
generated
1671
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.10",
|
"version": "1.0.19",
|
||||||
"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.0.22",
|
"@gitzone/tsbuild": "^2.1.17",
|
||||||
"@gitzone/tsrun": "^1.1.12",
|
"@gitzone/tsrun": "^1.2.8",
|
||||||
"@gitzone/tstest": "^1.0.15",
|
"@gitzone/tstest": "^1.0.28",
|
||||||
"@types/node": "^10.7.1",
|
"@pushrocks/smartfile": "^7.0.6",
|
||||||
"smartfile": "^4.2.28",
|
"@pushrocks/tapbundle": "^3.0.13",
|
||||||
"tapbundle": "^2.0.2"
|
"@types/node": "^12.7.12"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"smartq": "^1.1.8"
|
"@pushrocks/smartpromise": "^3.0.6",
|
||||||
}
|
"@pushrocks/smartstring": "^3.0.14"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"ts/*",
|
||||||
|
"ts_web/*",
|
||||||
|
"dist/*",
|
||||||
|
"dist_web/*",
|
||||||
|
"assets/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
26
readme.md
Normal file
26
readme.md
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
# @pushrocks/smartcsv
|
||||||
|
handle csv data | gitzone standard compliant
|
||||||
|
|
||||||
|
## Availabililty and Links
|
||||||
|
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartcsv)
|
||||||
|
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartcsv)
|
||||||
|
* [github.com (source mirror)](https://github.com/pushrocks/smartcsv)
|
||||||
|
* [docs (typedoc)](https://pushrocks.gitlab.io/smartcsv/)
|
||||||
|
|
||||||
|
## Status for master
|
||||||
|
[](https://gitlab.com/pushrocks/smartcsv/commits/master)
|
||||||
|
[](https://gitlab.com/pushrocks/smartcsv/commits/master)
|
||||||
|
[](https://www.npmjs.com/package/@pushrocks/smartcsv)
|
||||||
|
[](https://snyk.io/test/npm/@pushrocks/smartcsv)
|
||||||
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
|
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
|
[](https://prettier.io/)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
|
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||||
|
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||||
|
|
||||||
|
[](https://maintainedby.lossless.com)
|
||||||
@@ -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"
|
||||||
|
|||||||
|
14
test/test.ts
14
test/test.ts
@@ -1,9 +1,9 @@
|
|||||||
// tslint:disable-next-line:no-implicit-dependencies
|
// tslint:disable-next-line:no-implicit-dependencies
|
||||||
import { expect, tap } from 'tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import * as smartcsv from '../ts/index';
|
import * as smartcsv from '../ts/index';
|
||||||
|
|
||||||
// tslint:disable-next-line:no-implicit-dependencies
|
// tslint:disable-next-line:no-implicit-dependencies
|
||||||
import * as smartfile from 'smartfile';
|
import * as smartfile from '@pushrocks/smartfile';
|
||||||
|
|
||||||
let fileString: string;
|
let fileString: string;
|
||||||
let testCsv: smartcsv.Csv;
|
let testCsv: smartcsv.Csv;
|
||||||
@@ -13,9 +13,17 @@ 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);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('should create a valid csv string', async () => {
|
||||||
|
const createdCsvString = await smartcsv.Csv.createCsvStringFromArray([
|
||||||
|
{ wow: 'hi', wow2: 'there' },
|
||||||
|
{ wow: 'really', wow3: 'yes' }
|
||||||
|
]);
|
||||||
|
console.log(createdCsvString);
|
||||||
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
|||||||
106
ts/index.ts
106
ts/index.ts
@@ -2,27 +2,89 @@ import * as plugins from './smartcsv.plugins';
|
|||||||
|
|
||||||
export interface ICsvConstructorOptions {
|
export interface ICsvConstructorOptions {
|
||||||
headers: boolean;
|
headers: boolean;
|
||||||
|
unquote?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Csv {
|
export class Csv {
|
||||||
|
// STATIC
|
||||||
|
|
||||||
|
/**
|
||||||
|
* creates a Csv Object from string
|
||||||
|
* @param csvStringArg
|
||||||
|
* @param optionsArg
|
||||||
|
*/
|
||||||
public static async createCsvFromString(
|
public static async createCsvFromString(
|
||||||
csvStringArg: string,
|
csvStringArg: string,
|
||||||
options: ICsvConstructorOptions
|
optionsArg: ICsvConstructorOptions
|
||||||
): Promise<Csv> {
|
): Promise<Csv> {
|
||||||
const csvInstance = new Csv();
|
const csvInstance = new Csv(csvStringArg, optionsArg);
|
||||||
csvInstance.csvString = csvStringArg;
|
await csvInstance.exportAsObject();
|
||||||
csvInstance.determineKeyframe();
|
|
||||||
return csvInstance;
|
return csvInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async createCsvStringFromArray(arrayArg: any[]): Promise<string> {
|
||||||
|
const foundKeys: string[] = [];
|
||||||
|
|
||||||
|
// lets deal with the keys
|
||||||
|
for (const objectArg of arrayArg) {
|
||||||
|
for (const key of Object.keys(objectArg)) {
|
||||||
|
foundKeys.includes(key) ? null : foundKeys.push(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// lets deal with the data
|
||||||
|
const dataRows: string[] = [];
|
||||||
|
for (const objectArg of arrayArg) {
|
||||||
|
const dataRowArray: string[] = [];
|
||||||
|
for (const key of foundKeys) {
|
||||||
|
dataRowArray.push(objectArg[key]);
|
||||||
|
}
|
||||||
|
dataRows.push(dataRowArray.join(','));
|
||||||
|
}
|
||||||
|
|
||||||
|
// lets put togehter the csv string and return it
|
||||||
|
const headerString = foundKeys.join(',');
|
||||||
|
const dataString = dataRows.join('\n');
|
||||||
|
const csvString = `${headerString}\n${dataString}\n`;
|
||||||
|
return csvString;
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
public csvString: string;
|
public csvString: string;
|
||||||
public headers: string[];
|
public headers: string[];
|
||||||
public keyFrame: string = null;
|
public keyFrame: string = null;
|
||||||
|
public data: any[];
|
||||||
|
|
||||||
public options: ICsvConstructorOptions = {
|
public options: ICsvConstructorOptions = {
|
||||||
headers: true
|
headers: true,
|
||||||
|
unquote: true
|
||||||
};
|
};
|
||||||
|
|
||||||
public determineKeyframe() {
|
constructor(csvStringArg: string, optionsArg: ICsvConstructorOptions) {
|
||||||
|
this.options = {
|
||||||
|
...this.options,
|
||||||
|
...optionsArg
|
||||||
|
};
|
||||||
|
|
||||||
|
let csvStringToParse = csvStringArg;
|
||||||
|
if (this.options.unquote) {
|
||||||
|
csvStringToParse = csvStringToParse.replace(
|
||||||
|
/"(.*?)"/gi,
|
||||||
|
(match, p1, offset, originalString) => {
|
||||||
|
return plugins.smartstring.base64.encode(match);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.csvString = csvStringToParse;
|
||||||
|
|
||||||
|
this.determineKeyframe();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* determines the keyframe of the csv string
|
||||||
|
*/
|
||||||
|
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);
|
||||||
@@ -41,7 +103,10 @@ export class Csv {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public serializeCsvString() {
|
/**
|
||||||
|
* serializes the csv string
|
||||||
|
*/
|
||||||
|
private serializeCsvString() {
|
||||||
const rowArray = this.getRows();
|
const rowArray = this.getRows();
|
||||||
const resultArray = [];
|
const resultArray = [];
|
||||||
if (this.options.headers) {
|
if (this.options.headers) {
|
||||||
@@ -54,7 +119,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();
|
||||||
@@ -62,20 +130,35 @@ 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 (const header of this.headers) {
|
||||||
|
if (plugins.smartstring.type.isBase64(header)) {
|
||||||
|
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 && plugins.smartstring.type.isBase64(value)) {
|
||||||
|
value = plugins.smartstring.base64.decode(value).replace(/['"]+/g, '').replace(/['"]+/g, '');
|
||||||
|
}
|
||||||
|
resultJson[this.headers[i]] = value;
|
||||||
}
|
}
|
||||||
return resultJson;
|
return resultJson;
|
||||||
}
|
}
|
||||||
@@ -86,6 +169,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 'smartq';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
import * as smartstring from '@pushrocks/smartstring';
|
||||||
|
|
||||||
export { smartq };
|
export { smartpromise, smartstring };
|
||||||
|
|||||||
26
tslint.json
26
tslint.json
@@ -1,13 +1,17 @@
|
|||||||
{
|
{
|
||||||
"extends": [
|
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||||
"tslint:latest",
|
"rules": {
|
||||||
"tslint-config-prettier"
|
"semicolon": [true, "always"],
|
||||||
],
|
"no-console": false,
|
||||||
"rules": {
|
"ordered-imports": false,
|
||||||
"semicolon": [
|
"object-literal-sort-keys": false,
|
||||||
true,
|
"member-ordering": {
|
||||||
"always"
|
"options":{
|
||||||
]
|
"order": [
|
||||||
|
"static-method"
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"defaultSeverity": "warning"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user