Skip to content

Commit

Permalink
Add a method to allow package usability when the csv is already in st…
Browse files Browse the repository at this point in the history
…ring format
  • Loading branch information
Austin Lowther authored and Antò committed Jun 25, 2019
1 parent de267e8 commit 7b96b58
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 75 deletions.
44 changes: 24 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
'use strict';
"use strict";

let csvToJson = require('./src/csvToJson.js');
let csvToJson = require("./src/csvToJson.js");

/**
* Prints a digit as Number type (for example 32 instead of '32')
*/
exports.formatValueByType = function () {
exports.formatValueByType = function() {
csvToJson.formatValueByType();
return this;
};

/**
* Defines the field delimiter which will be used to split the fields
*/
exports.fieldDelimiter = function (delimiter) {
exports.fieldDelimiter = function(delimiter) {
csvToJson.fieldDelimiter(delimiter);
return this;
};
Expand All @@ -24,14 +24,14 @@ exports.fieldDelimiter = function (delimiter) {
* @param {outputFileName} path/filename
*
*/
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);
};

/**
Expand All @@ -40,11 +40,15 @@ exports.generateJsonFileFromCsv = function (inputFileName, outputFileName) {
* @return {Array} Array of Object in json format
*
*/
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);
};

/**
Expand All @@ -54,6 +58,6 @@ exports.getJsonFromCsv = function (inputFileName) {
*
* @deprecated Use generateJsonFileFromCsv()
*/
exports.jsonToCsv = function (inputFileName, outputFileName) {
csvToJson.generateJsonFileFromCsv(inputFileName, outputFileName);
};
exports.jsonToCsv = function(inputFileName, outputFileName) {
csvToJson.generateJsonFileFromCsv(inputFileName, outputFileName);
};
113 changes: 58 additions & 55 deletions src/csvToJson.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,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();

0 comments on commit 7b96b58

Please sign in to comment.