convert-csv-to-json 1.3.3 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.devcontainer/devcontainer.json +19 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +24 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +7 -0
- package/.github/dependabot.yml +10 -0
- package/.github/workflows/codeql-analysis.yml +4 -4
- package/.github/workflows/nodejs.yml +3 -3
- package/README.md +37 -17
- package/SECURITY.md +31 -0
- package/index.js +8 -0
- package/package.json +3 -3
- package/src/csvToJson.js +26 -4
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "Node.js",
|
|
3
|
+
"image": "mcr.microsoft.com/devcontainers/javascript-node:16-bullseye"
|
|
4
|
+
|
|
5
|
+
// Features to add to the dev container. More info: https://containers.dev/implementors/features.
|
|
6
|
+
// "features": {},
|
|
7
|
+
|
|
8
|
+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
|
9
|
+
// "forwardPorts": [],
|
|
10
|
+
|
|
11
|
+
// Use 'postCreateCommand' to run commands after the container is created.
|
|
12
|
+
// "postCreateCommand": "yarn install",
|
|
13
|
+
|
|
14
|
+
// Configure tool-specific properties.
|
|
15
|
+
// "customizations": {},
|
|
16
|
+
|
|
17
|
+
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
|
|
18
|
+
// "remoteUser": "root"
|
|
19
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Bug report
|
|
3
|
+
about: Create a report to help us improve
|
|
4
|
+
title: "[BUG]"
|
|
5
|
+
labels: ''
|
|
6
|
+
assignees: ''
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Expected Behavior
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## Actual Behavior
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## Steps to Reproduce the Problem
|
|
17
|
+
|
|
18
|
+
1.
|
|
19
|
+
1.
|
|
20
|
+
1.
|
|
21
|
+
|
|
22
|
+
## Specifications
|
|
23
|
+
|
|
24
|
+
- Version:
|
|
@@ -35,11 +35,11 @@ jobs:
|
|
|
35
35
|
|
|
36
36
|
steps:
|
|
37
37
|
- name: Checkout repository
|
|
38
|
-
uses: actions/checkout@
|
|
38
|
+
uses: actions/checkout@v3
|
|
39
39
|
|
|
40
40
|
# Initializes the CodeQL tools for scanning.
|
|
41
41
|
- name: Initialize CodeQL
|
|
42
|
-
uses: github/codeql-action/init@
|
|
42
|
+
uses: github/codeql-action/init@v2
|
|
43
43
|
with:
|
|
44
44
|
languages: ${{ matrix.language }}
|
|
45
45
|
# If you wish to specify custom queries, you can do so here or in a config file.
|
|
@@ -50,7 +50,7 @@ jobs:
|
|
|
50
50
|
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
|
51
51
|
# If this step fails, then you should remove it and run the build manually (see below)
|
|
52
52
|
- name: Autobuild
|
|
53
|
-
uses: github/codeql-action/autobuild@
|
|
53
|
+
uses: github/codeql-action/autobuild@v2
|
|
54
54
|
|
|
55
55
|
# ℹ️ Command-line programs to run using the OS shell.
|
|
56
56
|
# 📚 https://git.io/JvXDl
|
|
@@ -64,4 +64,4 @@ jobs:
|
|
|
64
64
|
# make release
|
|
65
65
|
|
|
66
66
|
- name: Perform CodeQL Analysis
|
|
67
|
-
uses: github/codeql-action/analyze@
|
|
67
|
+
uses: github/codeql-action/analyze@v2
|
|
@@ -9,12 +9,12 @@ jobs:
|
|
|
9
9
|
|
|
10
10
|
strategy:
|
|
11
11
|
matrix:
|
|
12
|
-
node-version: [
|
|
12
|
+
node-version: [12.x, 14.x, 16.x]
|
|
13
13
|
|
|
14
14
|
steps:
|
|
15
|
-
- uses: actions/checkout@
|
|
15
|
+
- uses: actions/checkout@v3
|
|
16
16
|
- name: Use Node.js ${{ matrix.node-version }}
|
|
17
|
-
uses: actions/setup-node@
|
|
17
|
+
uses: actions/setup-node@v3
|
|
18
18
|
with:
|
|
19
19
|
node-version: ${{ matrix.node-version }}
|
|
20
20
|
- name: npm install, build, and test
|
package/README.md
CHANGED
|
@@ -1,29 +1,39 @@
|
|
|
1
1
|
# CSVtoJSON
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+

|
|
3
|
+

|
|
4
|
+
[](https://snyk.io/test/github/iuccio/csvToJson)
|
|
4
5
|
[](https://codeclimate.com/github/iuccio/csvToJson)
|
|
5
6
|
[](https://npmjs.org/package/convert-csv-to-json)
|
|
7
|
+

|
|
6
8
|
[](https://npmjs.org/package/convert-csv-to-json)
|
|
7
9
|
|
|
8
10
|
**This project is not dependent on others packages or libraries.**
|
|
9
11
|
|
|
12
|
+
|
|
10
13
|
## Table of Contents
|
|
11
|
-
1. [Description](#description)
|
|
12
|
-
1. [Prerequisites](#prerequisites)
|
|
13
|
-
1. [Install npm *convert-csv-to-json package*](#install-npm-convert-csv-to-json-package)
|
|
14
|
-
1. [Usage](#usage)
|
|
15
|
-
* [Generate JSON file](#generate-json-file)
|
|
16
|
-
* [Generate Array of Object in JSON format](#generate-array-of-object-in-json-format)
|
|
17
|
-
* [Generate Object with sub array](#generate-object-with-sub-array)
|
|
18
|
-
* [Define field delimiter](#define-field-delimiter)
|
|
19
|
-
* [Format property value by type](#format-property-value-by-type)
|
|
20
|
-
* [Number](#number)
|
|
21
|
-
* [Boolean](#boolean)
|
|
22
|
-
* [Encoding](#encoding)
|
|
23
|
-
1. [Development](#development)
|
|
24
|
-
1. [License](#license)
|
|
25
|
-
1. [Buy me a Coffee](#buy-me-a-coffee)
|
|
26
14
|
|
|
15
|
+
<!-- toc -->
|
|
16
|
+
|
|
17
|
+
- [Description](#description)
|
|
18
|
+
- [Prerequisites](#prerequisites)
|
|
19
|
+
- [Install npm *convert-csv-to-json package*](#install-npm-convert-csv-to-json-package)
|
|
20
|
+
* [Install](#install)
|
|
21
|
+
* [Usage](#usage)
|
|
22
|
+
+ [Generate JSON file](#generate-json-file)
|
|
23
|
+
+ [Generate Array of Object in JSON format](#generate-array-of-object-in-json-format)
|
|
24
|
+
+ [Generate Object with sub array](#generate-object-with-sub-array)
|
|
25
|
+
+ [Define field delimiter](#define-field-delimiter)
|
|
26
|
+
+ [Index header](#index-header)
|
|
27
|
+
+ [Empty rows](#empty-rows)
|
|
28
|
+
+ [Format property value by type](#format-property-value-by-type)
|
|
29
|
+
- [Number](#number)
|
|
30
|
+
- [Boolean](#boolean)
|
|
31
|
+
+ [Encoding](#encoding)
|
|
32
|
+
- [Development](#development)
|
|
33
|
+
- [License](#license)
|
|
34
|
+
- [Buy me a Coffee](#buy-me-a-coffee)
|
|
35
|
+
|
|
36
|
+
<!-- tocstop -->
|
|
27
37
|
|
|
28
38
|
## Description
|
|
29
39
|
Converts *csv* files to *JSON* files with Node.js.
|
|
@@ -140,6 +150,16 @@ E.g. if your field delimiter is the comma **,** then:
|
|
|
140
150
|
csvToJson.fieldDelimiter(',').getJsonFromCsv(fileInputName);
|
|
141
151
|
```
|
|
142
152
|
|
|
153
|
+
#### Index header
|
|
154
|
+
If the header is not on the first line you can define the header index like:
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
csvToJson.indexHeader(3).getJsonFromCsv(fileInputName);
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
#### Empty rows
|
|
161
|
+
Empty rows are ignored and not parsed.
|
|
162
|
+
|
|
143
163
|
#### Format property value by type
|
|
144
164
|
If you want that a number will be printed as a Number type, and values *true* or *false* is printed as a boolean Type, use:
|
|
145
165
|
```js
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
[csvToJson](https://github.com/iuccio/csvToJson) is not dependent on others packages or libraries. The only dependencies that [csvToJson](https://github.com/iuccio/csvToJson) uses
|
|
4
|
+
are **devDependencies**.
|
|
5
|
+
This means that the prduced [npm package](https://www.npmjs.com/package/convert-csv-to-json) is never affected by dependency vulnerabilities.
|
|
6
|
+
|
|
7
|
+
For the **devDependencies** Github Dependabot is enabled and inform us when a security vulnerability affects [csvToJson](https://github.com/iuccio/csvToJson).
|
|
8
|
+
A generated Github Depedabot PR is normally quickly merged and a new version is released.
|
|
9
|
+
|
|
10
|
+
## Vulnerabities
|
|
11
|
+
[](https://snyk.io/test/github/iuccio/csvToJson)
|
|
12
|
+
|
|
13
|
+
## Supported Versions
|
|
14
|
+
|
|
15
|
+
| Version | Supported |
|
|
16
|
+
| ------- | ------------------ |
|
|
17
|
+
| 1.4.0 | :white_check_mark: |
|
|
18
|
+
| 1.3.3 | :white_check_mark: |
|
|
19
|
+
| 1.3.2 | :white_check_mark: |
|
|
20
|
+
|
|
21
|
+
### Dev Dependencies
|
|
22
|
+
|
|
23
|
+
| Version | Supported |
|
|
24
|
+
| ------- | ------------------ |
|
|
25
|
+
| 1.4.0 | :white_check_mark: |
|
|
26
|
+
| 1.3.3 | :warning: moderate dev dependencies vulnerability|
|
|
27
|
+
| 1.3.2 | :warning: moderate dev dependencies vulnerability|
|
|
28
|
+
|
|
29
|
+
## Reporting a Vulnerability
|
|
30
|
+
|
|
31
|
+
To report a Security Vulnerability please add an [Vulnerabilty Issue](https://github.com/iuccio/csvToJson/labels/vulnerabilty).
|
package/index.js
CHANGED
|
@@ -28,6 +28,14 @@ exports.fieldDelimiter = function (delimiter) {
|
|
|
28
28
|
return this;
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Defines the index where the header is defined
|
|
33
|
+
*/
|
|
34
|
+
exports.indexHeader = function (index) {
|
|
35
|
+
csvToJson.indexHeader(index);
|
|
36
|
+
return this;
|
|
37
|
+
};
|
|
38
|
+
|
|
31
39
|
/**
|
|
32
40
|
* Defines how to match and parse a sub array
|
|
33
41
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "convert-csv-to-json",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Convert CSV to JSON",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
},
|
|
36
36
|
"homepage": "https://github.com/iuccio/CSVtoJSON#readme",
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"chai": "^4.3.
|
|
38
|
+
"chai": "^4.3.7",
|
|
39
39
|
"chai-fs": "^2.0.0",
|
|
40
|
-
"mocha": "^
|
|
40
|
+
"mocha": "^10.2.0"
|
|
41
41
|
}
|
|
42
42
|
}
|
package/src/csvToJson.js
CHANGED
|
@@ -14,11 +14,20 @@ class CsvToJson {
|
|
|
14
14
|
return this;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
fieldDelimiter(
|
|
18
|
-
this.delimiter =
|
|
17
|
+
fieldDelimiter(delimiter) {
|
|
18
|
+
this.delimiter = delimiter;
|
|
19
19
|
return this;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
indexHeader(indexHeader) {
|
|
23
|
+
if(isNaN(indexHeader)){
|
|
24
|
+
throw new Error('The index Header must be a Number!');
|
|
25
|
+
}
|
|
26
|
+
this.indexHeader = indexHeader;
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
22
31
|
parseSubArray(delimiter = '*',separator = ',') {
|
|
23
32
|
this.parseSubArrayDelimiter = delimiter;
|
|
24
33
|
this.parseSubArraySeparator = separator;
|
|
@@ -53,10 +62,16 @@ class CsvToJson {
|
|
|
53
62
|
csvToJson(parsedCsv) {
|
|
54
63
|
let lines = parsedCsv.split(newLine);
|
|
55
64
|
let fieldDelimiter = this.getFieldDelimiter();
|
|
56
|
-
let
|
|
65
|
+
let index = this.getIndexHeader();
|
|
66
|
+
let headers = lines[index].split(fieldDelimiter);
|
|
67
|
+
|
|
68
|
+
while(!stringUtils.hasContent(headers) && index <= lines.length){
|
|
69
|
+
index = index + 1;
|
|
70
|
+
headers = lines[index].split(fieldDelimiter);
|
|
71
|
+
}
|
|
57
72
|
|
|
58
73
|
let jsonResult = [];
|
|
59
|
-
for (let i = 1; i < lines.length; i++) {
|
|
74
|
+
for (let i = (index + 1); i < lines.length; i++) {
|
|
60
75
|
let currentLine = lines[i].split(fieldDelimiter);
|
|
61
76
|
if (stringUtils.hasContent(currentLine)) {
|
|
62
77
|
jsonResult.push(this.buildJsonResult(headers, currentLine));
|
|
@@ -72,6 +87,13 @@ class CsvToJson {
|
|
|
72
87
|
return defaultFieldDelimiter;
|
|
73
88
|
}
|
|
74
89
|
|
|
90
|
+
getIndexHeader(){
|
|
91
|
+
if(this.indexHeader !== null && !isNaN(this.indexHeader)){
|
|
92
|
+
return this.indexHeader;
|
|
93
|
+
}
|
|
94
|
+
return 0;
|
|
95
|
+
}
|
|
96
|
+
|
|
75
97
|
buildJsonResult(headers, currentLine) {
|
|
76
98
|
let jsonObject = {};
|
|
77
99
|
for (let j = 0; j < headers.length; j++) {
|