-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparse-mini.js
154 lines (136 loc) · 3.81 KB
/
parse-mini.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
// chunkSize >> largest expected row
const defaultOptions = {
header: true, // false: return array; true: detect headers and return json; [...]: use defined headers and return json
newlineChar: '\r\n', // '': detect newline from chunk; '\r\n': Windows; '\n': Linux/Mac
delimiterChar: ',', // '': detect delimiter from chunk
// quoteChar: '"',
// escapeChar: '"', // default: `quoteChar`
// Parse
emptyFieldValue: '',
coerceField: (field) => field, // TODO tests
// commentPrefixValue: false, // falsy: disable, '//': enabled
// errorOnComment: true,
// errorOnEmptyLine: true,
errorOnFieldsMismatch: true
// errorOnFieldMalformed: true
}
const length = (value) => value.length
export const parse = (opts = {}) => {
const options = { ...defaultOptions, ...opts }
options.escapeChar ??= options.quoteChar
let { header, newlineChar, delimiterChar } = options
let headerLength = length(header)
const {
// quoteChar,
// escapeChar,
// commentPrefixValue,
emptyFieldValue,
coerceField,
// errorOnEmptyLine,
// errorOnComment,
errorOnFieldsMismatch
// errorOnFieldMalformed
} = options
let chunk, enqueue
let partialLine = ''
let idx = 0
const enqueueRow = (row) => {
let data = row
idx += 1
if (headerLength) {
const rowLength = length(row)
if (headerLength !== rowLength) {
if (errorOnFieldsMismatch) {
enqueueError(
'FieldsMismatch',
`Incorrect number of fields parsed, expected ${headerLength}.`
)
}
return
} else {
data = {}
for (let i = 0; i < rowLength; i++) {
data[header[i]] = row[i]
}
}
}
enqueue({ idx, data })
}
const enqueueError = (code, message) => {
enqueue({ idx, err: { code, message } })
}
const transformField = (field, idx) => {
return coerceField(field || emptyFieldValue, idx)
}
const chunkParse = (string, controller) => {
chunk = string
enqueue = controller.enqueue
const lines = chunk.split(newlineChar) // TODO use cursor pattern
let linesLength = length(lines)
if (linesLength > 1) {
partialLine = lines.pop()
linesLength -= 1
}
let i = 0
if (header === true) {
header = lines[i].split(delimiterChar)
headerLength = length(header)
i += 1
}
for (; i < linesLength; i++) {
const line = lines[i]
const row = []
let cursor = 0
while (cursor < line.length) {
const delimiterIndex = line.indexOf(delimiterChar, cursor)
if (delimiterIndex === -1) {
row.push(transformField(line.substring(cursor), row.length))
break
}
row.push(
transformField(line.substring(cursor, delimiterIndex), row.length)
)
cursor = delimiterIndex + 1
}
enqueueRow(row)
}
}
return {
chunkParse,
header: () => header,
previousChunk: () => partialLine
}
}
export default (input, opts) => {
const options = {
...defaultOptions,
...{
enableReturn: true,
chunkSize: 64 * 1024 * 1024,
enqueue: () => {}
},
...opts
}
const { chunkSize, enableReturn, enqueue } = options
const { chunkParse, previousChunk } = parse(options)
const res = []
const controller = { enqueue }
if (enableReturn) {
controller.enqueue = (row) => {
enqueue(row)
res.push(row.data)
}
}
let position = 0
while (position < input.length) {
const chunk =
previousChunk() + input.substring(position, position + chunkSize)
// Checking if you can use fastParse slows it down more than checking for quoteChar on ever field.
chunkParse(chunk, controller)
position += chunkSize
}
// flush
const chunk = previousChunk()
chunkParse(chunk, controller, true)
return enableReturn && res
}