Skip to content
Browse files

Updates to node.js .1.30

  • Loading branch information...
1 parent 74ee96c commit dcc187f835e66ed0d548c1cfecbb091d37e5c6f3 @voodootikigod committed
Showing with 32 additions and 22 deletions.
  1. +28 −20 lib/csv.js
  2. +4 −2 test/each_test.js
View
48 lib/csv.js
@@ -14,8 +14,9 @@
//
//
//
-var posix = require("posix");
-var sys = require("sys");
+var fs = require("fs"),
+ sys = require("sys"),
+ events = require("events");
// CSVToArray Parsing function from http://www.bennadel.com/blog/1504-Ask-Ben-Parsing-CSV-Strings-With-Javascript-Exec-Regular-Expression-Command.htm
@@ -109,10 +110,10 @@ exports.each = function (filename, options) {
var strDelimiter = (options.strDelimiter || ",");
var headers = null;
var position = 0;
- var readAmount = (options.readAmount || 4096);
+ var readAmount = (options.readAmount || (16384));
var buffer = "";
var fd = null;
- var stream = new process.EventEmitter();
+ var stream = new events.EventEmitter();
var emit_row = function(row) {
var data = CSVToArray(row)[0];
@@ -129,8 +130,12 @@ exports.each = function (filename, options) {
}
var readMore = function() {
- posix.read(fd, readAmount, position)
- .addCallback(function(data, bytesRead) {
+ fs.read(fd, readAmount, position, "utf8", function(err, data, bytesRead) {
+ if (err) {
+ sys.puts("E1");
+ stream.emit("error",e);
+ fs.close(fd);
+ } else {
position += bytesRead;
buffer += data;
var parts = buffer.split("\n");
@@ -142,23 +147,26 @@ exports.each = function (filename, options) {
buffer = parts[pl-1];
}
if (bytesRead == readAmount) {
- readMore();
+ readMore();
} else {
- emit_row(buffer);
+ if (buffer.length > 0) {
+ emit_row(buffer);
+ }
+ fs.close(fd);
+ stream.emit("complete")
}
- })
- .addErrback(function(e){stream.emit("error",e)})
- .addListener('eof',function(){posix.close(fd)})
- .addListener('error',function(){posix.close(fd)});
- };
-
- posix.open(filename, process.O_RDONLY, 0666)
- .addCallback(function (_fd) {
- if (_fd) {
- fd = _fd;
- readMore();
}
});
+ };
+
+ fs.open(filename, process.O_RDONLY, 0666, function (err, _fd) {
+ if (err) {
+ sys.puts("Could not open the file: "+filename);
+ } else if (_fd) {
+ fd = _fd;
+ readMore();
+ }
+ });
return stream;
};
@@ -173,7 +181,7 @@ exports.parse = function(str, options, data_listener) {
throw new Exception("Data listener must be provided");
}
options = (options || {});
- var stream = new process.EventEmitter();
+ var stream = new events.EventEmitter();
stream.addListener("data", data_listener);
var parts = str.split("\n");
var pl = parts.length;
View
6 test/each_test.js
@@ -8,9 +8,11 @@ helper.testCase("Each Tests", {
var count = 0;
csv.each("basic_sample.csv").addListener("data", function(data) {
count += 1;
- sys.puts(count);
+ }).addListener("complete", function() {
+ test.assertTrue(count == 1800);
});
- test.assertTrue(count == 1800);
+
+
}
});

0 comments on commit dcc187f

Please sign in to comment.
Something went wrong with that request. Please try again.