Skip to content

Commit

Permalink
Made logic and type checking simpler at certain places
Browse files Browse the repository at this point in the history
- Simplified logic to set encoding in createCsvStreamReader and createCsvFileReader
- Type checking for an Array in CsvWriter.prototype.writeRecord
  • Loading branch information
tedeh committed Oct 23, 2011
1 parent 63dcc87 commit d96f141
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions lib/ya-csv.js
Expand Up @@ -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);
};

Expand All @@ -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);
};

Expand All @@ -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) {
Expand Down

0 comments on commit d96f141

Please sign in to comment.