csv-parse
Version:
CSV parsing implementing the Node.js `stream.Transform` API
56 lines (42 loc) • 1.09 kB
JavaScript
;
// Generated by CoffeeScript 2.3.2
// # CSV Parse Sync
// Provides a synchronous alternative to the CSV parser.
// ## Usage
// `const records = parse(data, [options]`
var StringDecoder, parse;
var _require = require('string_decoder');
StringDecoder = _require.StringDecoder;
parse = require('./index');
module.exports = function (data) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var chunks, decoder, err, parser;
chunks = options.objname ? {} : [];
if (data instanceof Buffer) {
decoder = new StringDecoder();
data = decoder.write(data);
}
parser = new parse.Parser(options);
parser.push = function (chunk) {
if (options.objname) {
return chunks[chunk[0]] = chunk[1];
} else {
return chunks.push(chunk);
}
};
err = parser.__write(data, false);
if (err) {
throw err;
}
if (data instanceof Buffer) {
err = parser.__write(data.end(), true);
if (err) {
throw err;
}
}
err = parser.__flush();
if (err) {
throw err;
}
return chunks;
};