-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a method to allow package usability when the csv is already in st…
…ring format
- Loading branch information
Austin Lowther
authored and
Antò
committed
Jun 25, 2019
1 parent
de267e8
commit 7b96b58
Showing
2 changed files
with
82 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |