Comparing version 0.0.18 to 0.0.19
module.exports = { | ||
maxSizeForDirectRead : 100, | ||
parseCSV : function(fileName, callBack) { | ||
parseCSV : function(fileName, callBack, considerFirstRowAsHeading) { | ||
if(typeof considerFirstRowAsHeading == "undefined") { | ||
considerFirstRowAsHeading = true; | ||
} | ||
var presentInstance = this; | ||
@@ -8,3 +11,3 @@ var fs = require('fs'); | ||
if(exists) { | ||
presentInstance.parseFile(fileName, callBack); | ||
presentInstance.parseFile(fileName, callBack, considerFirstRowAsHeading); | ||
} else { | ||
@@ -21,7 +24,8 @@ console.log("The provided file " + fileName + " doesn't exists or inaccessible"); | ||
var lineLength = line.length; | ||
for(var index=0; index<lineLength; index++) { | ||
var index=0; | ||
while(index<lineLength) { | ||
if(line[index]=='"') { | ||
var index2=index+1; | ||
var index2 = index+1; | ||
while(line[index2]!='"') { | ||
tempString += line[index2]; | ||
tempString+=line[index2]; | ||
index2++; | ||
@@ -31,14 +35,21 @@ } | ||
tempString = ""; | ||
index = index2 + 1; | ||
}else if(line[index] != ",") { | ||
index = index2+2; | ||
continue; | ||
} | ||
if(line[index]!=",") { | ||
tempString += line[index]; | ||
} else { | ||
index++; continue; | ||
} | ||
if(line[index]==",") { | ||
dataArray.push(tempString); | ||
tempString = ""; | ||
index++;continue; | ||
} | ||
} | ||
dataArray.push(tempString); | ||
return dataArray; | ||
}, | ||
parseFile : function(fileName, callBack) { | ||
parseFile : function(fileName, callBack, considerFirstRowAsHeading) { | ||
var presentObject = module.exports; | ||
@@ -61,12 +72,13 @@ var lblReader = require('line-by-line'); | ||
tempAttributeNameArray = line.split(","); | ||
if(tempAttributeNameArray.length == 1) { | ||
tempDataArray.push(line); | ||
if(!considerFirstRowAsHeading) { | ||
if(tempAttributeNameArray.length == 1) { | ||
tempDataArray.push(line); | ||
} else { | ||
tempDataArray.push(tempAttributeNameArray); | ||
} | ||
} | ||
tempLineCounter = 1; | ||
} else { | ||
if(tempAttributeNameArray.length == 1) { | ||
tempDataArray.push(line); | ||
} else { | ||
tempDataArray.push(presentObject.buildOutputData(tempAttributeNameArray, line)); | ||
} | ||
tempDataArray.push(presentObject.buildOutputData(tempAttributeNameArray, line, considerFirstRowAsHeading)); | ||
} | ||
@@ -86,6 +98,12 @@ readStream.resume(); | ||
buildOutputData : function(tempAttributeNameArray, line) { | ||
buildOutputData : function(tempAttributeNameArray, line, considerFirstRowAsHeading) { | ||
var presentObject = module.exports; | ||
var dataArray = presentObject.getDataFromLine(line); | ||
var dataArray = presentObject.getDataFromLine(line); | ||
if(!considerFirstRowAsHeading) { | ||
if(tempAttributeNameArray.length == 1) { | ||
return dataArray[0]; | ||
} else { | ||
return dataArray; | ||
} | ||
} else { | ||
var tempObject = {}; | ||
@@ -97,6 +115,5 @@ var tempAttributeNameArrayLength = tempAttributeNameArray.length; | ||
return tempObject; | ||
} | ||
} | ||
} |
{ | ||
"name": "csv-array", | ||
"version": "0.0.18", | ||
"version": "0.0.19", | ||
"description": "Intelligent CSV parser created and made for nodeJS. Which takes a csv file and produce an array from it.", | ||
@@ -5,0 +5,0 @@ "main": "csv-array.js", |
131
README.md
@@ -8,13 +8,9 @@ #csv-array | ||
## Change log | ||
* Performence improvement | ||
* Removed dependency of fs | ||
* Streaming improvement by adding a tiny delay, it will increase execution time but also increase stability | ||
* Bug fix of parsing | ||
* Fixed a bug in file containg single coloumn | ||
* Empty string if null | ||
* Reduced number of variables used | ||
* Added option to "considerFirstRowAsHeading". See Usage Guide for details about using | ||
* Bug fix in data parsing | ||
* Truncation of data problem solved | ||
## Usage Guide | ||
### Installing | ||
@@ -30,8 +26,24 @@ | ||
``` | ||
parseCSV("CSV-file-name.csv", callBack) | ||
//where callBack is a function having the argument data | ||
//which is an array structure of the CSV file | ||
parseCSV("CSV-file-name.csv", callBack, considerFirstRowAsHeading) | ||
/* | ||
Where callBack is the method which have the output array as argument, and you can do | ||
anything you like inside the function with the array | ||
"considerFirstRowAsHeading" is a configuration variable which holds "true" value | ||
by default. If it is true or nothing then the first row of the csv data will be considered | ||
as heading and the out put data will use the first row's content as attribute names. | ||
If it is "false" then all of the rows of the file will be returned as array. | ||
See example below. | ||
*/ | ||
``` | ||
### Example | ||
test.csv file contains | ||
``` | ||
Question Statement,Option 1,Option 2,Option 3,Option 4,Option 5,Answer,Deficulty,Category | ||
this is a test question answer it?,answer 1,answer 2,answer3,answer 4,,answer 2,3,test | ||
this is another test question answer it?,"answer1,answer2","answer2,answer3","answer4,answer5","answer5,answer6","answer7,answer8","answer1,answer2",2,test | ||
``` | ||
```javascript | ||
@@ -43,11 +55,90 @@ var csv = require('csv-array'); | ||
``` | ||
If the test.csv file contains something like this | ||
Output | ||
```json | ||
[ | ||
{ | ||
"Question Statement":"this is a test question answer it?", | ||
"Option 1":"answer 1", | ||
"Option 2":"answer 2", | ||
"Option 3":"answer3", | ||
"Option 4":"answer 4", | ||
"Option 5":"", | ||
"Answer":"answer 2", | ||
"Deficulty":"3", | ||
"Category":"test" | ||
}, | ||
{ | ||
"Question Statement":"this is another test question answer it?", | ||
"Option 1":"answer1,answer2", | ||
"Option 2":"answer2,answer3", | ||
"Option 3":"answer4,answer5", | ||
"Option 4":"answer5,answer6", | ||
"Option 5":"answer7,answer8", | ||
"Answer":"answer1,answer2", | ||
"Deficulty":"2", | ||
"Category":"test" | ||
} | ||
] | ||
``` | ||
Question Statement,Option 1,Option 2,Option 3,Option 4,Option 5,Answer,Deficulty,Category | ||
this is a test question answer it?,answer 1,answer 2,answer3,answer 4,,answer 2,3,test | ||
this is another test question answer it?,"answer1,answer2","answer2,answer3","answer4,answer5","answer5,answer6","answer7,answer8","answer1,answer2",2,test | ||
```javascript | ||
var csv = require('csv-array'); | ||
csv.parseCSV("test.csv", function(data){ | ||
console.log(JSON.stringify(data)); | ||
}, false); | ||
``` | ||
Output | ||
```json | ||
[ | ||
[ | ||
"Question Statement", | ||
"Option 1", | ||
"Option 2", | ||
"Option 3", | ||
"Option 4", | ||
"Option 5", | ||
"Answer", | ||
"Deficulty", | ||
"Category" | ||
], | ||
[ | ||
"this is a test question answer it?", | ||
"answer 1", | ||
"answer 2", | ||
"answer3", | ||
"answer 4", | ||
"", | ||
"answer 2", | ||
"3", | ||
"test" | ||
], | ||
[ | ||
"this is another test question answer it?", | ||
"answer1,answer2", | ||
"answer2,answer3", | ||
"answer4,answer5", | ||
"answer5,answer6", | ||
"answer7,answer8", | ||
"answer1,answer2", | ||
"2", | ||
"test" | ||
] | ||
] | ||
``` | ||
Then the resulting data is as follows | ||
```javascript | ||
var csv = require('csv-array'); | ||
csv.parseCSV("test.csv", function(data){ | ||
console.log(JSON.stringify(data)); | ||
}, true); | ||
/* | ||
The output will be as same as | ||
var csv = require('csv-array'); | ||
csv.parseCSV("test.csv", function(data){ | ||
console.log(JSON.stringify(data)); | ||
}); | ||
*/ | ||
``` | ||
Output | ||
```json | ||
@@ -63,3 +154,4 @@ [ | ||
"Answer":"answer 2", | ||
"Deficulty":"3" | ||
"Deficulty":"3", | ||
"Category":"test" | ||
}, | ||
@@ -74,5 +166,6 @@ { | ||
"Answer":"answer1,answer2", | ||
"Deficulty":"2" | ||
"Deficulty":"2", | ||
"Category":"test" | ||
} | ||
] | ||
``` |
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
8989
101
167