convert-csv-to-json
Advanced tools
Comparing version 1.2.0 to 1.3.0
12
index.js
@@ -18,4 +18,4 @@ "use strict"; | ||
*/ | ||
exports.formatValueByType = function () { | ||
csvToJson.formatValueByType(); | ||
exports.formatValueByType = function (active = true) { | ||
csvToJson.formatValueByType(active); | ||
return this; | ||
@@ -33,2 +33,10 @@ }; | ||
/** | ||
* Defines how to match and parse a sub array | ||
*/ | ||
exports.parseSubArray = function (delimiter, separator) { | ||
csvToJson.parseSubArray(delimiter, separator); | ||
return this; | ||
}; | ||
/** | ||
* Defines a custom encoding to decode a file | ||
@@ -35,0 +43,0 @@ */ |
{ | ||
"name": "convert-csv-to-json", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Convert CSV to JSON", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# CSVtoJSON | ||
[![Build Status](https://travis-ci.org/iuccio/csvToJson.svg?branch=master)](https://travis-ci.org/iuccio/csvToJson) [![Code Climate](https://codeclimate.com/github/iuccio/csvToJson/badges/gpa.svg)](https://codeclimate.com/github/iuccio/csvToJson) | ||
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/iuccio/csvToJson/Node%20CI) | ||
![GitHub branch checks state](https://img.shields.io/github/checks-status/iuccio/csvToJson/master) | ||
[![Code Climate](https://codeclimate.com/github/iuccio/csvToJson/badges/gpa.svg)](https://codeclimate.com/github/iuccio/csvToJson) | ||
[![NPM Version](https://img.shields.io/npm/v/convert-csv-to-json.svg)](https://npmjs.org/package/convert-csv-to-json) | ||
@@ -15,2 +17,3 @@ [![Downloads](https://img.shields.io/npm/dm/convert-csv-to-json.svg)](https://npmjs.org/package/convert-csv-to-json) | ||
* [Generate Array of Object in JSON format](#generate-array-of-object-in-json-format) | ||
* [Generate Object with sub array](#generate-object-with-sub-array) | ||
* [Define field delimiter](#define-field-delimiter) | ||
@@ -104,5 +107,32 @@ * [Format property value by type](#format-property-value-by-type) | ||
``` | ||
#### Generate Object with sub array | ||
``` | ||
firstName;lastName;email;gender;age;birth;sons | ||
Constantin;Langsdon;clangsdon0@hc360.com;Male;96;10.02.1965;*diego,marek,dries* | ||
``` | ||
Given the above CSV example, to generate a JSON Object with properties that contains sub Array, like the property **sons** | ||
with the values <b>*diego,marek,dries*</b> you have to call the function ```parseSubArray(delimiter, separator)``` . | ||
To generate the JSON Object with sub array from the above CSV example: | ||
```js | ||
csvToJson.parseSubArray('*',',').getJsonFromCsv('myInputFile.csv'); | ||
``` | ||
The result will be: | ||
```json | ||
[ | ||
{ | ||
"firstName": "Constantin", | ||
"lastName": "Langsdon", | ||
"email": "clangsdon0@hc360.com", | ||
"gender": "Male", | ||
"age": "96", | ||
"birth": "10.02.1965", | ||
"sons": ["diego","marek","dries"] | ||
} | ||
] | ||
``` | ||
#### Define field delimiter | ||
As default the filed delimiter is the **semicolon** (**;**). You can define another field delimiter | ||
by call the function ```fieldDelimiter(myDilimiter)```. | ||
by call the function ```fieldDelimiter(myDelimiter)```. | ||
If you want that the field delimiter is a **~**: | ||
@@ -114,3 +144,2 @@ | ||
#### Format property value by type | ||
@@ -121,3 +150,3 @@ 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: | ||
``` | ||
In this case the result will be: | ||
For example: | ||
@@ -124,0 +153,0 @@ ```json |
@@ -12,4 +12,4 @@ "use strict"; | ||
formatValueByType() { | ||
this.printValueFormatByType = true; | ||
formatValueByType(active) { | ||
this.printValueFormatByType = active; | ||
return this; | ||
@@ -23,2 +23,7 @@ } | ||
parseSubArray(delimiter = '*',separator = ',') { | ||
this.parseSubArrayDelimiter = delimiter; | ||
this.parseSubArraySeparator = separator; | ||
} | ||
encoding(encoding){ | ||
@@ -76,7 +81,12 @@ this.encoding = encoding; | ||
let propertyName = stringUtils.trimPropertyName(headers[j]); | ||
let value = currentLine[j]; | ||
let value = currentLine[j]; | ||
if (this.printValueFormatByType) { | ||
if(this.isParseSubArray(value)){ | ||
value = this.buildJsonSubArray(value); | ||
} | ||
if (this.printValueFormatByType && !Array.isArray(value)) { | ||
value = stringUtils.getValueFormatByType(currentLine[j]); | ||
} | ||
jsonObject[propertyName] = value; | ||
@@ -87,4 +97,28 @@ } | ||
buildJsonSubArray(value) { | ||
let extractedValues = value.substring( | ||
value.indexOf(this.parseSubArrayDelimiter) + 1, | ||
value.lastIndexOf(this.parseSubArrayDelimiter) | ||
); | ||
extractedValues.trim(); | ||
value = extractedValues.split(this.parseSubArraySeparator); | ||
if(this.printValueFormatByType){ | ||
for(let i=0; i < value.length; i++){ | ||
value[i] = stringUtils.getValueFormatByType(value[i]); | ||
} | ||
} | ||
return value; | ||
} | ||
isParseSubArray(value){ | ||
if(this.parseSubArrayDelimiter){ | ||
if (value && (value.indexOf(this.parseSubArrayDelimiter) === 0 && value.lastIndexOf(this.parseSubArrayDelimiter) === (value.length - 1))) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
module.exports = new CsvToJson(); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
79753
285
241
19