Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c4194bf037 | |||
| fd0abaeb47 | |||
| ab1c84f41f | |||
| 6f575e8516 | |||
| d11f237221 | |||
| 16826ed337 | |||
| c49b518b8a | |||
| 5f49532fc2 | |||
| e7f06a7f44 | |||
| 491477da82 | |||
| 935109abb6 | |||
| ea26a1d8ef |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -15,8 +15,6 @@ node_modules/
|
||||
|
||||
# builds
|
||||
dist/
|
||||
dist_web/
|
||||
dist_serve/
|
||||
dist_ts_web/
|
||||
dist_*/
|
||||
|
||||
# custom
|
||||
@@ -4,7 +4,7 @@ image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
cache:
|
||||
paths:
|
||||
- .npmci_cache/
|
||||
key: "$CI_BUILD_STAGE"
|
||||
key: '$CI_BUILD_STAGE'
|
||||
|
||||
stages:
|
||||
- security
|
||||
@@ -12,6 +12,9 @@ stages:
|
||||
- release
|
||||
- metadata
|
||||
|
||||
before_script:
|
||||
- npm install -g @shipzone/npmci
|
||||
|
||||
# ====================
|
||||
# security stage
|
||||
# ====================
|
||||
@@ -19,57 +22,72 @@ mirror:
|
||||
stage: security
|
||||
script:
|
||||
- npmci git mirror
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
snyk:
|
||||
auditProductionDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install -g snyk
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command snyk test
|
||||
- npmci command npm install --production --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=prod --production
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
allow_failure: true
|
||||
|
||||
auditDevDependencies:
|
||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||
stage: security
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci command npm install --ignore-scripts
|
||||
- npmci command npm config set registry https://registry.npmjs.org
|
||||
- npmci command npm audit --audit-level=high --only=dev
|
||||
tags:
|
||||
- docker
|
||||
allow_failure: true
|
||||
|
||||
# ====================
|
||||
# test stage
|
||||
# ====================
|
||||
|
||||
testLTS:
|
||||
testStable:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install lts
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci npm test
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
testBuild:
|
||||
stage: test
|
||||
script:
|
||||
- npmci npm prepare
|
||||
- npmci node install lts
|
||||
- npmci node install stable
|
||||
- npmci npm install
|
||||
- npmci command npm run build
|
||||
coverage: /\d+.?\d+?\%\s*coverage/
|
||||
tags:
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
release:
|
||||
stage: release
|
||||
script:
|
||||
- npmci node install lts
|
||||
- npmci node install stable
|
||||
- npmci npm publish
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
@@ -79,11 +97,15 @@ release:
|
||||
codequality:
|
||||
stage: metadata
|
||||
allow_failure: true
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- npmci command npm install -g tslint typescript
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- priv
|
||||
|
||||
@@ -94,18 +116,20 @@ trigger:
|
||||
only:
|
||||
- tags
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
|
||||
pages:
|
||||
image: hosttoday/ht-docker-node:npmci
|
||||
stage: metadata
|
||||
script:
|
||||
- npmci node install lts
|
||||
- npmci command npm install -g @gitzone/tsdoc
|
||||
- npmci npm prepare
|
||||
- npmci npm install
|
||||
- npmci command tsdoc
|
||||
tags:
|
||||
- lossless
|
||||
- docker
|
||||
- notpriv
|
||||
only:
|
||||
|
||||
4
.snyk
4
.snyk
@@ -1,4 +0,0 @@
|
||||
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
|
||||
version: v1.12.0
|
||||
ignore: {}
|
||||
patch: {}
|
||||
11
.vscode/launch.json
vendored
Normal file
11
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"command": "npm test",
|
||||
"name": "Run npm test",
|
||||
"request": "launch",
|
||||
"type": "node-terminal"
|
||||
}
|
||||
]
|
||||
}
|
||||
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"json.schemas": [
|
||||
{
|
||||
"fileMatch": ["/npmextra.json"],
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"npmci": {
|
||||
"type": "object",
|
||||
"description": "settings for npmci"
|
||||
},
|
||||
"gitzone": {
|
||||
"type": "object",
|
||||
"description": "settings for gitzone",
|
||||
"properties": {
|
||||
"projectType": {
|
||||
"type": "string",
|
||||
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
"module": {
|
||||
"githost": "gitlab.com",
|
||||
"gitscope": "pushrocks",
|
||||
|
||||
27642
package-lock.json
generated
27642
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
41
package.json
41
package.json
@@ -1,36 +1,43 @@
|
||||
{
|
||||
"name": "@pushrocks/smartcsv",
|
||||
"version": "1.0.17",
|
||||
"version": "1.0.23",
|
||||
"private": false,
|
||||
"description": "handle csv data | gitzone standard compliant",
|
||||
"main": "dist/index.js",
|
||||
"typings": "dist/index.d.ts",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/)",
|
||||
"build": "(tsbuild)",
|
||||
"format": "(gitzone format)"
|
||||
"build": "(tsbuild)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.11",
|
||||
"@gitzone/tsrun": "^1.2.6",
|
||||
"@gitzone/tstest": "^1.0.21",
|
||||
"@pushrocks/smartfile": "^7.0.2",
|
||||
"@pushrocks/tapbundle": "^3.0.9",
|
||||
"@types/node": "^12.0.2"
|
||||
"@gitzone/tsbuild": "^2.1.28",
|
||||
"@gitzone/tsrun": "^1.2.18",
|
||||
"@gitzone/tstest": "^1.0.60",
|
||||
"@pushrocks/smartfile": "^9.0.5",
|
||||
"@pushrocks/tapbundle": "^3.2.15",
|
||||
"@types/node": "^17.0.7",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartpromise": "^3.0.2"
|
||||
"@pushrocks/smartpromise": "^3.1.6",
|
||||
"@pushrocks/smartstring": "^3.0.24"
|
||||
},
|
||||
"files": [
|
||||
"ts/*",
|
||||
"ts_web/*",
|
||||
"dist/*",
|
||||
"dist_web/*",
|
||||
"assets/*",
|
||||
"ts/**/*",
|
||||
"ts_web/**/*",
|
||||
"dist/**/*",
|
||||
"dist_*/**/*",
|
||||
"dist_ts/**/*",
|
||||
"dist_ts_web/**/*",
|
||||
"assets/**/*",
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
]
|
||||
}
|
||||
|
||||
34
readme.md
34
readme.md
@@ -8,19 +8,35 @@ handle csv data | gitzone standard compliant
|
||||
* [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/)
|
||||
|
||||
Status Category | Status Badge
|
||||
-- | --
|
||||
GitLab Pipelines | [](https://lossless.cloud)
|
||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||
npm | [](https://lossless.cloud)
|
||||
Snyk | [](https://lossless.cloud)
|
||||
TypeScript Support | [](https://lossless.cloud)
|
||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
Code Style | [](https://lossless.cloud)
|
||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||
|
||||
## Usage
|
||||
|
||||
## Contribution
|
||||
|
||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||
|
||||
|
||||
## Contribution
|
||||
|
||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||
|
||||
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)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
||||
[](https://maintainedby.lossless.com)
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
Header 1;Header 2; Header3
|
||||
row1data1; row1data2; row1data3
|
||||
row2data1; row2data2; row2data3
|
||||
Header 1;Header 2;"Header 3"
|
||||
"row1data1";"row1data2";"row1data3"
|
||||
"row2data1";row2data2;"row2data3"
|
||||
|
||||
|
@@ -1,19 +1,17 @@
|
||||
// tslint:disable-next-line:no-implicit-dependencies
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartcsv from '../ts/index';
|
||||
|
||||
// tslint:disable-next-line:no-implicit-dependencies
|
||||
import * as smartfile from '@pushrocks/smartfile';
|
||||
|
||||
let fileString: string;
|
||||
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');
|
||||
});
|
||||
|
||||
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();
|
||||
console.log(result);
|
||||
});
|
||||
@@ -21,7 +19,7 @@ tap.test('should create a valid csv', async () => {
|
||||
tap.test('should create a valid csv string', async () => {
|
||||
const createdCsvString = await smartcsv.Csv.createCsvStringFromArray([
|
||||
{ wow: 'hi', wow2: 'there' },
|
||||
{ wow: 'really', wow3: 'yes' }
|
||||
{ wow: 'really', wow3: 'yes' },
|
||||
]);
|
||||
console.log(createdCsvString);
|
||||
});
|
||||
|
||||
68
ts/index.ts
68
ts/index.ts
@@ -2,6 +2,8 @@ import * as plugins from './smartcsv.plugins';
|
||||
|
||||
export interface ICsvConstructorOptions {
|
||||
headers: boolean;
|
||||
unquote?: boolean;
|
||||
removeBomMarkers?: boolean;
|
||||
}
|
||||
|
||||
export class Csv {
|
||||
@@ -17,6 +19,7 @@ export class Csv {
|
||||
optionsArg: ICsvConstructorOptions
|
||||
): Promise<Csv> {
|
||||
const csvInstance = new Csv(csvStringArg, optionsArg);
|
||||
await csvInstance.exportAsObject();
|
||||
return csvInstance;
|
||||
}
|
||||
|
||||
@@ -51,21 +54,44 @@ export class Csv {
|
||||
public csvString: string;
|
||||
public headers: string[];
|
||||
public keyFrame: string = null;
|
||||
public data: any[];
|
||||
|
||||
private base64RecognitionPrefix = '####12345####';
|
||||
|
||||
public options: ICsvConstructorOptions = {
|
||||
headers: true
|
||||
headers: true,
|
||||
unquote: true,
|
||||
removeBomMarkers: true,
|
||||
};
|
||||
|
||||
constructor(csvStringArg: string, optionsArg: ICsvConstructorOptions) {
|
||||
this.csvString = csvStringArg;
|
||||
this.options = optionsArg;
|
||||
this.options = {
|
||||
...this.options,
|
||||
...optionsArg,
|
||||
};
|
||||
|
||||
let csvStringToParse = csvStringArg;
|
||||
if (this.options.removeBomMarkers) {
|
||||
csvStringToParse = csvStringToParse.replace(/^\uFEFF/, '');
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* determines the keyframe of the csv string
|
||||
*/
|
||||
public determineKeyframe() {
|
||||
private determineKeyframe() {
|
||||
let commaLength = 0;
|
||||
let semicolonLength = 0;
|
||||
const commaRegexResult = this.csvString.match(/,/g);
|
||||
@@ -87,7 +113,7 @@ export class Csv {
|
||||
/**
|
||||
* serializes the csv string
|
||||
*/
|
||||
public serializeCsvString() {
|
||||
private serializeCsvString() {
|
||||
const rowArray = this.getRows();
|
||||
const resultArray = [];
|
||||
if (this.options.headers) {
|
||||
@@ -100,7 +126,10 @@ export class Csv {
|
||||
return resultArray;
|
||||
}
|
||||
|
||||
public getRows(): string[] {
|
||||
/**
|
||||
* gets the rows of the csv
|
||||
*/
|
||||
private getRows(): string[] {
|
||||
const rowsArray = this.csvString.split('\n');
|
||||
if (rowsArray[rowsArray.length - 1] === '') {
|
||||
rowsArray.pop();
|
||||
@@ -108,20 +137,40 @@ export class Csv {
|
||||
return rowsArray;
|
||||
}
|
||||
|
||||
public getHeaders() {
|
||||
private getHeaders() {
|
||||
const rowArray = this.getRows();
|
||||
if (this.options.headers) {
|
||||
let headerRow = rowArray[0];
|
||||
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;
|
||||
}
|
||||
|
||||
public createDataObject(dataArray: string[]) {
|
||||
private createDataObject(dataArray: string[]) {
|
||||
const neededIterations = dataArray.length;
|
||||
let resultJson: any = {};
|
||||
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;
|
||||
}
|
||||
@@ -132,6 +181,7 @@ export class Csv {
|
||||
for (const dataArray of serializedData) {
|
||||
dataObjects.push(this.createDataObject(dataArray));
|
||||
}
|
||||
this.data = 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