convert-csv-to-json 1.4.0 → 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/.github/dependabot.yml +10 -0
- package/.github/workflows/codeql-analysis.yml +4 -4
- package/.github/workflows/nodejs.yml +2 -2
- package/.idea/csvToJson.iml +9 -0
- package/.idea/misc.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/vcs.xml +6 -0
- package/README.md +37 -19
- package/SECURITY.md +9 -0
- package/index.js +8 -0
- package/package.json +2 -2
- package/src/csvToJson.js +26 -4
|
@@ -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
|
|
@@ -12,9 +12,9 @@ jobs:
|
|
|
12
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
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<module type="JAVA_MODULE" version="4">
|
|
3
|
+
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
|
4
|
+
<exclude-output />
|
|
5
|
+
<content url="file://$MODULE_DIR$" />
|
|
6
|
+
<orderEntry type="inheritedJdk" />
|
|
7
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
|
8
|
+
</component>
|
|
9
|
+
</module>
|
package/.idea/misc.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<project version="4">
|
|
3
|
+
<component name="ProjectModuleManager">
|
|
4
|
+
<modules>
|
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/csvToJson.iml" filepath="$PROJECT_DIR$/.idea/csvToJson.iml" />
|
|
6
|
+
</modules>
|
|
7
|
+
</component>
|
|
8
|
+
</project>
|
package/.idea/vcs.xml
ADDED
package/README.md
CHANGED
|
@@ -1,31 +1,39 @@
|
|
|
1
1
|
# CSVtoJSON
|
|
2
|
-
](https://github.com/iuccio/csvToJson/actions?query=workflow%3ACodeQL)
|
|
5
|
-
[](https://codeclimate.com/github/iuccio/csvToJson)
|
|
2
|
+

|
|
3
|
+

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

|
|
8
8
|
[](https://npmjs.org/package/convert-csv-to-json)
|
|
9
9
|
|
|
10
10
|
**This project is not dependent on others packages or libraries.**
|
|
11
11
|
|
|
12
|
+
|
|
12
13
|
## Table of Contents
|
|
13
|
-
1. [Description](#description)
|
|
14
|
-
1. [Prerequisites](#prerequisites)
|
|
15
|
-
1. [Install npm *convert-csv-to-json package*](#install-npm-convert-csv-to-json-package)
|
|
16
|
-
1. [Usage](#usage)
|
|
17
|
-
* [Generate JSON file](#generate-json-file)
|
|
18
|
-
* [Generate Array of Object in JSON format](#generate-array-of-object-in-json-format)
|
|
19
|
-
* [Generate Object with sub array](#generate-object-with-sub-array)
|
|
20
|
-
* [Define field delimiter](#define-field-delimiter)
|
|
21
|
-
* [Format property value by type](#format-property-value-by-type)
|
|
22
|
-
* [Number](#number)
|
|
23
|
-
* [Boolean](#boolean)
|
|
24
|
-
* [Encoding](#encoding)
|
|
25
|
-
1. [Development](#development)
|
|
26
|
-
1. [License](#license)
|
|
27
|
-
1. [Buy me a Coffee](#buy-me-a-coffee)
|
|
28
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 -->
|
|
29
37
|
|
|
30
38
|
## Description
|
|
31
39
|
Converts *csv* files to *JSON* files with Node.js.
|
|
@@ -142,6 +150,16 @@ E.g. if your field delimiter is the comma **,** then:
|
|
|
142
150
|
csvToJson.fieldDelimiter(',').getJsonFromCsv(fileInputName);
|
|
143
151
|
```
|
|
144
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
|
+
|
|
145
163
|
#### Format property value by type
|
|
146
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:
|
|
147
165
|
```js
|
package/SECURITY.md
CHANGED
|
@@ -14,9 +14,18 @@ A generated Github Depedabot PR is normally quickly merged and a new version is
|
|
|
14
14
|
|
|
15
15
|
| Version | Supported |
|
|
16
16
|
| ------- | ------------------ |
|
|
17
|
+
| 1.4.0 | :white_check_mark: |
|
|
17
18
|
| 1.3.3 | :white_check_mark: |
|
|
18
19
|
| 1.3.2 | :white_check_mark: |
|
|
19
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
|
+
|
|
20
29
|
## Reporting a Vulnerability
|
|
21
30
|
|
|
22
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": {
|
|
@@ -37,6 +37,6 @@
|
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"chai": "^4.3.7",
|
|
39
39
|
"chai-fs": "^2.0.0",
|
|
40
|
-
"mocha": "^10.
|
|
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++) {
|