# All files / csv2json/src Result.ts

87.32% Statements 62/71
79.25% Branches 42/53
86.67% Functions 13/15
86.96% Lines 60/69

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

    1x

    1x
    1x
    94x

    1x
    102x
    63x

    102x

    1x
    155x

    73x
    73x
    94x
    94x

    94x
    94x
    31x

    31x

    31x
    31x

    63x
    63x

    1x
    94x
    73x

    94x

    1x
    6x

    6x
    2x

    1x
    61x
    46x
    46x

    61x

    1x

    31x

    31x
    23x
    23x
    23x
    23x

    23x

    23x
    1x

    23x
    10592x
    10592x
    10592x
    10592x
    1x

    23x

    8x
    8x
    10584x
    10584x

    8x

    10586x
    10584x

    2x

     import { Converter } from "./Converter";
    import { ProcessLineResult } from "./Processor";
    import P from "bluebird";
    import CSVError from "./CSVError";

    export class Result {
      private get needEmitLine(): boolean {
        return !!this.converter.parseRuntime.subscribe && !!this.converter.parseRuntime.subscribe.onNext || this.needPushDownstream
      }
      private _needPushDownstream?: boolean;
      private get needPushDownstream(): boolean {
        if (this._needPushDownstream === undefined) {
          this._needPushDownstream = this.converter.listeners("data").length > 0 || this.converter.listeners("readable").length > 0;
        }
        return this._needPushDownstream;
      }
      private get needEmitAll(): boolean {
        return !!this.converter.parseRuntime.then;
      }
      private finalResult: any[] = [];
      constructor(private converter: Converter) { }
      processResult(resultLines: ProcessLineResult[]): P<any> {
        const startPos = this.converter.parseRuntime.parsedLineNumber;
        // let prom: P<any>;
        return new P((resolve, reject) => {
          if (this.needEmitLine) {
            processLineByLine(
              resultLines,
              this.converter,
              0,
              this.needPushDownstream,
              (err) => {
                Iif (err) {
                  reject(err);
                } else {
                  this.appendFinalResult(resultLines);
                  resolve();
                }
              },
            )
          } else {
            this.appendFinalResult(resultLines);
            resolve();
          }
        })
      }
      appendFinalResult(lines: any[]) {
        if (this.needEmitAll) {
          this.finalResult = this.finalResult.concat(lines);
        }
        this.converter.parseRuntime.parsedLineNumber += lines.length;
      }
      processError(err: CSVError) {
        Iif (this.converter.parseRuntime.subscribe && this.converter.parseRuntime.subscribe.onError) {
          this.converter.parseRuntime.subscribe.onError(err);
        }
        if (this.converter.parseRuntime.then && this.converter.parseRuntime.then.onrejected) {
          this.converter.parseRuntime.then.onrejected(err);
        }
      }
      endProcess() {
        if (this.needEmitAll) {
          Eif (this.converter.parseRuntime.then && this.converter.parseRuntime.then.onfulfilled) {
            this.converter.parseRuntime.then.onfulfilled(this.finalResult);
          }
        }
        Iif (this.converter.parseRuntime.subscribe && this.converter.parseRuntime.subscribe.onCompleted) {
          this.converter.parseRuntime.subscribe.onCompleted();
        }
      }
    }

    function processLineByLine(
      lines: ProcessLineResult[],

      conv: Converter,
      offset: number,
      needPushDownstream: boolean,
      cb: (err?) => void,
    ) {
      Iif (lines.length === 0) {
        cb();
      } else {
        if (conv.parseRuntime.subscribe && conv.parseRuntime.subscribe.onNext) {
          const hook = conv.parseRuntime.subscribe.onNext;
          const nextLine = lines[offset];
          const res = hook(nextLine, conv.parseRuntime.parsedLineNumber + offset);
          offset++;
          // if (isAsync === undefined) {
          Iif (res && res.then) {
            res.then(function () {
              processRecursive(lines, hook, conv, offset, needPushDownstream, cb, nextLine);
            }, cb);
          } else {
            // processRecursive(lines, hook, conv, offset, needPushDownstream, cb, nextLine, false);
            if (needPushDownstream){
              pushDownstream(conv,nextLine);
            }
            while (offset<lines.length){
              const line=lines[offset];
              hook(line, conv.parseRuntime.parsedLineNumber + offset);
              offset++;
              if (needPushDownstream){
                pushDownstream(conv,line);
              }
            }
            cb();
          }
          // } else if (isAsync === true) {
          //   (res as PromiseLike<void>).then(function () {
          //     processRecursive(lines, hook, conv, offset, needPushDownstream, cb, nextLine, true);
          //   }, cb);
          // } else if (isAsync === false) {
          //   processRecursive(lines, hook, conv, offset, needPushDownstream, cb, nextLine, false);
          // }
        } else {
          Eif (needPushDownstream) {
            while (offset<lines.length) {
              const line = lines[offset++];
              pushDownstream(conv, line);
            }

          }
          cb();
        }

      }
    }

    function processRecursive(
      lines: ProcessLineResult[],
      hook: (data: any, lineNumber: number) => void | PromiseLike<void>,
      conv: Converter,
      offset: number,
      needPushDownstream: boolean,
      cb: (err?) => void,
      res: ProcessLineResult,
    ) {
      if (needPushDownstream) {
        pushDownstream(conv, res);
      }
      processLineByLine(lines, conv, offset, needPushDownstream, cb);
    }
    function pushDownstream(conv: Converter, res: ProcessLineResult) {
      if (typeof res === "object" && !conv.options.objectMode) {
        conv.push(JSON.stringify(res) + "\n", "utf8");
      } else {
        conv.push(res);
      }
    }