All files / csv2json/src lineToJson.ts

91.92% Statements 91/99
86.75% Branches 72/83
92.86% Functions 13/14
91.92% Lines 91/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 102x 102x 32101x 32099x 32090x     100x               32101x 2x     32099x 32099x 32099x 32090x   9x         32099x 32099x   32099x 65788x   65788x 7x   65781x   65781x 65781x 50x   65781x 65781x 6x 6x 6x             65775x 75x 75x   65775x 65775x       32099x 32090x   9x       1x           65781x 65243x   538x 538x 533x   5x 1x   5x 4x 4x 4x 4x       1x 1x             65781x 538x 3x   535x 103x 1x   102x     432x       65781x         64992x     789x         75x 9x 66x   66x   66x 66x             13x 13x     13x       60x       75x 75x 13x   62x 13x 49x 2x 47x 5x   42x         2x 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);
      Eif (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);
  Iif (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();
  Iif (trimed.length === 5 && trimed.toLowerCase() === "false") {
    return false;
  } else {
    return true;
  }
}
 
function jsonType(item) {
  try {
    return JSON.parse(item);
  } catch (e) {
    return item;
  }
}