All files Result.ts

94.37% Statements 67/71
88.68% Branches 47/53
100% Functions 15/15
94.2% Lines 65/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 147x     1x 158x 106x   158x   1x 251x   120x 120x 147x 147x   147x 147x 54x           54x     54x 54x         93x 93x       1x 147x 114x   147x   1x 9x     9x 5x     1x 104x 79x 79x     104x       1x                   58x 3x   55x 44x 44x 44x 44x   44x 4x 4x       40x 1x   40x 15896x 15896x 15896x 15896x 1x     40x                   11x 11x 15874x 15874x       11x                             4x     4x     15876x 10584x   5292x    
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,
) {
  if (offset >= lines.length) {
    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) {
      if (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,
) {
  Iif (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);
  }
}