-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
210 lines (179 loc) · 4.84 KB
/
index.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var through = require('through2')
var extend = require('extend')
module.exports = CSV
var quote = new Buffer('"')[0] // 22
function CSV(opts) {
if (!(this instanceof CSV)) return new CSV(opts)
if (!opts) opts = {}
var newline
var buffered
var headers
var inQuotes
var defaults = {
separator: ',',
newline: '\n',
detectNewlines: true,
json: false
}
opts = extend(defaults, opts)
if (opts.headers) {
headers = opts.headers
}
// alias 'delimiter' to 'newline'
if (opts.delimiter) opts.newline = opts.delimiter
if (opts.detectNewlines) delete opts.newline
else newline = new Buffer(opts.newline)
var comma = new Buffer(opts.separator || ',')[0]
var stream = through.obj(write, end)
stream.line = line
stream.cell = cell
stream.options = opts
return stream
function write(buf, enc, next) {
inQuotes = false
var offset = 0
if (buffered) {
buf = Buffer.concat([buffered, buf])
buffered = undefined
}
if (!newline) {
for (var i = 0; i < buf.length; i++) {
if (buf[i] === 13) { // \r
if (i === buf.length) return
if (buf[i + 1] === 10) { // \n
newline = new Buffer('\r\n')
break
} else {
newline = new Buffer('\r')
break
}
} else if (buf[i] === 10) { // \n
if (i === buf.length) return
newline = new Buffer('\n')
break
}
}
}
while (buf) {
var idx
if (newline) idx = nextLine(buf, offset)
if (idx) {
var line = buf.slice(offset, idx)
if (idx === buf.length) {
buffered = line
buf = undefined
offset = idx
} else {
queue(line)
offset = idx + newline.length
}
} else {
if (offset >= buf.length) {
buffered = undefined
} else {
buffered = buf.slice(offset, buf.length)
}
buf = undefined
}
}
next()
}
function end() {
if (buffered) queue(buffered)
queue(null)
}
function queue(lineBuffer) {
if (opts.json && lineBuffer) {
var cells = line(lineBuffer)
for (var i = 0; i < cells.length; i++) {
cells[i] = cell(cells[i]).toString()
}
if (!headers) return headers = cells
lineBuffer = zip(headers, cells)
}
stream.push(lineBuffer)
}
function zip(headers, cells) {
var obj = {}
for (var i = 0; i < headers.length; i++) obj[headers[i]] = cells[i]
return obj
}
function nextLine(buf, offset) {
var i = offset
if (offset >= buf.length) return false
for (var i = offset; i < buf.length; i++) {
if (buf[i] === quote) { // "
if (buf[i + 1] === quote) { // ""
i++
} else {
inQuotes = !inQuotes
}
continue
}
if (buf[i] === newline[0]) {
if (newline.length > 1) { // multichar newlines e.g. /r/n
var fullMatch = true
for (var j = i, k = 0; j < i + newline.length; j++, k++) {
if (buf[j] !== newline[k]) {
fullMatch = false
break
}
}
if (fullMatch && !inQuotes) return j - newline.length
} else { // singlechar newlines e.g. \n
if (!inQuotes) break
}
}
}
var idx = i + newline.length - 1
if (idx > buf.length) return false
return idx
}
function line(buf) {
var cells = []
var inQuotes = false
var offset = 0
for (var i = 0; i < buf.length; i++) {
if (buf[i] === quote) { // "
if (buf[i + 1] === quote) { // ""
i++
} else {
inQuotes = !inQuotes
}
continue
}
if (buf[i] === comma && !inQuotes) {
var cell = buf.slice(offset, i)
cells.push(cell)
offset = i + 1
}
}
if (offset < buf.length) cells.push(buf.slice(offset, buf.length))
if (buf[buf.length - 1] === comma) cells.push(new Buffer(0))
return cells
}
function cell(buf) {
if (buf[0] === quote && buf[buf.length - 1] === quote) buf = buf.slice(1, buf.length - 1)
// TODO way to implement this without looping twice?
var start = 0
var end = buf.length
if (buf[start] === quote && buf[end - 1] === quote) {
start++
end--
}
var cellLength = 0
// first loop is to figure out exact length of cell after escaped quotes are removed
for (var i = start; i < end; i++) {
if (buf[i] === quote && buf[i + 1] === quote) i++ // ""
cellLength++
}
var val = new Buffer(cellLength)
// second loop fills the val buffer with data
for (var i = start, y = 0; i < end; i++) {
if (buf[i] === quote && buf[i + 1] === quote) i++
val[y] = buf[i]
y++
}
return val
}
}