Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a9ee39f92b | |||
| ddfa5bfabc | |||
| e85751a90d | |||
| e6100e5bfc | |||
| 85ed6e8172 | |||
| d7c1677c9d | |||
| c4194bf037 | |||
| fd0abaeb47 | |||
| ab1c84f41f | |||
| 6f575e8516 | |||
| d11f237221 | |||
| 16826ed337 | |||
| c49b518b8a | |||
| 5f49532fc2 | |||
| e7f06a7f44 | |||
| 491477da82 | |||
| 935109abb6 | |||
| ea26a1d8ef | |||
| c7324f0240 | |||
| be44a1354b |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -15,8 +15,6 @@ node_modules/
|
|||||||
|
|
||||||
# builds
|
# builds
|
||||||
dist/
|
dist/
|
||||||
dist_web/
|
dist_*/
|
||||||
dist_serve/
|
|
||||||
dist_ts_web/
|
|
||||||
|
|
||||||
# custom
|
# custom
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
# gitzone standard
|
# gitzone ci_default
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
- .npmci_cache/
|
- .npmci_cache/
|
||||||
key: "$CI_BUILD_STAGE"
|
key: '$CI_BUILD_STAGE'
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- security
|
- security
|
||||||
@@ -12,45 +12,41 @@ stages:
|
|||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- npm install -g @shipzone/npmci
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
# ====================
|
# ====================
|
||||||
mirror:
|
auditProductionDependencies:
|
||||||
stage: security
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
script:
|
|
||||||
- npmci git mirror
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
snyk:
|
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci command npm install -g snyk
|
- npmci command npm install --production --ignore-scripts
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
- npmci command snyk test
|
- npmci command npm audit --audit-level=high --only=prod --production
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- 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
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
|
|
||||||
testLTS:
|
testStable:
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci node install lts
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
testSTABLE:
|
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
@@ -60,7 +56,17 @@ testSTABLE:
|
|||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
testBuild:
|
||||||
|
stage: test
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci node install stable
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command npm run build
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
@@ -70,6 +76,7 @@ release:
|
|||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
@@ -78,20 +85,15 @@ release:
|
|||||||
# ====================
|
# ====================
|
||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
image: docker:stable
|
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
services:
|
only:
|
||||||
- docker:stable-dind
|
- tags
|
||||||
script:
|
script:
|
||||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
- npmci command npm install -g typescript
|
||||||
- docker run
|
- npmci npm prepare
|
||||||
--env SOURCE_CODE="$PWD"
|
- npmci npm install
|
||||||
--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:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- priv
|
- priv
|
||||||
|
|
||||||
@@ -102,18 +104,19 @@ trigger:
|
|||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
- npmci node install stable
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command tsdoc
|
- npmci command npm run buildDocs
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
only:
|
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,11 +4,12 @@
|
|||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
},
|
},
|
||||||
"gitzone": {
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "gitlab.com",
|
"githost": "gitlab.com",
|
||||||
"gitscope": "pushrocks",
|
"gitscope": "pushrocks",
|
||||||
"gitrepo": "smartcsv",
|
"gitrepo": "smartcsv",
|
||||||
"shortDescription": "handle csv data | gitzone standard compliant",
|
"description": "handle csv data | gitzone standard compliant",
|
||||||
"npmPackagename": "@pushrocks/smartcsv",
|
"npmPackagename": "@pushrocks/smartcsv",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
|
|||||||
1481
package-lock.json
generated
1481
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
43
package.json
43
package.json
@@ -1,26 +1,43 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartcsv",
|
"name": "@pushrocks/smartcsv",
|
||||||
"version": "1.0.16",
|
"version": "2.0.2",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "handle csv data | gitzone standard compliant",
|
"description": "handle csv data | gitzone standard compliant",
|
||||||
"main": "dist/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
|
"type": "module",
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/)",
|
"test": "(tstest test/)",
|
||||||
"build": "(tsbuild)",
|
"build": "(tsbuild --allowimplicitany)",
|
||||||
"format": "(gitzone format)"
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.11",
|
"@gitzone/tsbuild": "^2.1.65",
|
||||||
"@gitzone/tsrun": "^1.2.6",
|
"@gitzone/tsrun": "^1.2.37",
|
||||||
"@gitzone/tstest": "^1.0.21",
|
"@gitzone/tstest": "^1.0.73",
|
||||||
"@pushrocks/smartfile": "^7.0.2",
|
"@pushrocks/smartfile": "^10.0.4",
|
||||||
"@pushrocks/tapbundle": "^3.0.9",
|
"@pushrocks/tapbundle": "^5.0.4",
|
||||||
"@types/node": "^12.0.2"
|
"@types/node": "^18.6.4"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartpromise": "^3.0.2"
|
"@pushrocks/smartpromise": "^3.1.7",
|
||||||
}
|
"@pushrocks/smartstring": "^4.0.2"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"ts/**/*",
|
||||||
|
"ts_web/**/*",
|
||||||
|
"dist/**/*",
|
||||||
|
"dist_*/**/*",
|
||||||
|
"dist_ts/**/*",
|
||||||
|
"dist_ts_web/**/*",
|
||||||
|
"assets/**/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
],
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 chrome versions"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
4331
pnpm-lock.yaml
generated
Normal file
4331
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
33
readme.md
33
readme.md
@@ -8,19 +8,34 @@ handle csv data | gitzone standard compliant
|
|||||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartcsv/)
|
* [docs (typedoc)](https://pushrocks.gitlab.io/smartcsv/)
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
[](https://gitlab.com/pushrocks/smartcsv/commits/master)
|
|
||||||
[](https://gitlab.com/pushrocks/smartcsv/commits/master)
|
Status Category | Status Badge
|
||||||
[](https://www.npmjs.com/package/@pushrocks/smartcsv)
|
-- | --
|
||||||
[](https://snyk.io/test/npm/@pushrocks/smartcsv)
|
GitLab Pipelines | [](https://lossless.cloud)
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
npm | [](https://lossless.cloud)
|
||||||
[](https://prettier.io/)
|
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
|
## 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.
|
For further information read the linked docs at the top of this readme.
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
> 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
|
Header 1;Header 2;"Header 3"
|
||||||
row1data1; row1data2; row1data3
|
"row1data1";"row1data2";"row1data3"
|
||||||
row2data1; row2data2; row2data3
|
"row2data1";row2data2;"row2data3"
|
||||||
|
|||||||
|
10
test/test.ts
10
test/test.ts
@@ -1,19 +1,17 @@
|
|||||||
// tslint:disable-next-line:no-implicit-dependencies
|
|
||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import * as smartcsv from '../ts/index';
|
import * as smartcsv from '../ts/index.js';
|
||||||
|
|
||||||
// tslint:disable-next-line:no-implicit-dependencies
|
|
||||||
import * as smartfile from '@pushrocks/smartfile';
|
import * as smartfile from '@pushrocks/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, unquote: true });
|
||||||
const result = await testCsv.exportAsObject();
|
const result = await testCsv.exportAsObject();
|
||||||
console.log(result);
|
console.log(result);
|
||||||
});
|
});
|
||||||
@@ -21,7 +19,7 @@ tap.test('should create a valid csv', async () => {
|
|||||||
tap.test('should create a valid csv string', async () => {
|
tap.test('should create a valid csv string', async () => {
|
||||||
const createdCsvString = await smartcsv.Csv.createCsvStringFromArray([
|
const createdCsvString = await smartcsv.Csv.createCsvStringFromArray([
|
||||||
{ wow: 'hi', wow2: 'there' },
|
{ wow: 'hi', wow2: 'there' },
|
||||||
{ wow: 'really', wow3: 'yes' }
|
{ wow: 'really', wow3: 'yes' },
|
||||||
]);
|
]);
|
||||||
console.log(createdCsvString);
|
console.log(createdCsvString);
|
||||||
});
|
});
|
||||||
|
|||||||
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@pushrocks/smartcsv',
|
||||||
|
version: '2.0.2',
|
||||||
|
description: 'handle csv data | gitzone standard compliant'
|
||||||
|
}
|
||||||
85
ts/index.ts
85
ts/index.ts
@@ -1,7 +1,10 @@
|
|||||||
import * as plugins from './smartcsv.plugins';
|
import * as plugins from './smartcsv.plugins.js';
|
||||||
|
|
||||||
export interface ICsvConstructorOptions {
|
export interface ICsvConstructorOptions {
|
||||||
headers: boolean;
|
headers: boolean;
|
||||||
|
unquote?: boolean;
|
||||||
|
trimSpace?: boolean;
|
||||||
|
removeBomMarkers?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Csv {
|
export class Csv {
|
||||||
@@ -17,6 +20,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 +55,48 @@ 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,
|
||||||
|
trimSpace: true,
|
||||||
|
removeBomMarkers: 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.removeBomMarkers) {
|
||||||
|
csvStringToParse = csvStringToParse.replace(/^\uFEFF/, '');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Quotes allow separators to be contained in quoted strings.
|
||||||
|
// To preserve them, when unquoting, we convert everything in between to base64 here
|
||||||
|
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 +118,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 +131,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 +142,50 @@ 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;
|
||||||
|
}
|
||||||
|
if (this.options.trimSpace) {
|
||||||
|
const trimmedHeaders = [];
|
||||||
|
for (const header of this.headers) {
|
||||||
|
trimmedHeaders.push(header.trim());
|
||||||
|
}
|
||||||
|
this.headers = trimmedHeaders;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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, '');
|
||||||
|
}
|
||||||
|
if (this.options.trimSpace) {
|
||||||
|
value = value.trim();
|
||||||
|
}
|
||||||
|
resultJson[this.headers[i]] = value;
|
||||||
}
|
}
|
||||||
return resultJson;
|
return resultJson;
|
||||||
}
|
}
|
||||||
@@ -132,6 +196,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 };
|
||||||
|
|||||||
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "ES2022",
|
||||||
|
"moduleResolution": "nodenext",
|
||||||
|
"esModuleInterop": true
|
||||||
|
}
|
||||||
|
}
|
||||||
17
tslint.json
17
tslint.json
@@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
|
||||||
"rules": {
|
|
||||||
"semicolon": [true, "always"],
|
|
||||||
"no-console": false,
|
|
||||||
"ordered-imports": false,
|
|
||||||
"object-literal-sort-keys": false,
|
|
||||||
"member-ordering": {
|
|
||||||
"options":{
|
|
||||||
"order": [
|
|
||||||
"static-method"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"defaultSeverity": "warning"
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user