convert-csv-to-json
Advanced tools
Comparing version 0.0.5 to 0.0.6
19
index.js
'use strict'; | ||
let csvToJson = require('./src/csvToJson.js'); | ||
/** | ||
@@ -9,5 +11,15 @@ * Parse .csv file and put its content into a file in json format. | ||
*/ | ||
let csvToJson = require('./src/csvToJson.js'); | ||
exports.formatValueByType = function () { | ||
csvToJson.formatValueByType(); | ||
return this; | ||
}; | ||
exports.generateJsonFileFromCsv = function (inputFileName, outputFileName) { | ||
csvToJson.generateJsonFileFromCsv(inputFileName, outputFileName); | ||
if(!inputFileName){ | ||
throw new Error('inputFileName is not defined!!!'); | ||
} | ||
if(!outputFileName){ | ||
throw new Error('outpuFileName is not defined!!!'); | ||
} | ||
csvToJson.generateJsonFileFromCsv(this.inputFileName, this.outputFileName); | ||
}; | ||
@@ -22,2 +34,5 @@ | ||
exports.getJsonFromCsv = function (inputFileName) { | ||
if(!inputFileName){ | ||
throw new Error('inputFileName is not defined!!!'); | ||
} | ||
return csvToJson.getJsonFromCsv(inputFileName); | ||
@@ -24,0 +39,0 @@ }; |
{ | ||
"name": "convert-csv-to-json", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Convert CSV to JSON", | ||
@@ -38,2 +38,3 @@ "main": "index.js", | ||
"chai": "^4.0.2", | ||
"chai-fs": "^1.0.0", | ||
"gulp": "^3.9.1", | ||
@@ -40,0 +41,0 @@ "gulp-mocha": "^4.3.1", |
@@ -32,3 +32,3 @@ # CSVtoJSON [![Build Status](https://travis-ci.org/iuccio/csvToJson.svg?branch=master)](https://travis-ci.org/iuccio/csvToJson) | ||
"gender": "Male", | ||
"age": 96 | ||
"age": "96" | ||
}, | ||
@@ -40,3 +40,3 @@ { | ||
"gender": "Female", | ||
"age": 32 | ||
"age": "32" | ||
} | ||
@@ -67,4 +67,4 @@ ] | ||
let fileInputName = './myInputFile.csv'; | ||
let fileOutputName = './myOutputFile.json'; | ||
let fileInputName = 'myInputFile.csv'; | ||
let fileOutputName = 'myOutputFile.json'; | ||
@@ -82,4 +82,30 @@ csvToJson.generateJsonFileFromCsv(fileInputName,fileOutputName); | ||
``` | ||
#### Format property value by type | ||
If you want that a number will be printed as a Numbert type and not as a String type, use: | ||
```js | ||
csvToJson.formatValueByType().getJsonFromCsv(fileInputName) | ||
``` | ||
In this case the result will be: | ||
```json | ||
[ | ||
{ | ||
"first_name": "Constantin", | ||
"last_name": "Langsdon", | ||
"email": "clangsdon0@hc360.com", | ||
"gender": "Male", | ||
"age": 96 //instead of "96" | ||
}, | ||
{ | ||
"first_name": "Norah", | ||
"last_name": "Raison", | ||
"email": "nraison1@wired.com", | ||
"gender": "Female", | ||
"age": 32 //instead of "32" | ||
} | ||
] | ||
``` | ||
## License | ||
CSVtoJSON is licensed under the GNU General Public License v3.0 [License](LICENSE). |
@@ -12,2 +12,7 @@ 'use strict'; | ||
formatValueByType(){ | ||
this.printValueFormatByType = true; | ||
return this; | ||
} | ||
generateJsonFileFromCsv(fileInputName, fileOutputName) { | ||
@@ -37,9 +42,5 @@ let jsonStringified = this.getJsonFromCsvStringified(fileInputName); | ||
let currentLine = lines[i].split(fieldDelimiter); | ||
let jsonObject = {}; | ||
for (let j = 0; j < headers.length; j++) { | ||
let propertyName = stringUtils.trimPropertyName(headers[j]); | ||
let value = stringUtils.getValueFormatByType(currentLine[j]); | ||
jsonObject[propertyName] = value; | ||
if (stringUtils.hasContent(currentLine)) { | ||
jsonResult.push(this.buildJsonResult(headers,currentLine)); | ||
} | ||
jsonResult.push(jsonObject); | ||
} | ||
@@ -49,4 +50,17 @@ return jsonResult; | ||
buildJsonResult(headers,currentLine){ | ||
let jsonObject = {}; | ||
for (let j = 0; j < headers.length; j++) { | ||
let propertyName = stringUtils.trimPropertyName(headers[j]); | ||
let value = currentLine[j]; | ||
if(this.printValueFormatByType){ | ||
value = stringUtils.getValueFormatByType(currentLine[j]); | ||
} | ||
jsonObject[propertyName] = value; | ||
} | ||
return jsonObject; | ||
} | ||
} | ||
module.exports = new CsvToJson(); |
@@ -16,3 +16,14 @@ 'use strict'; | ||
} | ||
hasContent(values){ | ||
if(values.length >0){ | ||
for(let i=0; i<values.length;i++){ | ||
if(values[i]){ | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
module.exports = new StringUtils(); |
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
129028
20
108
6
154