txt-file-to-json
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "txt-file-to-json", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Reads a txt file having a table and returns an array of obects. In which each object consists of all headers as keys and there data as values.", | ||
@@ -5,0 +5,0 @@ "main": "txt_to_json.js", |
@@ -1,2 +0,2 @@ | ||
<h1 align="center">txtToJson</h1> | ||
<h1 align="center">txt-file-to-json</h1> | ||
@@ -11,3 +11,3 @@ <div align="center"> | ||
```sh | ||
``` | ||
npm i --save txt-file-to-json | ||
@@ -27,3 +27,3 @@ ``` | ||
#### Sample txt file data : | ||
#### Sample input (txt data) : | ||
@@ -39,3 +39,3 @@ ``` | ||
#### Sample json data : | ||
#### Sample output (json data) : | ||
@@ -42,0 +42,0 @@ ``` |
const fs = require("fs"); | ||
const { CR, NL, WS, ES, FORMAT } = require("./constants.js"); | ||
const getStartPoints = function(firstLine) { | ||
let headers = firstLine.split(" "); | ||
let startPoints = [0]; | ||
const getHeadersAndStartPoints = function(data) { | ||
const line = data[0].split(ES); | ||
const startPoints = [0]; | ||
const headers = []; | ||
let header = ES; | ||
headers.reduce(function(acc, currentElement, index) { | ||
if (currentElement == "") { | ||
acc++; | ||
if (headers[index + 1] != "") { | ||
acc++; | ||
startPoints.push(acc); | ||
} | ||
} else { | ||
acc += currentElement.length; | ||
line.forEach((char, index) => { | ||
const charIsSpace = char == WS; | ||
const nextCharIsNotSpace = line[index + 1] != WS; | ||
const charIsNotSpace = char != WS; | ||
const charIsNotCR = char != CR; | ||
const charIsCR = char == CR; | ||
const previousCharIsNOtSpace = line[index - 1] != WS; | ||
if (charIsNotSpace && charIsNotCR) { | ||
return (header += char); | ||
} | ||
return acc; | ||
}, 0); | ||
return startPoints; | ||
if (charIsSpace && nextCharIsNotSpace) { | ||
startPoints.push(index + 1); | ||
headers.push(header); | ||
return (header = ES); | ||
} | ||
if (charIsCR && previousCharIsNOtSpace) { | ||
headers.push(header); | ||
header = ES; | ||
} | ||
}); | ||
startPoints.push(addEndPoint(data)); | ||
return { headers, startPoints }; | ||
}; | ||
const formatDataInArray = function(data, startPoints) { | ||
let headers = data[0].split(" ").filter(element => element); | ||
headers.pop(); | ||
const addEndPoint = function(data) { | ||
const lengths = []; | ||
data.forEach(line => { | ||
lengths.push(line.length); | ||
}); | ||
return Math.max(...lengths); | ||
}; | ||
const formatDataInArray = function(data, startPoints, headers) { | ||
let finalResult = []; | ||
@@ -41,5 +60,5 @@ data = data.slice(1); | ||
const txtToJson = function(filePath) { | ||
const data = fs.readFileSync(filePath, "utf8").split("\n"); | ||
const startPoints = getStartPoints(data[0]); | ||
const finalResult = formatDataInArray(data, startPoints); | ||
const data = fs.readFileSync(filePath, FORMAT).split(NL); | ||
const { headers, startPoints } = getHeadersAndStartPoints(data); | ||
const finalResult = formatDataInArray(data, startPoints, headers); | ||
return finalResult; | ||
@@ -46,0 +65,0 @@ }; |
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
5703
5
64