ProcessorLocal.js
11.3 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var Processor_1 = require("./Processor");
var bluebird_1 = __importDefault(require("bluebird"));
var dataClean_1 = require("./dataClean");
var getEol_1 = __importDefault(require("./getEol"));
var fileline_1 = require("./fileline");
var util_1 = require("./util");
var rowSplit_1 = require("./rowSplit");
var lineToJson_1 = __importDefault(require("./lineToJson"));
var CSVError_1 = __importDefault(require("./CSVError"));
var ProcessorLocal = /** @class */ (function (_super) {
__extends(ProcessorLocal, _super);
function ProcessorLocal() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.rowSplit = new rowSplit_1.RowSplit(_this.converter);
_this.eolEmitted = false;
_this._needEmitEol = undefined;
_this.headEmitted = false;
_this._needEmitHead = undefined;
return _this;
}
ProcessorLocal.prototype.flush = function () {
var _this = this;
if (this.runtime.csvLineBuffer && this.runtime.csvLineBuffer.length > 0) {
var buf = this.runtime.csvLineBuffer;
this.runtime.csvLineBuffer = undefined;
return this.process(buf, true)
.then(function (res) {
if (_this.runtime.csvLineBuffer && _this.runtime.csvLineBuffer.length > 0) {
return bluebird_1.default.reject(CSVError_1.default.unclosed_quote(_this.runtime.parsedLineNumber, _this.runtime.csvLineBuffer.toString()));
}
else {
return bluebird_1.default.resolve(res);
}
});
}
else {
return bluebird_1.default.resolve([]);
}
};
ProcessorLocal.prototype.destroy = function () {
return bluebird_1.default.resolve();
};
Object.defineProperty(ProcessorLocal.prototype, "needEmitEol", {
get: function () {
if (this._needEmitEol === undefined) {
this._needEmitEol = this.converter.listeners("eol").length > 0;
}
return this._needEmitEol;
},
enumerable: true,
configurable: true
});
Object.defineProperty(ProcessorLocal.prototype, "needEmitHead", {
get: function () {
if (this._needEmitHead === undefined) {
this._needEmitHead = this.converter.listeners("header").length > 0;
}
return this._needEmitHead;
},
enumerable: true,
configurable: true
});
ProcessorLocal.prototype.process = function (chunk, finalChunk) {
var _this = this;
if (finalChunk === void 0) { finalChunk = false; }
var csvString;
if (finalChunk) {
csvString = chunk.toString();
}
else {
csvString = dataClean_1.prepareData(chunk, this.converter.parseRuntime);
}
return bluebird_1.default.resolve()
.then(function () {
if (_this.runtime.preRawDataHook) {
return _this.runtime.preRawDataHook(csvString);
}
else {
return csvString;
}
})
.then(function (csv) {
if (csv && csv.length > 0) {
return _this.processCSV(csv, finalChunk);
}
else {
return bluebird_1.default.resolve([]);
}
});
};
ProcessorLocal.prototype.processCSV = function (csv, finalChunk) {
var _this = this;
var params = this.params;
var runtime = this.runtime;
if (!runtime.eol) {
getEol_1.default(csv, runtime);
}
if (this.needEmitEol && !this.eolEmitted && runtime.eol) {
this.converter.emit("eol", runtime.eol);
this.eolEmitted = true;
}
// trim csv file has initial blank lines.
if (params.ignoreEmpty && !runtime.started) {
csv = csv.trimLeft();
}
var stringToLineResult = fileline_1.stringToLines(csv, runtime);
if (!finalChunk) {
this.prependLeftBuf(util_1.bufFromString(stringToLineResult.partial));
}
else {
stringToLineResult.lines.push(stringToLineResult.partial);
stringToLineResult.partial = "";
}
if (stringToLineResult.lines.length > 0) {
var prom = void 0;
if (runtime.preFileLineHook) {
prom = this.runPreLineHook(stringToLineResult.lines);
}
else {
prom = bluebird_1.default.resolve(stringToLineResult.lines);
}
return prom.then(function (lines) {
if (!runtime.started
&& !_this.runtime.headers) {
return _this.processDataWithHead(lines);
}
else {
return _this.processCSVBody(lines);
}
});
}
else {
return bluebird_1.default.resolve([]);
}
};
ProcessorLocal.prototype.processDataWithHead = function (lines) {
if (this.params.noheader) {
if (this.params.headers) {
this.runtime.headers = this.params.headers;
}
else {
this.runtime.headers = [];
}
}
else {
var left = "";
var headerRow = [];
while (lines.length) {
var line = left + lines.shift();
var row = this.rowSplit.parse(line);
if (row.closed) {
headerRow = row.cells;
left = "";
break;
}
else {
left = line + getEol_1.default(line, this.runtime);
}
}
this.prependLeftBuf(util_1.bufFromString(left));
if (headerRow.length === 0) {
return [];
}
if (this.params.headers) {
this.runtime.headers = this.params.headers;
}
else {
this.runtime.headers = headerRow;
}
}
if (this.runtime.needProcessIgnoreColumn || this.runtime.needProcessIncludeColumn) {
this.filterHeader();
}
if (this.needEmitHead && !this.headEmitted) {
this.converter.emit("header", this.runtime.headers);
this.headEmitted = true;
}
return this.processCSVBody(lines);
};
ProcessorLocal.prototype.filterHeader = function () {
this.runtime.selectedColumns = [];
if (this.runtime.headers) {
var headers = this.runtime.headers;
for (var i = 0; i < headers.length; i++) {
if (this.params.ignoreColumns) {
if (this.params.ignoreColumns.test(headers[i])) {
if (this.params.includeColumns && this.params.includeColumns.test(headers[i])) {
this.runtime.selectedColumns.push(i);
}
else {
continue;
}
}
else {
this.runtime.selectedColumns.push(i);
}
}
else if (this.params.includeColumns) {
if (this.params.includeColumns.test(headers[i])) {
this.runtime.selectedColumns.push(i);
}
}
else {
this.runtime.selectedColumns.push(i);
}
// if (this.params.includeColumns && this.params.includeColumns.test(headers[i])){
// this.runtime.selectedColumns.push(i);
// }else{
// if (this.params.ignoreColumns && this.params.ignoreColumns.test(headers[i])){
// continue;
// }else{
// if (this.params.ignoreColumns && !this.params.includeColumns){
// this.runtime.selectedColumns.push(i);
// }
// }
// }
}
this.runtime.headers = util_1.filterArray(this.runtime.headers, this.runtime.selectedColumns);
}
};
ProcessorLocal.prototype.processCSVBody = function (lines) {
if (this.params.output === "line") {
return lines;
}
else {
var result = this.rowSplit.parseMultiLines(lines);
this.prependLeftBuf(util_1.bufFromString(result.partial));
if (this.params.output === "csv") {
return result.rowsCells;
}
else {
return lineToJson_1.default(result.rowsCells, this.converter);
}
}
// var jsonArr = linesToJson(lines.lines, params, this.recordNum);
// this.processResult(jsonArr);
// this.lastIndex += jsonArr.length;
// this.recordNum += jsonArr.length;
};
ProcessorLocal.prototype.prependLeftBuf = function (buf) {
if (buf) {
if (this.runtime.csvLineBuffer) {
this.runtime.csvLineBuffer = Buffer.concat([buf, this.runtime.csvLineBuffer]);
}
else {
this.runtime.csvLineBuffer = buf;
}
}
};
ProcessorLocal.prototype.runPreLineHook = function (lines) {
var _this = this;
return new bluebird_1.default(function (resolve, reject) {
processLineHook(lines, _this.runtime, 0, function (err) {
if (err) {
reject(err);
}
else {
resolve(lines);
}
});
});
};
return ProcessorLocal;
}(Processor_1.Processor));
exports.ProcessorLocal = ProcessorLocal;
function processLineHook(lines, runtime, offset, cb) {
if (offset >= lines.length) {
cb();
}
else {
if (runtime.preFileLineHook) {
var line = lines[offset];
var res = runtime.preFileLineHook(line, runtime.parsedLineNumber + offset);
offset++;
if (res && res.then) {
res.then(function (value) {
lines[offset - 1] = value;
processLineHook(lines, runtime, offset, cb);
});
}
else {
lines[offset - 1] = res;
while (offset < lines.length) {
lines[offset] = runtime.preFileLineHook(lines[offset], runtime.parsedLineNumber + offset);
offset++;
}
cb();
}
}
else {
cb();
}
}
}
//# sourceMappingURL=ProcessorLocal.js.map