|
| 1 | +// chunkSize >> largest expected row |
| 2 | +const defaultOptions = { |
| 3 | + header: true, // false: return array; true: detect headers and return json; [...]: use defined headers and return json |
| 4 | + newlineChar: '\r\n', // undefined: detect newline from file; '\r\n': Windows; '\n': Linux/Mac |
| 5 | + delimiterChar: '', |
| 6 | + // quoteChar: '"', |
| 7 | + // escapeChar: '"', // default: `quoteChar` |
| 8 | + |
| 9 | + // Parse |
| 10 | + emptyFieldValue: '', |
| 11 | + coerceField: (field) => field, // TODO tests |
| 12 | + // commentPrefixValue: false, // falsy: disable, '//': enabled |
| 13 | + // errorOnComment: true, |
| 14 | + // errorOnEmptyLine: true, |
| 15 | + errorOnFieldsMismatch: true |
| 16 | + // errorOnFieldMalformed: true |
| 17 | +} |
| 18 | + |
| 19 | +const length = (value) => value.length |
| 20 | + |
| 21 | +export const parse = (opts = {}) => { |
| 22 | + const options = { ...defaultOptions, ...opts } |
| 23 | + options.escapeChar ??= options.quoteChar |
| 24 | + |
| 25 | + let { header, newlineChar, delimiterChar } = options |
| 26 | + let headerLength = length(header) |
| 27 | + const { |
| 28 | + // quoteChar, |
| 29 | + // escapeChar, |
| 30 | + // commentPrefixValue, |
| 31 | + emptyFieldValue, |
| 32 | + coerceField, |
| 33 | + // errorOnEmptyLine, |
| 34 | + // errorOnComment, |
| 35 | + errorOnFieldsMismatch |
| 36 | + // errorOnFieldMalformed |
| 37 | + } = options |
| 38 | + |
| 39 | + let chunk, enqueue |
| 40 | + let partialLine = '' |
| 41 | + let idx = 0 |
| 42 | + const enqueueRow = (row) => { |
| 43 | + let data = row |
| 44 | + idx += 1 |
| 45 | + if (headerLength) { |
| 46 | + const rowLength = length(row) |
| 47 | + |
| 48 | + if (headerLength !== rowLength) { |
| 49 | + if (errorOnFieldsMismatch) { |
| 50 | + enqueueError( |
| 51 | + 'FieldsMismatch', |
| 52 | + `Incorrect number of fields parsed, expected ${headerLength}.` |
| 53 | + ) |
| 54 | + } |
| 55 | + return |
| 56 | + } else { |
| 57 | + data = {} |
| 58 | + for (let i = 0; i < rowLength; i++) { |
| 59 | + data[header[i]] = row[i] |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + enqueue({ idx, data }) |
| 64 | + } |
| 65 | + |
| 66 | + const enqueueError = (code, message) => { |
| 67 | + enqueue({ idx, err: { code, message } }) |
| 68 | + } |
| 69 | + |
| 70 | + const transformField = (field, idx) => { |
| 71 | + return coerceField(field || emptyFieldValue, idx) |
| 72 | + } |
| 73 | + |
| 74 | + const chunkParse = (string, controller) => { |
| 75 | + chunk = string |
| 76 | + enqueue = controller.enqueue |
| 77 | + const lines = chunk.split(newlineChar) // TODO use cursor pattern |
| 78 | + let linesLength = length(lines) |
| 79 | + if (linesLength > 1) { |
| 80 | + partialLine = lines.pop() |
| 81 | + linesLength -= 1 |
| 82 | + } |
| 83 | + |
| 84 | + let i = 0 |
| 85 | + if (header === true) { |
| 86 | + header = lines[i].split(delimiterChar) |
| 87 | + headerLength = length(header) |
| 88 | + i += 1 |
| 89 | + } |
| 90 | + |
| 91 | + for (; i < linesLength; i++) { |
| 92 | + const line = lines[i] |
| 93 | + const row = [] |
| 94 | + let cursor = 0 |
| 95 | + while (cursor < line.length) { |
| 96 | + const delimiterIndex = line.indexOf(delimiterChar, cursor) |
| 97 | + if (delimiterIndex === -1) { |
| 98 | + row.push(transformField(line.substring(cursor), row.length)) |
| 99 | + break |
| 100 | + } |
| 101 | + row.push( |
| 102 | + transformField(line.substring(cursor, delimiterIndex), row.length) |
| 103 | + ) |
| 104 | + cursor = delimiterIndex + 1 |
| 105 | + } |
| 106 | + enqueueRow(row) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return { |
| 111 | + chunkParse, |
| 112 | + header: () => header, |
| 113 | + previousChunk: () => partialLine |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +export default (input, opts) => { |
| 118 | + const options = { |
| 119 | + ...defaultOptions, |
| 120 | + ...{ |
| 121 | + enableReturn: true, |
| 122 | + chunkSize: 64 * 1024 * 1024, |
| 123 | + enqueue: () => {} |
| 124 | + }, |
| 125 | + ...opts |
| 126 | + } |
| 127 | + const { chunkSize, enableReturn, enqueue } = options |
| 128 | + const { chunkParse, previousChunk } = parse(options) |
| 129 | + |
| 130 | + const res = [] |
| 131 | + const controller = { enqueue } |
| 132 | + |
| 133 | + if (enableReturn) { |
| 134 | + controller.enqueue = (row) => { |
| 135 | + enqueue(row) |
| 136 | + res.push(row.data) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + let position = 0 |
| 141 | + while (position < input.length) { |
| 142 | + const chunk = |
| 143 | + previousChunk() + input.substring(position, position + chunkSize) |
| 144 | + |
| 145 | + // Checking if you can use fastParse slows it down more than checking for quoteChar on ever field. |
| 146 | + chunkParse(chunk, controller) |
| 147 | + position += chunkSize |
| 148 | + } |
| 149 | + // flush |
| 150 | + const chunk = previousChunk() |
| 151 | + chunkParse(chunk, controller, true) |
| 152 | + |
| 153 | + return enableReturn && res |
| 154 | +} |
0 commit comments