1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return function (d, b) {
|
7 | extendStatics(d, b);
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 | })();
|
12 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
13 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
14 | };
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | var Processor_1 = require("./Processor");
|
17 | var bluebird_1 = __importDefault(require("bluebird"));
|
18 | var dataClean_1 = require("./dataClean");
|
19 | var getEol_1 = __importDefault(require("./getEol"));
|
20 | var fileline_1 = require("./fileline");
|
21 | var util_1 = require("./util");
|
22 | var rowSplit_1 = require("./rowSplit");
|
23 | var lineToJson_1 = __importDefault(require("./lineToJson"));
|
24 | var CSVError_1 = __importDefault(require("./CSVError"));
|
25 | var ProcessorLocal = (function (_super) {
|
26 | __extends(ProcessorLocal, _super);
|
27 | function ProcessorLocal() {
|
28 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
29 | _this.rowSplit = new rowSplit_1.RowSplit(_this.converter);
|
30 | _this.eolEmitted = false;
|
31 | _this._needEmitEol = undefined;
|
32 | _this.headEmitted = false;
|
33 | _this._needEmitHead = undefined;
|
34 | return _this;
|
35 | }
|
36 | ProcessorLocal.prototype.flush = function () {
|
37 | var _this = this;
|
38 | if (this.runtime.csvLineBuffer && this.runtime.csvLineBuffer.length > 0) {
|
39 | var buf = this.runtime.csvLineBuffer;
|
40 | this.runtime.csvLineBuffer = undefined;
|
41 | return this.process(buf, true)
|
42 | .then(function (res) {
|
43 | if (_this.runtime.csvLineBuffer && _this.runtime.csvLineBuffer.length > 0) {
|
44 | return bluebird_1.default.reject(CSVError_1.default.unclosed_quote(_this.runtime.parsedLineNumber, _this.runtime.csvLineBuffer.toString()));
|
45 | }
|
46 | else {
|
47 | return bluebird_1.default.resolve(res);
|
48 | }
|
49 | });
|
50 | }
|
51 | else {
|
52 | return bluebird_1.default.resolve([]);
|
53 | }
|
54 | };
|
55 | ProcessorLocal.prototype.destroy = function () {
|
56 | return bluebird_1.default.resolve();
|
57 | };
|
58 | Object.defineProperty(ProcessorLocal.prototype, "needEmitEol", {
|
59 | get: function () {
|
60 | if (this._needEmitEol === undefined) {
|
61 | this._needEmitEol = this.converter.listeners("eol").length > 0;
|
62 | }
|
63 | return this._needEmitEol;
|
64 | },
|
65 | enumerable: true,
|
66 | configurable: true
|
67 | });
|
68 | Object.defineProperty(ProcessorLocal.prototype, "needEmitHead", {
|
69 | get: function () {
|
70 | if (this._needEmitHead === undefined) {
|
71 | this._needEmitHead = this.converter.listeners("header").length > 0;
|
72 | }
|
73 | return this._needEmitHead;
|
74 | },
|
75 | enumerable: true,
|
76 | configurable: true
|
77 | });
|
78 | ProcessorLocal.prototype.process = function (chunk, finalChunk) {
|
79 | var _this = this;
|
80 | if (finalChunk === void 0) { finalChunk = false; }
|
81 | var csvString;
|
82 | if (finalChunk) {
|
83 | csvString = chunk.toString();
|
84 | }
|
85 | else {
|
86 | csvString = dataClean_1.prepareData(chunk, this.converter.parseRuntime);
|
87 | }
|
88 | return bluebird_1.default.resolve()
|
89 | .then(function () {
|
90 | if (_this.runtime.preRawDataHook) {
|
91 | return _this.runtime.preRawDataHook(csvString);
|
92 | }
|
93 | else {
|
94 | return csvString;
|
95 | }
|
96 | })
|
97 | .then(function (csv) {
|
98 | if (csv && csv.length > 0) {
|
99 | return _this.processCSV(csv, finalChunk);
|
100 | }
|
101 | else {
|
102 | return bluebird_1.default.resolve([]);
|
103 | }
|
104 | });
|
105 | };
|
106 | ProcessorLocal.prototype.processCSV = function (csv, finalChunk) {
|
107 | var _this = this;
|
108 | var params = this.params;
|
109 | var runtime = this.runtime;
|
110 | if (!runtime.eol) {
|
111 | getEol_1.default(csv, runtime);
|
112 | }
|
113 | if (this.needEmitEol && !this.eolEmitted && runtime.eol) {
|
114 | this.converter.emit("eol", runtime.eol);
|
115 | this.eolEmitted = true;
|
116 | }
|
117 |
|
118 | if (params.ignoreEmpty && !runtime.started) {
|
119 | csv = util_1.trimLeft(csv);
|
120 | }
|
121 | var stringToLineResult = fileline_1.stringToLines(csv, runtime);
|
122 | if (!finalChunk) {
|
123 | this.prependLeftBuf(util_1.bufFromString(stringToLineResult.partial));
|
124 | }
|
125 | else {
|
126 | stringToLineResult.lines.push(stringToLineResult.partial);
|
127 | stringToLineResult.partial = "";
|
128 | }
|
129 | if (stringToLineResult.lines.length > 0) {
|
130 | var prom = void 0;
|
131 | if (runtime.preFileLineHook) {
|
132 | prom = this.runPreLineHook(stringToLineResult.lines);
|
133 | }
|
134 | else {
|
135 | prom = bluebird_1.default.resolve(stringToLineResult.lines);
|
136 | }
|
137 | return prom.then(function (lines) {
|
138 | if (!runtime.started
|
139 | && !_this.runtime.headers) {
|
140 | return _this.processDataWithHead(lines);
|
141 | }
|
142 | else {
|
143 | return _this.processCSVBody(lines);
|
144 | }
|
145 | });
|
146 | }
|
147 | else {
|
148 | return bluebird_1.default.resolve([]);
|
149 | }
|
150 | };
|
151 | ProcessorLocal.prototype.processDataWithHead = function (lines) {
|
152 | if (this.params.noheader) {
|
153 | if (this.params.headers) {
|
154 | this.runtime.headers = this.params.headers;
|
155 | }
|
156 | else {
|
157 | this.runtime.headers = [];
|
158 | }
|
159 | }
|
160 | else {
|
161 | var left = "";
|
162 | var headerRow = [];
|
163 | while (lines.length) {
|
164 | var line = left + lines.shift();
|
165 | var row = this.rowSplit.parse(line);
|
166 | if (row.closed) {
|
167 | headerRow = row.cells;
|
168 | left = "";
|
169 | break;
|
170 | }
|
171 | else {
|
172 | left = line + getEol_1.default(line, this.runtime);
|
173 | }
|
174 | }
|
175 | this.prependLeftBuf(util_1.bufFromString(left));
|
176 | if (headerRow.length === 0) {
|
177 | return [];
|
178 | }
|
179 | if (this.params.headers) {
|
180 | this.runtime.headers = this.params.headers;
|
181 | }
|
182 | else {
|
183 | this.runtime.headers = headerRow;
|
184 | }
|
185 | }
|
186 | if (this.runtime.needProcessIgnoreColumn || this.runtime.needProcessIncludeColumn) {
|
187 | this.filterHeader();
|
188 | }
|
189 | if (this.needEmitHead && !this.headEmitted) {
|
190 | this.converter.emit("header", this.runtime.headers);
|
191 | this.headEmitted = true;
|
192 | }
|
193 | return this.processCSVBody(lines);
|
194 | };
|
195 | ProcessorLocal.prototype.filterHeader = function () {
|
196 | this.runtime.selectedColumns = [];
|
197 | if (this.runtime.headers) {
|
198 | var headers = this.runtime.headers;
|
199 | for (var i = 0; i < headers.length; i++) {
|
200 | if (this.params.ignoreColumns) {
|
201 | if (this.params.ignoreColumns.test(headers[i])) {
|
202 | if (this.params.includeColumns && this.params.includeColumns.test(headers[i])) {
|
203 | this.runtime.selectedColumns.push(i);
|
204 | }
|
205 | else {
|
206 | continue;
|
207 | }
|
208 | }
|
209 | else {
|
210 | this.runtime.selectedColumns.push(i);
|
211 | }
|
212 | }
|
213 | else if (this.params.includeColumns) {
|
214 | if (this.params.includeColumns.test(headers[i])) {
|
215 | this.runtime.selectedColumns.push(i);
|
216 | }
|
217 | }
|
218 | else {
|
219 | this.runtime.selectedColumns.push(i);
|
220 | }
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | }
|
233 | this.runtime.headers = util_1.filterArray(this.runtime.headers, this.runtime.selectedColumns);
|
234 | }
|
235 | };
|
236 | ProcessorLocal.prototype.processCSVBody = function (lines) {
|
237 | if (this.params.output === "line") {
|
238 | return lines;
|
239 | }
|
240 | else {
|
241 | var result = this.rowSplit.parseMultiLines(lines);
|
242 | this.prependLeftBuf(util_1.bufFromString(result.partial));
|
243 | if (this.params.output === "csv") {
|
244 | return result.rowsCells;
|
245 | }
|
246 | else {
|
247 | return lineToJson_1.default(result.rowsCells, this.converter);
|
248 | }
|
249 | }
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | };
|
255 | ProcessorLocal.prototype.prependLeftBuf = function (buf) {
|
256 | if (buf) {
|
257 | if (this.runtime.csvLineBuffer) {
|
258 | this.runtime.csvLineBuffer = Buffer.concat([buf, this.runtime.csvLineBuffer]);
|
259 | }
|
260 | else {
|
261 | this.runtime.csvLineBuffer = buf;
|
262 | }
|
263 | }
|
264 | };
|
265 | ProcessorLocal.prototype.runPreLineHook = function (lines) {
|
266 | var _this = this;
|
267 | return new bluebird_1.default(function (resolve, reject) {
|
268 | processLineHook(lines, _this.runtime, 0, function (err) {
|
269 | if (err) {
|
270 | reject(err);
|
271 | }
|
272 | else {
|
273 | resolve(lines);
|
274 | }
|
275 | });
|
276 | });
|
277 | };
|
278 | return ProcessorLocal;
|
279 | }(Processor_1.Processor));
|
280 | exports.ProcessorLocal = ProcessorLocal;
|
281 | function processLineHook(lines, runtime, offset, cb) {
|
282 | if (offset >= lines.length) {
|
283 | cb();
|
284 | }
|
285 | else {
|
286 | if (runtime.preFileLineHook) {
|
287 | var line = lines[offset];
|
288 | var res = runtime.preFileLineHook(line, runtime.parsedLineNumber + offset);
|
289 | offset++;
|
290 | if (res && res.then) {
|
291 | res.then(function (value) {
|
292 | lines[offset - 1] = value;
|
293 | processLineHook(lines, runtime, offset, cb);
|
294 | });
|
295 | }
|
296 | else {
|
297 | lines[offset - 1] = res;
|
298 | while (offset < lines.length) {
|
299 | lines[offset] = runtime.preFileLineHook(lines[offset], runtime.parsedLineNumber + offset);
|
300 | offset++;
|
301 | }
|
302 | cb();
|
303 | }
|
304 | }
|
305 | else {
|
306 | cb();
|
307 | }
|
308 | }
|
309 | }
|
310 |
|
\ | No newline at end of file |