|
| 1 | +/* |
| 2 | +The MIT License (MIT) |
| 3 | +
|
| 4 | +Copyright (c) 2019 Evan Plaice <[email protected]> |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining |
| 7 | +a copy of this software and associated documentation files (the |
| 8 | +'Software'), to deal in the Software without restriction, including |
| 9 | +without limitation the rights to use, copy, modify, merge, publish, |
| 10 | +distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | +permit persons to whom the Software is furnished to do so, subject to |
| 12 | +the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be |
| 15 | +included in all copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
| 18 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 | +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 | +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 | +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 | +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | +*/ |
| 25 | +export function parse (csv, options, reviver = v => v) { |
| 26 | + const ctx = Object.create(null) |
| 27 | + ctx.options = options || {} |
| 28 | + ctx.reviver = reviver |
| 29 | + ctx.value = '' |
| 30 | + ctx.entry = [] |
| 31 | + ctx.output = [] |
| 32 | + ctx.col = 1 |
| 33 | + ctx.row = 1 |
| 34 | + |
| 35 | + ctx.options.delimiter = ctx.options.delimiter === undefined ? '"' : options.delimiter; |
| 36 | + if(ctx.options.delimiter.length > 1 || ctx.options.delimiter.length === 0) |
| 37 | + throw Error(`CSVError: delimiter must be one character [${ctx.options.separator}]`) |
| 38 | + |
| 39 | + ctx.options.separator = ctx.options.separator === undefined ? ',' : options.separator; |
| 40 | + if(ctx.options.separator.length > 1 || ctx.options.separator.length === 0) |
| 41 | + throw Error(`CSVError: separator must be one character [${ctx.options.separator}]`) |
| 42 | + |
| 43 | + const lexer = new RegExp(`${escapeRegExp(ctx.options.delimiter)}|${escapeRegExp(ctx.options.separator)}|\r\n|\n|\r|[^${escapeRegExp(ctx.options.delimiter)}${escapeRegExp(ctx.options.separator)}\r\n]+`, 'y') |
| 44 | + const isNewline = /^(\r\n|\n|\r)$/ |
| 45 | + |
| 46 | + let matches = [] |
| 47 | + let match = '' |
| 48 | + let state = 0 |
| 49 | + |
| 50 | + while ((matches = lexer.exec(csv)) !== null) { |
| 51 | + match = matches[0] |
| 52 | + |
| 53 | + switch (state) { |
| 54 | + case 0: // start of entry |
| 55 | + switch (true) { |
| 56 | + case match === ctx.options.delimiter: |
| 57 | + state = 3 |
| 58 | + break |
| 59 | + case match === ctx.options.separator: |
| 60 | + state = 0 |
| 61 | + valueEnd(ctx) |
| 62 | + break |
| 63 | + case isNewline.test(match): |
| 64 | + state = 0 |
| 65 | + valueEnd(ctx) |
| 66 | + entryEnd(ctx) |
| 67 | + break |
| 68 | + default: |
| 69 | + ctx.value += match |
| 70 | + state = 2 |
| 71 | + break |
| 72 | + } |
| 73 | + break |
| 74 | + case 2: // un-delimited input |
| 75 | + switch (true) { |
| 76 | + case match === ctx.options.separator: |
| 77 | + state = 0 |
| 78 | + valueEnd(ctx) |
| 79 | + break |
| 80 | + case isNewline.test(match): |
| 81 | + state = 0 |
| 82 | + valueEnd(ctx) |
| 83 | + entryEnd(ctx) |
| 84 | + break |
| 85 | + default: |
| 86 | + state = 4 |
| 87 | + throw Error(`CSVError: Illegal state [row:${ctx.row}, col:${ctx.col}]`) |
| 88 | + } |
| 89 | + break |
| 90 | + case 3: // delimited input |
| 91 | + switch (true) { |
| 92 | + case match === ctx.options.delimiter: |
| 93 | + state = 4 |
| 94 | + break |
| 95 | + default: |
| 96 | + state = 3 |
| 97 | + ctx.value += match |
| 98 | + break |
| 99 | + } |
| 100 | + break |
| 101 | + case 4: // escaped or closing delimiter |
| 102 | + switch (true) { |
| 103 | + case match === ctx.options.delimiter: |
| 104 | + state = 3 |
| 105 | + ctx.value += match |
| 106 | + break |
| 107 | + case match === ctx.options.separator: |
| 108 | + state = 0 |
| 109 | + valueEnd(ctx) |
| 110 | + break |
| 111 | + case isNewline.test(match): |
| 112 | + state = 0 |
| 113 | + valueEnd(ctx) |
| 114 | + entryEnd(ctx) |
| 115 | + break |
| 116 | + default: |
| 117 | + throw Error(`CSVError: Illegal state [row:${ctx.row}, col:${ctx.col}]`) |
| 118 | + } |
| 119 | + break |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // flush the last value |
| 124 | + if (ctx.entry.length !== 0) { |
| 125 | + valueEnd(ctx) |
| 126 | + entryEnd(ctx) |
| 127 | + } |
| 128 | + |
| 129 | + return ctx.output |
| 130 | +} |
| 131 | + |
| 132 | +export function stringify (array, options = {}, replacer = v => v) { |
| 133 | + const ctx = Object.create(null) |
| 134 | + ctx.options = options |
| 135 | + ctx.options.eof = ctx.options.eof !== undefined ? ctx.options.eof : true |
| 136 | + ctx.row = 1 |
| 137 | + ctx.col = 1 |
| 138 | + ctx.output = '' |
| 139 | + |
| 140 | + ctx.options.delimiter = ctx.options.delimiter === undefined ? '"' : options.delimiter; |
| 141 | + if(ctx.options.delimiter.length > 1 || ctx.options.delimiter.length === 0) |
| 142 | + throw Error(`CSVError: delimiter must be one character [${ctx.options.separator}]`) |
| 143 | + |
| 144 | + ctx.options.separator = ctx.options.separator === undefined ? ',' : options.separator; |
| 145 | + if(ctx.options.separator.length > 1 || ctx.options.separator.length === 0) |
| 146 | + throw Error(`CSVError: separator must be one character [${ctx.options.separator}]`) |
| 147 | + |
| 148 | + const needsDelimiters = new RegExp(`${escapeRegExp(ctx.options.delimiter)}|${escapeRegExp(ctx.options.separator)}|\r\n|\n|\r`) |
| 149 | + |
| 150 | + array.forEach((row, rIdx) => { |
| 151 | + let entry = '' |
| 152 | + ctx.col = 1 |
| 153 | + row.forEach((col, cIdx) => { |
| 154 | + if (typeof col === 'string') { |
| 155 | + col = col.replace(new RegExp(ctx.options.delimiter, 'g'), `${ctx.options.delimiter}${ctx.options.delimiter}`) |
| 156 | + col = needsDelimiters.test(col) ? `${ctx.options.delimiter}${col}${ctx.options.delimiter}` : col |
| 157 | + } |
| 158 | + entry += replacer(col, ctx.row, ctx.col) |
| 159 | + if (cIdx !== row.length - 1) { |
| 160 | + entry += ctx.options.separator |
| 161 | + } |
| 162 | + ctx.col++ |
| 163 | + }) |
| 164 | + switch (true) { |
| 165 | + case ctx.options.eof: |
| 166 | + case !ctx.options.eof && rIdx !== array.length - 1: |
| 167 | + ctx.output += `${entry}\n` |
| 168 | + break |
| 169 | + default: |
| 170 | + ctx.output += `${entry}` |
| 171 | + break |
| 172 | + } |
| 173 | + ctx.row++ |
| 174 | + }) |
| 175 | + |
| 176 | + return ctx.output |
| 177 | +} |
| 178 | + |
| 179 | +function valueEnd (ctx) { |
| 180 | + const value = ctx.options.typed ? inferType(ctx.value) : ctx.value |
| 181 | + ctx.entry.push(ctx.reviver(value, ctx.row, ctx.col)) |
| 182 | + ctx.value = '' |
| 183 | + ctx.col++ |
| 184 | +} |
| 185 | + |
| 186 | +function entryEnd (ctx) { |
| 187 | + ctx.output.push(ctx.entry) |
| 188 | + ctx.entry = [] |
| 189 | + ctx.row++ |
| 190 | + ctx.col = 1 |
| 191 | +} |
| 192 | + |
| 193 | +function inferType (value) { |
| 194 | + const isNumber = /.\./ |
| 195 | + |
| 196 | + switch (true) { |
| 197 | + case value === 'true': |
| 198 | + case value === 'false': |
| 199 | + return value === 'true' |
| 200 | + case isNumber.test(value): |
| 201 | + return parseFloat(value) |
| 202 | + case isFinite(value): |
| 203 | + return parseInt(value) |
| 204 | + default: |
| 205 | + return value |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +function escapeRegExp(str) { |
| 210 | + return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); |
| 211 | +} |
0 commit comments