Comparing version 0.0.14 to 0.0.15
module.exports = { | ||
maxSizeForDirectRead : 10000000, | ||
maxSizeForDirectRead : 100, | ||
parseCSV : function(fileName, callBack) { | ||
@@ -8,9 +8,3 @@ var presentInstance = this; | ||
if(exists) { | ||
fs.stat(fileName, function(err, stats){ | ||
if(stats.size > presentInstance.maxSizeForDirectRead) { | ||
presentInstance.parseBigFile(fileName, callBack); | ||
} else { | ||
presentInstance.parseFile(fileName, callBack); | ||
} | ||
}); | ||
presentInstance.parseFile(fileName, callBack); | ||
} else { | ||
@@ -22,35 +16,2 @@ 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; | ||
}, | ||
// returns total data | ||
getDataArray : function(dataArray) { | ||
presentInstance = this; | ||
var attributeNameArray = dataArray[0].split(","); | ||
var finalArray = []; | ||
var length = dataArray.length; | ||
for(var index=1; index<length; index++) { | ||
var tempArray = {}; | ||
var dataList = presentInstance.getDataFromLine(dataArray[index]); | ||
var atrributeNameArrayLength = attributeNameArray.length; | ||
for(var index2=0; index2<atrributeNameArrayLength; index2++) { | ||
tempArray[attributeNameArray[index2]] = dataList[index2]; | ||
} | ||
finalArray.push(tempArray); | ||
} | ||
return finalArray; | ||
}, | ||
// returns data from a single line | ||
@@ -80,6 +41,4 @@ getDataFromLine : function(line) { | ||
}, | ||
tempLineCounter : 0,// only used for large files | ||
tempDataArray : [],// only used for large files | ||
tempAttributeNameArray : [],// only used for large files | ||
parseBigFile : function(fileName, callBack) { | ||
parseFile : function(fileName, callBack) { | ||
var presentObject = module.exports; | ||
@@ -89,5 +48,5 @@ var lblReader = require('line-by-line'); | ||
presentObject.tempDataArray = []; | ||
presentObject.tempAttributeNameArray = []; | ||
presentObject.tempLineCounter = 0; | ||
var tempDataArray = []; | ||
var tempAttributeNameArray = []; | ||
var tempLineCounter = 0; | ||
@@ -97,31 +56,43 @@ readStream.on('error', function(){ | ||
}); | ||
readStream.on('line', function(line) { | ||
readStream.on('line', function(line) {console.log(line); | ||
readStream.pause(); | ||
presentObject.buildOutputData(line); | ||
setTimeout(function() { | ||
readStream.resume(); | ||
},50); | ||
setTimeout(function () { | ||
if(tempLineCounter == 0) { | ||
tempAttributeNameArray = line.split(","); | ||
if(tempAttributeNameArray.length == 1) { | ||
tempDataArray.push(line); | ||
} | ||
tempLineCounter = 1; | ||
} else { | ||
if(tempAttributeNameArray.length == 1) { | ||
tempDataArray.push(line); | ||
} else { | ||
tempDataArray.push(presentObject.buildOutputData(tempAttributeNameArray, line)); | ||
} | ||
} | ||
readStream.resume(); | ||
}, 2); | ||
}); | ||
readStream.on('end', function() { | ||
callBack(presentObject.tempDataArray); | ||
callBack(tempDataArray); | ||
}); | ||
}, | ||
buildOutputData : function(line) { | ||
buildOutputData : function(tempAttributeNameArray, line) { | ||
var presentObject = module.exports; | ||
if(presentObject.tempLineCounter == 0) { | ||
presentObject.tempAttributeNameArray = line.split(","); | ||
} else { | ||
var dataArray = presentObject.getDataFromLine(line); | ||
var tempObject = {}; | ||
var tempAttributeNameArrayLength = presentObject.tempAttributeNameArray.length; | ||
var tempAttributeNameArrayLength = tempAttributeNameArray.length; | ||
for(var index=0; index<tempAttributeNameArrayLength; index++) { | ||
tempObject[presentObject.tempAttributeNameArray[index]] = dataArray[index]; | ||
tempObject[tempAttributeNameArray[index]] = dataArray[index]; | ||
} | ||
presentObject.tempDataArray.push(tempObject); | ||
} | ||
presentObject.tempLineCounter += 1; | ||
return tempObject; | ||
} | ||
} |
{ | ||
"name": "csv-array", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "Intelligent CSV parser created and made for nodeJS. Which takes a csv file and produce an array from it.", | ||
@@ -26,5 +26,4 @@ "main": "csv-array.js", | ||
"dependencies": { | ||
"fs": "0.0.2", | ||
"line-by-line": "~0.1.3" | ||
} | ||
} |
@@ -5,9 +5,12 @@ #csv-array | ||
## Dependencies | ||
This package got only two dependencies of "fs", and "line-by-line". | ||
This package got only one dependencies of "line-by-line". | ||
## Change log | ||
* Performence improvement | ||
* Better runtime memory management | ||
* For small files memory usage improved | ||
* For large files the option of streaming rather than reading all at once, added | ||
* 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 row | ||
@@ -14,0 +17,0 @@ ## Usage Guide |
Sorry, the diff of this file is not supported yet
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
1
73
1
5921
81