|
| 1 | +var stream = require('stream'); |
| 2 | +var util = require('util'); |
| 3 | + |
| 4 | +/** |
| 5 | + * @const |
| 6 | + * @type {RegExp} |
| 7 | + */ |
| 8 | +var PARSE_FLOAT_TEST = /^[0-9]+(?:\.[0-9]*)?(?:[eE]\+[0-9]+)?$|^(?:[0-9]+)?\.[0-9]+(?:e+[0-9]+)?$|^[-+]?Infinity$|^[-+]?NaN$/; |
| 9 | + |
| 10 | +var Transform = stream.Transform; |
| 11 | + |
| 12 | +/** |
| 13 | + * @param {Object?} options |
| 14 | + * @param {String?} options.delimiter=',' - Specify what is the CSV delimeter |
| 15 | + * @param {Boolean=true} options.multiline - Support Excel-like multiline CSV |
| 16 | + * @param {Boolean=true} options.allowQuotes - Allow quotation marks to wrap columns |
| 17 | + * @param {Boolean=false} options.skipEmptyLines - Should empty lines be automatically skipped? |
| 18 | + * @param {Boolean=false} options.parseNumbers - Automatically parse numbers (with a . as the decimal separator) |
| 19 | + * @param {Boolean=false} options.parseBooleans - Automatically parse booleans (strictly lowercase `true` and `false`) |
| 20 | + * @param {Boolean=false} options.ltrim - Automatically left-trims columns |
| 21 | + * @param {Boolean=false} options.rtrim - Automatically right-trims columns |
| 22 | + * @param {Boolean=false} options.trim - If true, then both 'ltrim' and 'rtrim' are set to true |
| 23 | + * @returns {CsvReadableStream} |
| 24 | + * @constructor |
| 25 | + */ |
| 26 | +var CsvReadableStream = function (options) { |
| 27 | + options = options || {}; |
| 28 | + |
| 29 | + //noinspection JSUndefinedPropertyAssignment |
| 30 | + options.objectMode = true; |
| 31 | + |
| 32 | + if (!(this instanceof CsvReadableStream)) { |
| 33 | + return new CsvReadableStream(options); |
| 34 | + } |
| 35 | + |
| 36 | + var data = null |
| 37 | + , dataIndex = null |
| 38 | + , nextIndex = null |
| 39 | + , dataLen = null |
| 40 | + , columns = [] |
| 41 | + , column = '' |
| 42 | + , lastLineEndCR = false |
| 43 | + , lookForBOM = true |
| 44 | + , isQuoted = false |
| 45 | + |
| 46 | + , multiline = !!options.multiline || typeof options.multiline === 'undefined' |
| 47 | + , delimiter = options.delimiter != null ? options.delimiter.toString() || ',' : ',' |
| 48 | + , allowQuotes = !!options.allowQuotes || typeof options.allowQuotes === 'undefined' |
| 49 | + , skipEmptyLines = !!options.skipEmptyLines |
| 50 | + , parseNumbers = !!options.parseNumbers |
| 51 | + , parseBooleans = !!options.parseBooleans |
| 52 | + , ltrim = !!options.ltrim || !!options.trim |
| 53 | + , rtrim = !!options.rtrim || !!options.trim |
| 54 | + , trim = options.ltrim && options.rtrim |
| 55 | + |
| 56 | + , postProcessingEnabled = parseNumbers || parseBooleans || ltrim || rtrim; |
| 57 | + |
| 58 | + var postProcessColumn = function (column) { |
| 59 | + |
| 60 | + if (trim) { |
| 61 | + column = column.replace(/^\s+|\s+$/, ''); |
| 62 | + } |
| 63 | + else if (ltrim) { |
| 64 | + column = column.replace(/^\s+/, ''); |
| 65 | + } |
| 66 | + else if (rtrim) { |
| 67 | + column = column.replace(/\s+$/, ''); |
| 68 | + } |
| 69 | + |
| 70 | + if (parseBooleans) { |
| 71 | + if (column === 'true') { |
| 72 | + return true; |
| 73 | + } |
| 74 | + if (column === 'false') { |
| 75 | + return false; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (parseNumbers) { |
| 80 | + if (PARSE_FLOAT_TEST.test(column)) { |
| 81 | + return parseFloat(column); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return column; |
| 86 | + }; |
| 87 | + |
| 88 | + this._processChunk = function (newData) { |
| 89 | + |
| 90 | + if (newData) { |
| 91 | + if (data) { |
| 92 | + data = data.substr(dataIndex) + newData; |
| 93 | + } else { |
| 94 | + data = newData; |
| 95 | + } |
| 96 | + dataLen = data.length; |
| 97 | + dataIndex = 0; |
| 98 | + } |
| 99 | + |
| 100 | + // Node doesn't strip BOMs, that's in user's land |
| 101 | + if (lookForBOM) { |
| 102 | + if (newData.charCodeAt(0) === 0xfeff) { |
| 103 | + dataIndex++; |
| 104 | + } |
| 105 | + lookForBOM = false; |
| 106 | + } |
| 107 | + |
| 108 | + var isFinishedLine = false; |
| 109 | + |
| 110 | + for (; dataIndex < dataLen; dataIndex++) { |
| 111 | + var c = data[dataIndex]; |
| 112 | + |
| 113 | + if (c === '\n' || c === '\r') { |
| 114 | + if (!isQuoted || !multiline) { |
| 115 | + if (lastLineEndCR && c === '\n') { |
| 116 | + lastLineEndCR = false; |
| 117 | + continue; |
| 118 | + } |
| 119 | + lastLineEndCR = c === '\r'; |
| 120 | + dataIndex++; |
| 121 | + isFinishedLine = true; |
| 122 | + |
| 123 | + if (!multiline) { |
| 124 | + isQuoted = false; |
| 125 | + } |
| 126 | + |
| 127 | + break; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if (isQuoted) { |
| 132 | + if (c === '"') { |
| 133 | + nextIndex = dataIndex + 1; |
| 134 | + |
| 135 | + // Do we have enough data to peek at the next character? |
| 136 | + if (nextIndex >= dataLen && !this._isStreamDone) { |
| 137 | + // Wait for more data to arrive |
| 138 | + break; |
| 139 | + } |
| 140 | + |
| 141 | + if (nextIndex < dataLen && data[nextIndex] === '"') { |
| 142 | + column += '"'; |
| 143 | + dataIndex++; |
| 144 | + } else { |
| 145 | + isQuoted = false; |
| 146 | + } |
| 147 | + } |
| 148 | + else { |
| 149 | + column += c; |
| 150 | + } |
| 151 | + } |
| 152 | + else { |
| 153 | + if (c === delimiter) { |
| 154 | + columns.push(column); |
| 155 | + column = ''; |
| 156 | + } |
| 157 | + else if (c === '"' && allowQuotes) { |
| 158 | + if (column.length) { |
| 159 | + column += c; |
| 160 | + } |
| 161 | + else { |
| 162 | + isQuoted = true; |
| 163 | + } |
| 164 | + } |
| 165 | + else { |
| 166 | + column += c; |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + if (dataIndex === dataLen) { |
| 172 | + data = null; |
| 173 | + } |
| 174 | + |
| 175 | + if (isFinishedLine || (data === null && this._isStreamDone)) { |
| 176 | + |
| 177 | + if (columns.length || column || data || !this._isStreamDone) { |
| 178 | + |
| 179 | + // We have a row, send it to the callback |
| 180 | + |
| 181 | + // Commit this row |
| 182 | + columns.push(column); |
| 183 | + var row = columns; |
| 184 | + |
| 185 | + // Clear row state data |
| 186 | + columns = []; |
| 187 | + column = ''; |
| 188 | + isQuoted = false; |
| 189 | + |
| 190 | + // Is this row full or empty? |
| 191 | + if (row.length > 1 || row[0].length || !skipEmptyLines) { |
| 192 | + |
| 193 | + // Post processing |
| 194 | + if (postProcessingEnabled) { |
| 195 | + for (var i = 0, rowSize = row.length; i < rowSize; i++) { |
| 196 | + row[i] = postProcessColumn(row[i]); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + // Emit the parsed row |
| 201 | + //noinspection JSUnresolvedFunction |
| 202 | + this.push(row); |
| 203 | + } |
| 204 | + |
| 205 | + // Look to see if there are more rows in available data |
| 206 | + this._processChunk(); |
| 207 | + |
| 208 | + } else { |
| 209 | + // We just ran into a newline at the end of the file, ignore it |
| 210 | + } |
| 211 | + |
| 212 | + } else { |
| 213 | + |
| 214 | + if (data) { |
| 215 | + |
| 216 | + // Let more data come in. |
| 217 | + // We are probably waiting for a "peek" at the next character |
| 218 | + |
| 219 | + } else { |
| 220 | + |
| 221 | + // We have probably hit end of file. |
| 222 | + // Let the end event come in. |
| 223 | + |
| 224 | + } |
| 225 | + |
| 226 | + } |
| 227 | + |
| 228 | + }; |
| 229 | + |
| 230 | + Transform.call(this, options); |
| 231 | +}; |
| 232 | + |
| 233 | +util.inherits(CsvReadableStream, Transform); |
| 234 | + |
| 235 | +//noinspection JSUnusedGlobalSymbols |
| 236 | +CsvReadableStream.prototype._transform = function (chunk, enc, cb) { |
| 237 | + |
| 238 | + this._processChunk(chunk); |
| 239 | + |
| 240 | + cb(); |
| 241 | +}; |
| 242 | + |
| 243 | +//noinspection JSUnusedGlobalSymbols |
| 244 | +CsvReadableStream.prototype._flush = function (cb) { |
| 245 | + |
| 246 | + this._isStreamDone = true; |
| 247 | + |
| 248 | + this._processChunk(); |
| 249 | + |
| 250 | + cb(); |
| 251 | +}; |
| 252 | + |
| 253 | +/** |
| 254 | + * @module |
| 255 | + * @type {CsvReadableStream} |
| 256 | + */ |
| 257 | +module.exports = CsvReadableStream; |
0 commit comments