txt-file-to-json
Advanced tools
Comparing version 2.0.2 to 3.0.1
{ | ||
"name": "txt-file-to-json", | ||
"version": "2.0.2", | ||
"version": "3.0.1", | ||
"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", |
@@ -87,1 +87,22 @@ <h1 align="center">txt-file-to-json</h1> | ||
``` | ||
Use `noOfRecords` key along with `filePath` or `data` key to get data of `specific number of rows`. | ||
```javascript | ||
const txtToJson = require("txt-file-to-json"); | ||
const dataInJSON = txtToJSON({ filePath: "./filePath.txt", noOfRecords: 1 }); | ||
``` | ||
#### Sample output when `noOfRecords` is `1` (json data) : | ||
``` | ||
[ | ||
{ | ||
FIRST_NAME: 'Debra', | ||
LAST_NAME: 'Burks', | ||
NUMBER: '880012XXXX', | ||
EMAIL: 'debra.burks@yahoo.com', | ||
ADDRESS: '9273 Thome Ave., Orchard Park, NY - 14127' | ||
} | ||
] | ||
``` |
@@ -15,2 +15,5 @@ const CR = "\r"; | ||
const NOTVALIDARGMSG = "ERROR -->> No valid argument provided."; | ||
const INVALIDARGUMENTEXCEPTION = "InvalidArgumentException"; | ||
const FILENOTFOUNDEXCEPTION = "FileNotFoundException"; | ||
@@ -29,3 +32,5 @@ | ||
INVALIDDATAEXCEPTION, | ||
FILENOTFOUNDEXCEPTION | ||
FILENOTFOUNDEXCEPTION, | ||
NOTVALIDARGMSG, | ||
INVALIDARGUMENTEXCEPTION | ||
}; |
@@ -12,3 +12,5 @@ const { | ||
INVALIDDATAEXCEPTION, | ||
FILENOTFOUNDEXCEPTION | ||
FILENOTFOUNDEXCEPTION, | ||
NOTVALIDARGMSG, | ||
INVALIDARGUMENTEXCEPTION | ||
} = require("./constants"); | ||
@@ -34,3 +36,2 @@ | ||
let finalResult = []; | ||
data = data.slice(1); | ||
@@ -116,8 +117,21 @@ data.forEach(line => { | ||
const getData = function (params, fs) { | ||
if (params.filePath == null) { | ||
if (params.filePath == null && params.data == null) { | ||
console.error(NOTVALIDARGMSG); | ||
throw Error(INVALIDARGUMENTEXCEPTION); | ||
} | ||
if (params.filePath) { | ||
return readFile(params.filePath, fs); | ||
} else { | ||
return readData(params.data); | ||
} | ||
return readFile(params.filePath, fs); | ||
}; | ||
const getRequiredData = function (params, data) { | ||
let dataWithoutHeaders = data.slice(1); | ||
if (params.noOfRecords || params.noOfRecords == 0) { | ||
dataWithoutHeaders = dataWithoutHeaders.slice(0,params.noOfRecords); | ||
} | ||
return dataWithoutHeaders; | ||
} | ||
module.exports = { | ||
@@ -134,3 +148,4 @@ getStartPoints, | ||
readData, | ||
getData | ||
getData, | ||
getRequiredData | ||
}; |
@@ -24,2 +24,4 @@ const WS = " "; | ||
const SPLITTED_DATA_WITHOUT_HEADERS = SPLITTED_DATA.slice(1); | ||
const HEADERS_LINE = SPLITTED_DATA[0]; | ||
@@ -35,2 +37,4 @@ | ||
const NO_OF_RECORDS_PARAM = {noOfRecords:1}; | ||
const HEADERS = ["FIRST_NAME", "LAST_NAME", "NUMBER", "EMAIL", "ADDRESS"]; | ||
@@ -68,3 +72,5 @@ | ||
SPLITTED_DATA, | ||
SPLITTED_DATA_WITHOUT_HEADERS, | ||
HEADERS_LINE, | ||
NO_OF_RECORDS_PARAM, | ||
SPLITTED_HEADERS_LINE, | ||
@@ -71,0 +77,0 @@ LINE1, |
@@ -15,2 +15,4 @@ const {equal, deepEqual} = require("assert"); | ||
SPLITTED_HEADERS_LINE, | ||
SPLITTED_DATA_WITHOUT_HEADERS, | ||
NO_OF_RECORDS_PARAM, | ||
LINE1, | ||
@@ -32,2 +34,3 @@ LINE2, | ||
formatDataInArray, | ||
getRequiredData, | ||
getObject, | ||
@@ -215,3 +218,3 @@ getTrimmedValue, | ||
const expected = [OBJ_FOR_LINE_1, OBJ_FOR_LINE_2]; | ||
const actual = formatDataInArray(SPLITTED_DATA, START_POINTS, HEADERS); | ||
const actual = formatDataInArray(SPLITTED_DATA_WITHOUT_HEADERS, START_POINTS, HEADERS); | ||
@@ -222,2 +225,18 @@ deepEqual(actual, expected); | ||
describe("getRequiredData",function(){ | ||
it("should return required number of records if passed in the params", function(){ | ||
const expected = SPLITTED_DATA_WITHOUT_HEADERS.slice(0,1); | ||
const actual = getRequiredData(NO_OF_RECORDS_PARAM,SPLITTED_DATA); | ||
deepEqual(actual, expected); | ||
}) | ||
it("should return all the records if required number of records is not passed", function(){ | ||
const expected = SPLITTED_DATA_WITHOUT_HEADERS; | ||
const actual = getRequiredData(new Object(),SPLITTED_DATA); | ||
deepEqual(actual, expected); | ||
}) | ||
}) | ||
describe("getObject", function () { | ||
@@ -268,2 +287,6 @@ const expectedOutputForLine1 = { | ||
describe("getData", function () { | ||
it("should throw an error if no valid argument is passed", function() { | ||
expect(() => getData({})).to.throw(); | ||
}); | ||
it("should take data as string if filepath is not given", function () { | ||
@@ -270,0 +293,0 @@ const actual = getData({data: STRING_DATA}); |
const fs = require("fs"); | ||
const {ES} = require("./src/constants"); | ||
const {getStartPoints, getHeaders, formatDataInArray, getData} = require("./src/lib"); | ||
const {getStartPoints, getHeaders, formatDataInArray, getData, getRequiredData} = require("./src/lib"); | ||
@@ -10,3 +10,4 @@ const main = function (params) { | ||
const headers = getHeaders(data[0]); | ||
const finalResult = formatDataInArray(data, startPoints, headers); | ||
const requiredData = getRequiredData(params, data); | ||
const finalResult = formatDataInArray(requiredData, startPoints, headers); | ||
return finalResult; | ||
@@ -13,0 +14,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
21953
494
108