Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

txt-file-to-json

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

txt-file-to-json - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

constants.js

2

package.json
{
"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 @@ };

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc