# All files Converter.ts

100% Statements 98/98
100% Branches 18/18
100% Functions 29/29
100% Lines 92/92

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

    1x
    1x
    1x

    1x
    1x
    3x
    3x

    1x
    2x
    2x

    1x

    34x

    34x

    8x
    8x

    8x
    8x
    7x
    7x

    1x

    8x

    1x
    46x
    46x

    1x
    30x
    30x
    30x
    30x
    65x
    30x

    35x
    35x
    35x

    30x

    86x
    86x
    86x

    79x
    78x

    1x

    5x
    4x

    1x

    1x
    166228x

    1x
    442655x

    356x
    120x
    120x
    120x
    120x
    120x
    37x

    83x

    120x

    9x
    9x
    9x

    120x
    113x

    120x

    217x
    217x

    215x
    81x
    81x

    215x
    215x

    2x
    2x
    2x
    2x

    110x
    110x

    104x
    66x

    104x

    5x
    5x

    1x
    104x
    104x
    104x

    1x
    1x

    1x

     import { Transform, TransformOptions, Readable } from "stream";
    import { CSVParseParam, mergeParams } from "./Parameters";
    import { ParseRuntime, initParseRuntime } from "./ParseRuntime";
    import P from "bluebird";
    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):Converter {
        this.runtime.preRawDataHook = onRawData;
        return this;
      }
      preFileLine(onFileLine: PreFileLineCallback):Converter {
        this.runtime.preFileLineHook = onFileLine;
        return this;
      }
      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) => {
          if (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[]) => {
              if (onfulfilled) {
                resolve(onfulfilled(value));
              } else {
                resolve(value as any);
              }
            },
            onrejected: (err: Error) => {
              if (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);
        if (this.params.fork) {
          this.processor = new ProcessorFork(this);
        } else {
          this.processor = new ProcessorLocal(this);
        }
        this.once("error", (err: any) => {
          // console.log("BBB");

          setTimeout(() => {
            this.result.processError(err);
            this.emit("done", err);
          },0);

        });
        this.once("done",()=>{
          this.processor.destroy();
        })

        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(() => {
            this.emit("drained");
            cb();
          }, (error) => {
            this.runtime.hasError = true;
            this.runtime.error = error;
            this.emit("error", error);
            cb();
          });
      }
      _flush(cb: Function) {
        this.processor.flush()
        .then((data)=>{
          if (data.length>0){
            return this.result.processResult(data);
          }
        })
        .then(()=>{
          this.processEnd(cb);
        },(err)=>{
          this.emit("error",err);
          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>;