Skip to content
Browse files

Made logic and type checking simpler at certain places

- Simplified logic to set encoding in createCsvStreamReader and createCsvFileReader
- Type checking for an Array in CsvWriter.prototype.writeRecord
  • Loading branch information...
1 parent 63dcc87 commit d96f141d244472562154cd2901e6e1fd5adafcef @tedeh tedeh committed
Showing with 5 additions and 15 deletions.
  1. +5 −15 lib/ya-csv.js
View
20 lib/ya-csv.js
@@ -161,11 +161,7 @@ csv.createCsvFileReader = function(path, options) {
var readStream = fs.createReadStream(path, {
'flags': 'r'
});
- if (options.encoding) {
- readStream.setEncoding(options.encoding);
- } else {
- readStream.setEncoding('utf8');
- }
+ readStream.setEncoding(options.encoding || 'utf8');
return new CsvReader(readStream, options);
};
@@ -175,8 +171,7 @@ csv.createCsvStreamReader = function(readStream, options) {
readStream = undefined;
}
options = options || {};
- if (readStream)
- readStream.setEncoding(options.encoding ? options.encoding : 'utf8');
+ if (readStream) readStream.setEncoding(options.encoding || 'utf8');
return new CsvReader(readStream, options);
};
@@ -199,15 +194,10 @@ sys.inherits(CsvWriter, events.EventEmitter);
CsvWriter.prototype.writeRecord = function(rec) {
if (!rec) return; // ignore empty records
- if (typeof(rec) !== 'object') {
- throw new Error("CsvWriter.writeRecord takes an array as an argument");
- }
- if (rec.length || rec.length === 0) { // array
- _writeArray(this, rec);
- } else { // hash
- throw new Error("CsvWriter.writeRecord takes an array as an argument, "
- + "hashes are not supported.");
+ if (!Array.isArray(rec)) {
+ throw new Error("CsvWriter.writeRecord only takes an array as an argument");
}
+ _writeArray(this, rec);
};
function _writeArray(writer, arr) {

0 comments on commit d96f141

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