# All files lineToJson.ts

93.94% Statements 93/99
91.57% Branches 76/83
100% Functions 14/14
93.94% Lines 93/99

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

    1x

    1x

    1x
    118x
    118x
    32118x
    32116x
    32107x

    116x

    32118x
    2x

    32116x
    32116x
    32116x
    32107x

    9x

    32116x
    32116x

    32116x
    65845x

    65845x
    7x

    65838x

    65838x
    65838x
    51x

    65838x
    65838x
    10x
    10x
    8x

    65828x
    75x
    75x

    65828x
    65828x

    32116x
    32107x

    9x

    1x

    65838x
    65275x

    563x
    563x
    556x

    7x
    1x

    7x
    6x
    6x
    6x
    6x

    1x
    1x

    65836x
    562x
    3x

    559x
    103x
    1x

    102x

    456x

    65836x

    65047x

    789x

    75x
    9x
    66x

    66x

    66x
    66x

    15x
    15x
    1x

    14x

    58x

    75x
    75x
    13x

    62x
    13x
    49x
    4x
    45x
    5x

    40x

    4x
    4x
    2x

    2x

    5x
    5x

     import { Converter } from "./Converter";
    import CSVError from "./CSVError";
    import { CellParser, ColumnParam } from "./Parameters";
    import set from "lodash/set";
    import { ParseRuntime } from "./ParseRuntime";

    var numReg = /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;

    export default function (csvRows: string[][], conv: Converter): JSONResult[] {
      const res: JSONResult[] = [];
      for (let i = 0, len = csvRows.length; i < len; i++) {
        const r = processRow(csvRows[i], conv, i);
        if (r) {
          res.push(r);
        }
      }
      return res;
    };
    export type JSONResult = {
      [key: string]: any
    }

    function processRow(row: string[], conv: Converter, index): JSONResult | null {

      if (conv.parseParam.checkColumn && conv.parseRuntime.headers && row.length !== conv.parseRuntime.headers.length) {
        throw (CSVError.column_mismatched(conv.parseRuntime.parsedLineNumber + index))
      }

      const headRow = conv.parseRuntime.headers || [];
      const resultRow = convertRowToJson(row, headRow, conv);
      if (resultRow) {
        return resultRow;
      } else {
        return null;
      }
    }

    function convertRowToJson(row: string[], headRow: string[], conv: Converter): { [key: string]: any } | null {
      let hasValue = false;
      const resultRow = {};

      for (let i = 0, len = row.length; i < len; i++) {
        let item = row[i];

        if (conv.parseParam.ignoreEmpty && item === '') {
          continue;
        }
        hasValue = true;

        let head = headRow[i];
        if (!head || head === "") {
          head = headRow[i] = "field" + (i + 1);
        }
        const convFunc = getConvFunc(head, i, conv);
        if (convFunc) {
          const convRes = convFunc(item, head, resultRow, row, i);
          if (convRes !== undefined) {
            setPath(resultRow, head, convRes, conv,i);
          }
        } else {
          // var flag = getFlag(head, i, param);
          // if (flag === 'omit') {
          //   continue;
          // }
          if (conv.parseParam.checkType) {
            const convertFunc = checkType(item, head, i, conv);
            item = convertFunc(item);
          }
          Eif (item !== undefined) {
            setPath(resultRow, head, item, conv,i);
          }
        }
      }
      if (hasValue) {
        return resultRow;
      } else {
        return null;
      }
    }

    const builtInConv: { [key: string]: CellParser } = {
      "string": stringType,
      "number": numberType,
      "omit": function () { }
    }
    function getConvFunc(head: string, i: number, conv: Converter): CellParser | null {
      if (conv.parseRuntime.columnConv[i] !== undefined) {
        return conv.parseRuntime.columnConv[i];
      } else {
        let flag = conv.parseParam.colParser[head];
        if (flag === undefined) {
          return conv.parseRuntime.columnConv[i] = null;
        }
        if (typeof flag === "object") {
          flag = (flag as ColumnParam).cellParser || "string";
        }
        if (typeof flag === "string") {
          flag = flag.trim().toLowerCase();
          const builtInFunc = builtInConv[flag];
          Eif (builtInFunc) {
            return conv.parseRuntime.columnConv[i] = builtInFunc;
          } else {
            return conv.parseRuntime.columnConv[i] = null;
          }
        } else Eif (typeof flag === "function") {
          return conv.parseRuntime.columnConv[i] = flag;
        } else {
          return conv.parseRuntime.columnConv[i] = null;
        }
      }
    }
    function setPath(resultJson: any, head: string, value: any, conv: Converter,headIdx:number) {
      if (!conv.parseRuntime.columnValueSetter[headIdx]) {
        if (conv.parseParam.flatKeys) {
          conv.parseRuntime.columnValueSetter[headIdx] = flatSetter;
        } else {
          if (head.indexOf(".") > -1) {
            if (conv.parseParam.colParser[head] && (conv.parseParam.colParser[head] as ColumnParam).flat) {
              conv.parseRuntime.columnValueSetter[headIdx] = flatSetter;
            } else {
              conv.parseRuntime.columnValueSetter[headIdx] = jsonSetter;
            }
          } else {
            conv.parseRuntime.columnValueSetter[headIdx] = flatSetter;
          }
        }
      }
      conv.parseRuntime.columnValueSetter[headIdx](resultJson, head, value);
      // flatSetter(resultJson, head, value);

    }
    function flatSetter(resultJson: any, head: string, value: any) {
      resultJson[head] = value;
    }
    function jsonSetter(resultJson: any, head: string, value: any) {
      set(resultJson, head, value);
    }

    function checkType(item: string, head: string, headIdx: number, conv: Converter): Function {
      if (conv.parseRuntime.headerType[headIdx]) {
        return conv.parseRuntime.headerType[headIdx];
      } else Iif (head.indexOf('number#!') > -1) {
        return conv.parseRuntime.headerType[headIdx] = numberType;
      } else Iif (head.indexOf('string#!') > -1) {
        return conv.parseRuntime.headerType[headIdx] = stringType;
      } else Eif (conv.parseParam.checkType) {
        return conv.parseRuntime.headerType[headIdx] = dynamicType;
      } else {
        return conv.parseRuntime.headerType[headIdx] = stringType;
      }
    }

    function numberType(item) {
      var rtn = parseFloat(item);
      if (isNaN(rtn)) {
        return item;
      }
      return rtn;
    }

    function stringType(item: string): string {
      return item.toString();
    }

    function dynamicType(item) {
      var trimed = item.trim();
      if (trimed === "") {
        return stringType(item);
      }
      if (numReg.test(trimed)) {
        return numberType(item);
      } else if (trimed.length === 5 && trimed.toLowerCase() === "false" || trimed.length === 4 && trimed.toLowerCase() === "true") {
        return booleanType(item);
      } else if (trimed[0] === "{" && trimed[trimed.length - 1] === "}" || trimed[0] === "[" && trimed[trimed.length - 1] === "]") {
        return jsonType(item);
      } else {
        return stringType(item);
      }
    }

    function booleanType(item) {
      var trimed = item.trim();
      if (trimed.length === 5 && trimed.toLowerCase() === "false") {
        return false;
      } else {
        return true;
      }
    }

    function jsonType(item) {
      try {
        return JSON.parse(item);
      } catch (e) {
        return item;
      }
    }