convert-csv-to-json
Advanced tools
Comparing version 0.0.14 to 0.0.15
44
index.js
@@ -1,4 +0,4 @@ | ||
'use strict'; | ||
"use strict"; | ||
let csvToJson = require('./src/csvToJson.js'); | ||
let csvToJson = require("./src/csvToJson.js"); | ||
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
exports.formatValueByType = function () { | ||
exports.formatValueByType = function() { | ||
csvToJson.formatValueByType(); | ||
@@ -17,3 +17,3 @@ return this; | ||
*/ | ||
exports.fieldDelimiter = function (delimiter) { | ||
exports.fieldDelimiter = function(delimiter) { | ||
csvToJson.fieldDelimiter(delimiter); | ||
@@ -29,10 +29,10 @@ return this; | ||
*/ | ||
exports.generateJsonFileFromCsv = function (inputFileName, outputFileName) { | ||
if(!inputFileName){ | ||
throw new Error('inputFileName is not defined!!!'); | ||
} | ||
if(!outputFileName){ | ||
throw new Error('outputFileName is not defined!!!'); | ||
} | ||
csvToJson.generateJsonFileFromCsv(inputFileName, outputFileName); | ||
exports.generateJsonFileFromCsv = function(inputFileName, outputFileName) { | ||
if (!inputFileName) { | ||
throw new Error("inputFileName is not defined!!!"); | ||
} | ||
if (!outputFileName) { | ||
throw new Error("outputFileName is not defined!!!"); | ||
} | ||
csvToJson.generateJsonFileFromCsv(inputFileName, outputFileName); | ||
}; | ||
@@ -46,9 +46,13 @@ | ||
*/ | ||
exports.getJsonFromCsv = function (inputFileName) { | ||
if(!inputFileName){ | ||
throw new Error('inputFileName is not defined!!!'); | ||
} | ||
return csvToJson.getJsonFromCsv(inputFileName); | ||
exports.getJsonFromCsv = function(inputFileName) { | ||
if (!inputFileName) { | ||
throw new Error("inputFileName is not defined!!!"); | ||
} | ||
return csvToJson.getJsonFromCsv(inputFileName); | ||
}; | ||
exports.csvStringToJson = function(csvString) { | ||
return csvStringToJson(csvString); | ||
}; | ||
/** | ||
@@ -61,4 +65,4 @@ * Parses .csv file and put its content into a file in json format. | ||
*/ | ||
exports.jsonToCsv = function (inputFileName, outputFileName) { | ||
csvToJson.generateJsonFileFromCsv(inputFileName, outputFileName); | ||
}; | ||
exports.jsonToCsv = function(inputFileName, outputFileName) { | ||
csvToJson.generateJsonFileFromCsv(inputFileName, outputFileName); | ||
}; |
{ | ||
"name": "convert-csv-to-json", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "Convert CSV to JSON", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,76 +0,79 @@ | ||
'use strict'; | ||
"use strict"; | ||
let fileUtils = require('././util/fileUtils'); | ||
let stringUtils = require('././util/stringUtils'); | ||
let jsonUtils = require('././util/jsonUtils'); | ||
let fileUtils = require("././util/fileUtils"); | ||
let stringUtils = require("././util/stringUtils"); | ||
let jsonUtils = require("././util/jsonUtils"); | ||
const newLine = /\r?\n/; | ||
const defaultFieldDelimiter = ';'; | ||
const defaultFieldDelimiter = ";"; | ||
class CsvToJson { | ||
formatValueByType() { | ||
this.printValueFormatByType = true; | ||
return this; | ||
} | ||
formatValueByType() { | ||
this.printValueFormatByType = true; | ||
return this; | ||
} | ||
fieldDelimiter(delimieter) { | ||
this.delimiter = delimieter; | ||
return this; | ||
} | ||
fieldDelimiter(delimieter){ | ||
this.delimiter = delimieter; | ||
return this; | ||
} | ||
generateJsonFileFromCsv(fileInputName, fileOutputName) { | ||
let jsonStringified = this.getJsonFromCsvStringified(fileInputName); | ||
fileUtils.writeFile(jsonStringified, fileOutputName); | ||
} | ||
generateJsonFileFromCsv(fileInputName, fileOutputName) { | ||
let jsonStringified = this.getJsonFromCsvStringified(fileInputName); | ||
fileUtils.writeFile(jsonStringified, fileOutputName); | ||
} | ||
getJsonFromCsvStringified(fileInputName) { | ||
let json = this.getJsonFromCsv(fileInputName); | ||
let jsonStringified = JSON.stringify(json, undefined, 1); | ||
jsonUtils.validateJson(jsonStringified); | ||
return jsonStringified; | ||
} | ||
getJsonFromCsvStringified(fileInputName) { | ||
let json = this.getJsonFromCsv(fileInputName); | ||
let jsonStringified = JSON.stringify(json, undefined, 1); | ||
jsonUtils.validateJson(jsonStringified); | ||
return jsonStringified; | ||
} | ||
getJsonFromCsv(fileInputName) { | ||
let parsedCsv = fileUtils.readFile(fileInputName); | ||
return this.csvToJson(parsedCsv); | ||
} | ||
getJsonFromCsv(fileInputName) { | ||
let parsedCsv = fileUtils.readFile(fileInputName); | ||
return this.csvToJson(parsedCsv); | ||
} | ||
csvStringToJson(csvString) { | ||
return this.csvToJson(csvString); | ||
} | ||
csvToJson(parsedCsv) { | ||
let lines = parsedCsv.split(newLine); | ||
let fieldDelimiter = this.getFieldDelimiter(); | ||
let headers = lines[0].split(fieldDelimiter); | ||
csvToJson(parsedCsv) { | ||
let lines = parsedCsv.split(newLine); | ||
let fieldDelimiter = this.getFieldDelimiter(); | ||
let headers = lines[0].split(fieldDelimiter); | ||
let jsonResult = []; | ||
for (let i = 1; i < lines.length; i++) { | ||
let currentLine = lines[i].split(fieldDelimiter); | ||
if (stringUtils.hasContent(currentLine)) { | ||
jsonResult.push(this.buildJsonResult(headers, currentLine)); | ||
} | ||
} | ||
return jsonResult; | ||
let jsonResult = []; | ||
for (let i = 1; i < lines.length; i++) { | ||
let currentLine = lines[i].split(fieldDelimiter); | ||
if (stringUtils.hasContent(currentLine)) { | ||
jsonResult.push(this.buildJsonResult(headers, currentLine)); | ||
} | ||
} | ||
return jsonResult; | ||
} | ||
getFieldDelimiter(){ | ||
if(this.delimiter){ | ||
return this.delimiter; | ||
} | ||
return defaultFieldDelimiter; | ||
getFieldDelimiter() { | ||
if (this.delimiter) { | ||
return this.delimiter; | ||
} | ||
return defaultFieldDelimiter; | ||
} | ||
buildJsonResult(headers, currentLine) { | ||
let jsonObject = {}; | ||
for (let j = 0; j < headers.length; j++) { | ||
let propertyName = stringUtils.trimPropertyName(headers[j]); | ||
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; | ||
let value = currentLine[j]; | ||
if (this.printValueFormatByType) { | ||
value = stringUtils.getValueFormatByType(currentLine[j]); | ||
} | ||
jsonObject[propertyName] = value; | ||
} | ||
return jsonObject; | ||
} | ||
} | ||
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
183
144691