-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
194 lines (180 loc) · 4.53 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
/**
* Parse takes a string of CSV data and converts it to a 2 dimensional array
*
* options
* - typed - infer types [false]
*
* @static
* @param {string} csv the CSV string to parse
* @param {Object} [options] an object containing the options
* @param {Function} [reviver] a custom function to modify the values
* @returns {Array} a 2 dimensional array of `[entries][values]`
*/
export function parse (csv, options, reviver = v => v) {
const ctx = Object.create(null)
ctx.options = options || {}
ctx.reviver = reviver
ctx.value = ''
ctx.entry = []
ctx.output = []
ctx.col = 1
ctx.row = 1
const lexer = /"|,|\r\n|\n|\r|[^",\r\n]+/y
const isNewline = /^(\r\n|\n|\r)$/
let matches = []
let match = ''
let state = 0
while ((matches = lexer.exec(csv)) !== null) {
match = matches[0]
switch (state) {
case 0: // start of entry
switch (true) {
case match === '"':
state = 3
break
case match === ',':
state = 0
valueEnd(ctx)
break
case isNewline.test(match):
state = 0
valueEnd(ctx)
entryEnd(ctx)
break
default:
ctx.value += match
state = 2
break
}
break
case 2: // un-delimited input
switch (true) {
case match === ',':
state = 0
valueEnd(ctx)
break
case isNewline.test(match):
state = 0
valueEnd(ctx)
entryEnd(ctx)
break
default:
state = 4
throw Error(`CSVError: Illegal state [row:${ctx.row}, col:${ctx.col}]`)
}
break
case 3: // delimited input
switch (true) {
case match === '"':
state = 4
break
default:
state = 3
ctx.value += match
break
}
break
case 4: // escaped or closing delimiter
switch (true) {
case match === '"':
state = 3
ctx.value += match
break
case match === ',':
state = 0
valueEnd(ctx)
break
case isNewline.test(match):
state = 0
valueEnd(ctx)
entryEnd(ctx)
break
default:
throw Error(`CSVError: Illegal state [row:${ctx.row}, col:${ctx.col}]`)
}
break
}
}
// flush the last value
if (ctx.entry.length !== 0) {
valueEnd(ctx)
entryEnd(ctx)
}
return ctx.output
}
/**
* Stringify takes a 2 dimensional array of `[entries][values]` and converts them to CSV
*
* options
* - eof - add a trailing newline at the end of file [true]
*
* @static
* @param {Array} array the input array to stringify
* @param {Object} [options] an object containing the options
* @param {Function} [replacer] a custom function to modify the values
* @returns {string} the CSV string
*/
export function stringify (array, options = {}, replacer = v => v) {
const ctx = Object.create(null)
ctx.options = options
ctx.options.eof = ctx.options.eof !== undefined ? ctx.options.eof : true
ctx.row = 1
ctx.col = 1
ctx.output = ''
const needsDelimiters = /"|,|\r\n|\n|\r/
array.forEach((row, rIdx) => {
let entry = ''
ctx.col = 1
row.forEach((col, cIdx) => {
if (typeof col === 'string') {
col = col.replace(/"/g, '""')
col = needsDelimiters.test(col) ? `"${col}"` : col
}
entry += replacer(col, ctx.row, ctx.col)
if (cIdx !== row.length - 1) {
entry += ','
}
ctx.col++
})
switch (true) {
case ctx.options.eof:
case !ctx.options.eof && rIdx !== array.length - 1:
ctx.output += `${entry}\n`
break
default:
ctx.output += `${entry}`
break
}
ctx.row++
})
return ctx.output
}
/** @private */
function valueEnd (ctx) {
const value = ctx.options.typed ? inferType(ctx.value) : ctx.value
ctx.entry.push(ctx.reviver(value, ctx.row, ctx.col))
ctx.value = ''
ctx.col++
}
/** @private */
function entryEnd (ctx) {
ctx.output.push(ctx.entry)
ctx.entry = []
ctx.row++
ctx.col = 1
}
/** @private */
function inferType (value) {
const isNumber = /.\./
switch (true) {
case value === 'true':
case value === 'false':
return value === 'true'
case isNumber.test(value):
return parseFloat(value)
case isFinite(value):
return parseInt(value)
default:
return value
}
}