Socket
Socket
Sign inDemoInstall

csv-array

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

csv-array - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

122

csv-array.js
module.exports = {
parseCSV : function(fileName, callBack) {
parseCSV : function(fileName, callBack, considerFirstRowAsHeading) {
if(typeof considerFirstRowAsHeading == "undefined") {
considerFirstRowAsHeading = true;
}
var presentInstance = this;

@@ -7,3 +11,3 @@ var fs = require('fs');

if(exists) {
presentInstance.parseFile(fileName, callBack);
presentInstance.parseFile(fileName, callBack, considerFirstRowAsHeading);
} else {

@@ -15,41 +19,13 @@ console.log("The provided file " + fileName + " doesn't exists or inaccessible");

parseFile : function(fileName, callBack) {
var presentInstance = this;
var fs = require('fs');
fs.readFile(fileName, 'utf-8', function(err, data) {
var dataArray = presentInstance.getDataSeparatedByNewLine(data);
var finalDataArray = presentInstance.getDataArray(dataArray);
callBack(finalDataArray);
})
},
getDataSeparatedByNewLine : function(data) {
var dataArray = data.split("\n");
return dataArray;
},
getDataArray : function(dataArray) {
presentInstance = this;
var attributeNameArray = dataArray[0].split(",");
var finalArray = [];
for(var index=1; index<dataArray.length; index++) {
var tempArray = {};
var dataList = presentInstance.getDataFromLine(dataArray[index]);
for(var index2=0; index2<attributeNameArray.length; index2++) {
tempArray[attributeNameArray[index2]] = dataList[index2];
}
finalArray.push(tempArray);
}
return finalArray;
},
// returns data from a single line
getDataFromLine : function(line) {
var dataArray = [];
var tempString="";
for(var index=0; index<line.length; index++) {
var lineLength = line.length;
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++;

@@ -59,13 +35,81 @@ }

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, considerFirstRowAsHeading) {
var presentObject = module.exports;
var lblReader = require('line-by-line');
var readStream = new lblReader(fileName);
var tempDataArray = [];
var tempAttributeNameArray = [];
var tempLineCounter = 0;
readStream.on('error', function(){
console.log("cannot read the file any more.");
});
readStream.on('line', function(line) {
readStream.pause();
if(tempLineCounter == 0) {
tempAttributeNameArray = line.split(",");
if(!considerFirstRowAsHeading) {
if(tempAttributeNameArray.length == 1) {
tempDataArray.push(line);
} else {
tempDataArray.push(tempAttributeNameArray);
}
}
tempLineCounter = 1;
} else {
tempDataArray.push(presentObject.buildOutputData(tempAttributeNameArray, line, considerFirstRowAsHeading));
}
readStream.resume();
});
readStream.on('end', function() {
if(tempDataArray.length == 0) {
tempDataArray = tempAttributeNameArray;
}
callBack(tempDataArray);
});
},
buildOutputData : function(tempAttributeNameArray, line, considerFirstRowAsHeading) {
var presentObject = module.exports;
var dataArray = presentObject.getDataFromLine(line);
if(!considerFirstRowAsHeading) {
if(tempAttributeNameArray.length == 1) {
return dataArray[0];
} else {
return dataArray;
}
} else {
var tempObject = {};
var tempAttributeNameArrayLength = tempAttributeNameArray.length;
for(var index=0; index<tempAttributeNameArrayLength; index++) {
tempObject[tempAttributeNameArray[index]] = ((typeof dataArray[index]!="undefined")?dataArray[index]:"");
}
return tempObject;
}
}
}
{
"name": "csv-array",
"version": "0.0.1",
"description": "This is a CSV parser created and made for nodeJS. Which takes a csv file and produce an array from it",
"main": "csv-parse.js",
"version": "0.0.2",
"description": "Intelligent CSV parser created and made for nodeJS. Which takes a csv file and produce an array from it.",
"main": "csv-array.js",
"scripts": {},
"repository": {
"type": "git",
"url": "https://github.com/sguha-work/CSV-parse-node.git"
"url": "https://github.com/sguha-work/csv-array.git"
},
"keywords": [
"csv",
"array",
"csv-array",
"parsing",
"parse csv",
"convert csv",
"json",
"parser",
"streaming"
],
"author": "Sahasrangshu Guha",
"license": "GNU",
"license": "MIT",
"engines": "v0.8.15",
"dependencies": {
"fs": "0.0.2"
"line-by-line": "~0.1.3"
}
}

@@ -5,17 +5,41 @@ #csv-array

## Dependencies
This package got only one dependency of "fs". You may install that first using following command
This package got only one dependencies of "line-by-line".
## Change log
* Dramatic improvement in speed.. Please forget version 0.0.1x
## Usage Guide
### Installing
The installation is just a command
```
npm install fs
npm install csv-array
```
## Usage Guide
After installing the package you can use the "parseCSV" method as follows
```
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

@@ -27,14 +51,115 @@ 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 array 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
[{"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"},{"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"}]
[
{
"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"
}
]
```

Sorry, the diff of this file is not supported yet

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