Last active
April 19, 2016 12:16
-
-
Save Codesleuth/4f50a6e4d9e4e1f56f20 to your computer and use it in GitHub Desktop.
Some sort of CSV parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var readline = require('readline') | |
var fs = require('fs') | |
var stream = require('stream') | |
var util = require('util') | |
var Transform = stream.Transform | |
var iconv = require('iconv-lite'); | |
function CSVer(options) { | |
this.line = '' | |
Transform.call(this, options) | |
} | |
util.inherits(CSVer, Transform) | |
CSVer.prototype._transform = function (line, enc, cb) { | |
var thisLine = iconv.decode(line, 'win1252'); | |
var lines = thisLine.split('\r\n') | |
if (lines.length == 0) { | |
cb() | |
return | |
} | |
this.line += lines[0] | |
while (lines.length > 1) { | |
var fields = this.line.split(',') | |
for (var fieldIndex = 0; fieldIndex < fields.length; fieldIndex++) { | |
var field = fields[fieldIndex] | |
var needsQuotes = false | |
if (field.indexOf('"') > -1) { | |
field = field.replace(/"/g, '""') | |
needsQuotes = true | |
} | |
if (field.indexOf('\n') > -1) needsQuotes = true | |
if (needsQuotes === true) | |
this.push('"' + field + '"') | |
else | |
this.push(field) | |
if (fieldIndex < fields.length - 1) | |
this.push(',') | |
else | |
this.push('\r\n') | |
} | |
lines.splice(0, 1) | |
this.line = lines[0] | |
} | |
cb() | |
} | |
var readStream = fs.createReadStream(process.argv[2]) | |
var writeStream = fs.createWriteStream(process.argv[3]) | |
writeStream.write('\uFEFF', 'utf8'); | |
var csver = new CSVer() | |
readStream.pipe(csver) | |
csver.pipe(writeStream) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment