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.2 to 1.0.3

lib.js

10

package.json
{
"name": "txt-file-to-json",
"version": "1.0.2",
"version": "1.0.3",
"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.",
"main": "txt_to_json.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "nyc --reporter=text mocha"
},

@@ -30,3 +30,7 @@ "repository": {

},
"homepage": "https://github.com/vprince001/txt_to_json#readme"
"homepage": "https://github.com/vprince001/txt_to_json#readme",
"dependencies": {
"mocha": "^6.1.4",
"nyc": "^14.1.1"
}
}
const fs = require("fs");
const { CR, NL, WS, ES, FORMAT } = require("./constants.js");
const { addEndPoint, getObject } = require("./lib.js");

@@ -11,13 +12,14 @@ const getHeadersAndStartPoints = function(data) {

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;
const isNotSpace = char != WS;
const isNotCR = char != CR;
const isSpace = char == WS;
const nextIsNotSpace = line[index + 1] != WS;
const isCR = char == CR;
const isPreviousNOtSpace = line[index - 1] != WS;
if (charIsNotSpace && charIsNotCR) {
if (isNotSpace && isNotCR) {
return (header += char);
}
if (charIsSpace && nextCharIsNotSpace) {
if (isSpace && nextIsNotSpace) {
startPoints.push(index + 1);

@@ -27,3 +29,4 @@ headers.push(header);

}
if (charIsCR && previousCharIsNOtSpace) {
if (isCR && isPreviousNOtSpace) {
headers.push(header);

@@ -37,10 +40,2 @@ header = ES;

const addEndPoint = function(data) {
const lengths = [];
data.forEach(line => {
lengths.push(line.length);
});
return Math.max(...lengths);
};
const formatDataInArray = function(data, startPoints, headers) {

@@ -51,9 +46,3 @@ let finalResult = [];

data.forEach(line => {
let obj = headers.reduce(function(acc, currentElement, index) {
acc[currentElement] = line
.substring(startPoints[index], startPoints[index + 1])
.trim();
return acc;
}, {});
finalResult.push(obj);
finalResult.push(getObject(line, headers, startPoints));
});

@@ -60,0 +49,0 @@ return finalResult;

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