# All files ProcessorLocal.ts

98.09% Statements 154/157
93.75% Branches 90/96
100% Functions 20/20
97.99% Lines 146/149

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    2681x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

    86x
    73x
    73x
    36x
    36x
    36x

    36x
    5x

    31x

    37x

    1x
    76x

    86x
    86x
    86x
    1x
    170x
    76x

    170x

    86x
    86x
    1x
    75x
    75x

    75x

    306x

    171x
    36x

    135x

    171x

    171x
    5x

    166x

    171x
    170x

    1x

    170x
    170x
    170x
    170x
    113x

    170x
    2x
    2x

    170x
    1x

    170x
    170x
    134x

    36x
    36x

    170x
    122x
    122x
    3x

    119x

    122x
    122x

    76x

    46x

    48x

    1x
    76x
    8x
    4x

    4x

    68x
    68x
    68x
    68x
    68x
    68x
    67x
    67x
    67x

    1x

    68x

    68x
    1x

    67x
    2x

    65x

    75x
    5x

    75x
    4x
    4x

    75x

    1x
    5x
    5x
    5x
    5x
    25x
    8x
    3x
    1x

    2x

    5x

    17x
    17x
    6x

    5x

    1x
    121x
    1x

    120x
    120x
    120x
    2x

    118x

    1x
    322x
    322x
    154x

    168x

    3x
    3x
    3x
    3x

    3x

    1x

    5x
    2x

    3x
    3x
    3x
    3x
    3x
    2x
    2x
    2x

    1x
    1x
    1x
    1x

    1x

     import { Processor, ProcessLineResult } from "./Processor";
    import P from "bluebird";
    import { prepareData } from "./dataClean";
    import getEol from "./getEol";
    import { stringToLines } from "./fileline";
    import { bufFromString, filterArray } from "./util";
    import { RowSplit } from "./rowSplit";
    import lineToJson from "./lineToJson";
    import { ParseRuntime } from "./ParseRuntime";
    import CSVError from "./CSVError";

    export class ProcessorLocal extends Processor {
      flush(): P<ProcessLineResult[]> {
        if (this.runtime.csvLineBuffer && this.runtime.csvLineBuffer.length > 0) {
          const buf = this.runtime.csvLineBuffer;
          this.runtime.csvLineBuffer = undefined;
          return this.process(buf, true)
            .then((res) => {
              if (this.runtime.csvLineBuffer && this.runtime.csvLineBuffer.length > 0) {
                return P.reject(CSVError.unclosed_quote(this.runtime.parsedLineNumber, this.runtime.csvLineBuffer.toString()))
              } else {
                return P.resolve(res);
              }
            })
        } else {
          return P.resolve([]);
        }
      }
      destroy(): P<void> {
        return P.resolve();
      }
      private rowSplit: RowSplit = new RowSplit(this.converter);
      private eolEmitted = false;
      private _needEmitEol?: boolean = undefined;
      private get needEmitEol() {
        if (this._needEmitEol === undefined) {
          this._needEmitEol = this.converter.listeners("eol").length > 0;
        }
        return this._needEmitEol;
      }
      private headEmitted = false;
      private _needEmitHead?: boolean = undefined;
      private get needEmitHead() {
        Eif (this._needEmitHead === undefined) {
          this._needEmitHead = this.converter.listeners("header").length > 0;
        }
        return this._needEmitHead;

      }
      process(chunk: Buffer, finalChunk = false): P<ProcessLineResult[]> {
        let csvString: string;
        if (finalChunk) {
          csvString = chunk.toString();
        } else {
          csvString = prepareData(chunk, this.converter.parseRuntime);

        }
        return P.resolve()
          .then(() => {
            if (this.runtime.preRawDataHook) {
              return this.runtime.preRawDataHook(csvString);
            } else {
              return csvString;
            }
          })
          .then((csv) => {
            if (csv && csv.length > 0) {
              return this.processCSV(csv, finalChunk);
            } else {
              return P.resolve([]);
            }
          })
      }
      private processCSV(csv: string, finalChunk: boolean): P<ProcessLineResult[]> {
        const params = this.params;
        const runtime = this.runtime;
        if (!runtime.eol) {
          getEol(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();
        }
        const stringToLineResult = stringToLines(csv, runtime);
        if (!finalChunk) {
          this.prependLeftBuf(bufFromString(stringToLineResult.partial));
        } else {
          stringToLineResult.lines.push(stringToLineResult.partial);
          stringToLineResult.partial = "";
        }
        if (stringToLineResult.lines.length > 0) {
          let prom: P<string[]>;
          if (runtime.preFileLineHook) {
            prom = this.runPreLineHook(stringToLineResult.lines);
          } else {
            prom = P.resolve(stringToLineResult.lines);
          }
          return prom.then((lines) => {
            if (!runtime.started
              && !this.runtime.headers
            ) {
              return this.processDataWithHead(lines);
            } else {
              return this.processCSVBody(lines);
            }

          })

        } else {

          return P.resolve([]);
        }

      }
      private processDataWithHead(lines: string[]): ProcessLineResult[] {
        if (this.params.noheader) {
          if (this.params.headers) {
            this.runtime.headers = this.params.headers;
          } else {
            this.runtime.headers = [];
          }
        } else {
          let left = "";
          let headerRow: string[] = [];
          while (lines.length) {
            const line = left + lines.shift();
            const row = this.rowSplit.parse(line);
            if (row.closed) {
              headerRow = row.cells;
              left = "";
              break;
            } else {
              left = line + getEol(line, this.runtime);
            }
          }
          this.prependLeftBuf(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);
      }
      private filterHeader() {
        this.runtime.selectedColumns = [];
        Eif (this.runtime.headers) {
          const headers = this.runtime.headers;
          for (let 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 Eif (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 = filterArray(this.runtime.headers, this.runtime.selectedColumns);
        }

      }
      private processCSVBody(lines: string[]): ProcessLineResult[] {
        if (this.params.output === "line") {
          return lines;
        } else {
          const result = this.rowSplit.parseMultiLines(lines);
          this.prependLeftBuf(bufFromString(result.partial));
          if (this.params.output === "csv") {
            return result.rowsCells;
          } else {
            return lineToJson(result.rowsCells, this.converter);
          }
        }

        // var jsonArr = linesToJson(lines.lines, params, this.recordNum);
        // this.processResult(jsonArr);
        // this.lastIndex += jsonArr.length;
        // this.recordNum += jsonArr.length;
      }

      private prependLeftBuf(buf: Buffer) {
        Eif (buf) {
          if (this.runtime.csvLineBuffer) {
            this.runtime.csvLineBuffer = Buffer.concat([buf, this.runtime.csvLineBuffer]);
          } else {
            this.runtime.csvLineBuffer = buf;
          }
        }

      }
      private runPreLineHook(lines: string[]): P<string[]> {
        return new P((resolve, reject) => {
          processLineHook(lines, this.runtime, 0, (err) => {
            Iif (err) {
              reject(err);
            } else {
              resolve(lines);
            }
          })
        });
      }
    }

    function processLineHook(lines: string[], runtime: ParseRuntime, offset: number,
      cb: (err?) => void
    ) {
      if (offset >= lines.length) {
        cb();
      } else {
        Eif (runtime.preFileLineHook) {
          const line = lines[offset];
          const res = runtime.preFileLineHook(line, runtime.parsedLineNumber + offset);
          offset++;
          if (res && (res as PromiseLike<string>).then) {
            (res as PromiseLike<string>).then((value) => {
              lines[offset - 1] = value;
              processLineHook(lines, runtime, offset, cb);
            });
          } else {
            lines[offset - 1] = res as string;
            while (offset < lines.length) {
              lines[offset] = runtime.preFileLineHook(lines[offset], runtime.parsedLineNumber + offset) as string;
              offset++;
            }
            cb();
          }
        } else {
          cb();
        }
      }
    }