# All files / csv2json/src Converter.ts

94% Statements 94/100
84.62% Branches 22/26
96.43% Functions 27/28
93.62% Lines 88/94

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

    1x
    1x
    1x
    1x

    1x
    1x
    2x

    1x
    1x

    1x

    17x

    17x

    5x
    5x

    5x
    5x
    5x
    5x

    5x

    1x
    25x
    25x

    1x
    16x
    16x
    16x
    16x
    37x
    16x

    21x
    21x
    21x

    16x

    49x
    49x
    49x

    46x
    46x

    2x
    2x

    1x
    165734x

    1x
    436312x

    217x
    73x
    73x
    73x
    73x
    73x

    73x

    73x

    6x
    6x
    6x

    73x

    124x
    124x

    122x
    70x
    70x

    122x

    2x
    2x
    2x
    2x

    65x
    65x
    28x
    28x
    28x

    28x
    24x

    28x
    4x

    24x

    37x

    1x
    61x
    61x
    61x

    1x
    4x

    1x

     import { Transform, TransformOptions, Readable } from "stream";
    import { CSVParseParam, mergeParams } from "./Parameters";
    import { ParseRuntime, initParseRuntime } from "./ParseRuntime";
    import P from "bluebird";
    import { Worker } from "./Worker";
    import { stringToLines } from "./fileline";
    import { map } from "lodash/map";
    import { RowSplit, RowSplitResult } from "./rowSplit";
    import getEol from "./getEol";
    import lineToJson, { JSONResult } from "./lineToJson";
    import { Processor, ProcessLineResult } from "./Processor";
    import { ProcessorFork } from "./ProcessFork";
    import { ProcessorLocal } from "./ProcessorLocal";
    import { Result } from "./Result";
    import CSVError from "./CSVError";
    import { bufFromString } from "./util";
    export class Converter extends Transform {
      preRawData(onRawData: PreRawDataCallback) {
        this.runtime.preRawDataHook = onRawData;
      }
      preFileLine(onFileLine: PreFileLineCallback) {
        this.runtime.preFileLineHook = onFileLine;
      }
      subscribe(
        onNext?: (data: any, lineNumber: number) => void | PromiseLike<void>,
        onError?: (err: CSVError) => void,
        onCompleted?: () => void): Converter {
        this.parseRuntime.subscribe = {
          onNext,
          onError,
          onCompleted
        }
        return this;
      }
      fromFile(filePath: string, options?: string | CreateReadStreamOption | undefined): Converter {
        const fs = require("fs");
        // var rs = null;
        // this.wrapCallback(cb, function () {
        //   if (rs && rs.destroy) {
        //     rs.destroy();
        //   }
        // });
        fs.exists(filePath, (exist) => {
          Eif (exist) {
            const rs = fs.createReadStream(filePath, options);
            rs.pipe(this);
          } else {
            this.emit('error', new Error("File does not exist. Check to make sure the file path to your csv is correct."));
          }
        });
        return this;
      }
      fromStream(readStream: Readable): Converter {
        readStream.pipe(this);
        return this;
      }
      fromString(csvString: string): Converter {
        const csv = csvString.toString();
        const read = new Readable();
        let idx = 0;
        read._read = function (size) {
          if (idx >= csvString.length) {
            this.push(null);
          } else {
            const str = csvString.substr(idx, size);
            this.push(str);
            idx += size;
          }
        }
        return this.fromStream(read);
      }
      then<TResult1 = any[], TResult2 = never>(onfulfilled?: (value: any[]) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>): PromiseLike<TResult1 | TResult2> {
        return new P((resolve, reject) => {
          this.parseRuntime.then = {
            onfulfilled: (value: any[]) => {
              Eif (onfulfilled) {
                resolve(onfulfilled(value));
              } else {
                resolve(value as any);
              }
            },
            onrejected: (err: Error) => {
              Eif (onrejected) {
                resolve(onrejected(err));
              } else {
                reject(err);
              }
            }
          }
        });
      }
      public get parseParam(): CSVParseParam {
        return this.params;
      }
      public get parseRuntime(): ParseRuntime {
        return this.runtime;
      }
      private params: CSVParseParam;
      private runtime: ParseRuntime;
      private processor: Processor;
      private result: Result;
      constructor(param?: Partial<CSVParseParam>, public options: TransformOptions = {}) {
        super(options);
        this.params = mergeParams(param);
        this.runtime = initParseRuntime(this);
        this.result = new Result(this);
        Iif (this.params.fork) {
          this.processor = new ProcessorFork(this);
        } else {
          this.processor = new ProcessorLocal(this);
        }
        this.once("error", (err: any) => {
          // console.log("BBB");
          this.result.processError(err);
          setTimeout(() => {
            this.emit("done", err);
          },0);

        });

        return this;
      }
      _transform(chunk: any, encoding: string, cb: Function) {
        this.processor.process(chunk)
          .then((result) => {
            if (result.length > 0) {
              this.runtime.started = true;
              return this.result.processResult(result);
            }
          })
          .then(() => {
            cb();
          }, (error) => {
            this.runtime.hasError = true;
            this.runtime.error = error;
            this.emit("error", error);
            cb();
          });
      }
      _flush(cb: Function) {
        if (this.runtime.csvLineBuffer && this.runtime.csvLineBuffer.length > 0) {
          const buf = this.runtime.csvLineBuffer;
          this.runtime.csvLineBuffer = undefined;
          this.processor.process(buf, true)
            .then((result) => {
              if (result.length > 0) {
                return this.result.processResult(result);
              }
            })
            .then(() => {
              if (this.runtime.csvLineBuffer && this.runtime.csvLineBuffer.length > 0) {
                this.emit("error", CSVError.unclosed_quote(this.parsedLineNumber, this.runtime.csvLineBuffer.toString()));
              } else {
                this.processEnd(cb);
              }
              // cb();

            }, (err) => {
              this.emit("error", err);
              cb();
            })
        } else {
          this.processEnd(cb);
        }
      }
      private processEnd(cb) {
        this.result.endProcess();
        this.emit("done");
        cb();
      }
      get parsedLineNumber(): number {
        return this.runtime.parsedLineNumber;
      }
    }
    export interface CreateReadStreamOption {
      flags?: string;
      encoding?: string;
      fd?: number;
      mode?: number;
      autoClose?: boolean;
      start?: number;
      end?: number;
      highWaterMark?: number;
    }
    export type CallBack = (err: Error, data: Array<any>) => void;

    export type PreFileLineCallback = (line: string, lineNumber: number) => string | PromiseLike<string>;
    export type PreRawDataCallback = (csvString: string) => string | PromiseLike<string>;