-
Notifications
You must be signed in to change notification settings - Fork 15
/
csv-streamify.js
180 lines (155 loc) · 4.39 KB
/
csv-streamify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict'
var through = require('through2')
/**
* Creates the CSV parser
*
* @param {object} opts Options object
* @param {string} [opts.delimiter=','] The delimiter character
* @param {string} [opts.newline='\n'] The newline character
* @param {string} [opts.quote='"'] The quote character
* @param {string} [opts.empty=''] Which character to return for empty fields
* @param {boolean} [opts.objectMode=true] Whether to return an object or a buffer per line
* @param {boolean} [opts.columns=false] Whether to parse headers
* @param {function} [cb] Callback function
*/
module.exports = function (opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
opts = opts || {}
if (typeof cb === 'function') opts.cb = cb
opts.delimiter = opts.delimiter || ','
opts.newline = opts.newline || '\n'
opts.quote = opts.quote || '"'
opts.empty = opts.hasOwnProperty('empty') ? opts.empty : ''
opts.objectMode = 'objectMode' in opts ? opts.objectMode : true
opts.hasColumns = opts.columns || false
// state
var state = {
body: [],
lineNo: 0,
_isQuoted: false,
_prev: [],
_newlineDetected: false,
_line: [],
_field: '',
_columns: []
}
return createParser(opts, state).on('error', function (err) {
if (opts.cb) cb(err)
})
}
function createParser (opts, state) {
function emitLine (parser) {
state._line.push(state._field)
var line = {}
if (opts.hasColumns) {
if (state.lineNo === 0) {
state._columns = state._line
state.lineNo += 1
reset()
return
}
state._columns.forEach(function (column, i) {
line[column] = state._line[i]
})
state._line = line
}
// buffer
if (opts.cb) state.body.push(state._line)
// emit the parsed line as an array if in object mode
// or as a stringified array (default)
if (opts.objectMode) {
parser.push(state._line)
} else {
parser.push(JSON.stringify(state._line) + '\n')
}
state.lineNo += 1
// reset state
reset()
}
function queue (char) {
state._prev.unshift(char)
while (state._prev.length > 3) state._prev.pop()
}
function reset () {
state._prev = []
state._field = ''
state._line = []
state._isQuoted = false
}
return through(opts, function parse (chunk, enc, cb) {
var data = chunk.toString()
var c
for (var i = 0; i < data.length; i++) {
c = data.charAt(i)
// we have a line break
if (!state._isQuoted && state._newlineDetected) {
state._newlineDetected = false
emitLine(this)
// crlf
if (c === opts.newline[1]) {
queue(c)
continue
}
}
// are the last two chars quotes?
if (state._isQuoted && state._prev[0] === opts.quote && state._prev[1] === opts.quote) {
state._field += opts.quote
state._prev = []
}
// skip over quote
if (c === opts.quote) {
queue(c)
continue
}
// once we hit a regular char, check if quoting applies
// xx"[c]
if (c !== opts.quote && state._prev[0] === opts.quote &&
state._prev[1] !== opts.quote) {
state._isQuoted = !state._isQuoted
}
// """[c]
if (c !== opts.quote && state._prev[0] === opts.quote &&
state._prev[1] === opts.quote && state._prev[2] === opts.quote) {
state._isQuoted = !state._isQuoted
state._field += opts.quote
}
// x""[c]
if (state._field && c !== opts.quote &&
state._prev[0] === opts.quote &&
state._prev[1] === opts.quote &&
state._prev[2] !== opts.quote) {
state._field += opts.quote
}
// delimiter
if (!state._isQuoted && c === opts.delimiter) {
if (state._field === '') state._field = opts.empty
state._line.push(state._field)
state._field = ''
queue(c)
continue
}
// newline
if (!state._isQuoted && (c === opts.newline || c === opts.newline[0])) {
state._newlineDetected = true
queue(c)
continue
}
queue(c)
// append current char to _field string
state._field += c
}
cb()
}, function flush (fn) {
// flush last line
try {
if (state._line.length || state._field) emitLine(this)
if (opts.cb) opts.cb(null, state.body)
fn()
} catch (err) {
fn(err)
}
})
}