# All files / csv2json/libs/core parser.js

0% Statements 0/72
0% Branches 0/49
0% Functions 0/10
0% Lines 0/72

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

     var explicitTypes = ["number", "string"];

    function Parser(name, regExp, parser, processSafe) {
      this.name = typeof name === "undefined" ? "Default" : name;
      this.regExp = null;
      this.type = "";
      this.processSafe = processSafe;
      if (typeof regExp !== "undefined") {
        if (typeof regExp === "string") {
          this.regExp = new RegExp(regExp);
        } else {
          this.regExp = regExp;
        }
      }
      if (typeof parser !== "undefined") {
        this.parse = parser;
      }
    }
    // var numReg = /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;
    Parser.prototype.convertType = function(item) {
      var type=this.type;
      if (type === 'number') {
        var rtn = parseFloat(item);
        if (isNaN(rtn)) {
          return 0;
        } else {
          return rtn;
        }
      } else if (this.param && this.param.checkType && type === '') {
        var trimed = item.trim();
        if (trimed === ""){
          return trimed;
        }
        if (!isNaN(trimed)) {
          return parseFloat(trimed);
        } else if (trimed.length === 5 && trimed.toLowerCase() === "false") {
          return false;
        } else if (trimed.length === 4 && trimed.toLowerCase() === "true") {
          return true;
        } else if (trimed[0] === "{" && trimed[trimed.length - 1] === "}" || trimed[0] === "[" && trimed[trimed.length - 1]==="]") {
          try {
            return JSON.parse(trimed);
          } catch (e) {
            return item;
          }
        } else {
          return item;
        }
      }
      return item;
    };

    Parser.prototype.setParam = function(param) {
      this.param = param;
    };

    Parser.prototype.test = function(str) {
      return this.regExp && this.regExp.test(str);
    };

    Parser.prototype.parse = function(params) {
      params.resultRow[params.head] = params.item;
    };

    Parser.prototype.getHeadStr = function() {
      if (this.headStr) {
        return this.headStr;
      } else {
        var head = this.head;
        this.headStr = head.replace(this.regExp, '');
        if (!this.headStr) {
          this.headStr = "Unknown Header";
        }
        return this.getHeadStr();
      }
    };

    Parser.prototype.getHead = function() {
      return this.head;
    };

    Parser.prototype.initHead = function(columnTitle) {
      this.head = columnTitle;
      var wholeHead = columnTitle.replace(this.regExp, '');
      //init type && headStr
      var splitArr = wholeHead.split("#!");
      if (splitArr.length === 1) { //no explicit type
        this.headStr = splitArr[0];
      } else {
        var type = splitArr.shift();
        if (explicitTypes.indexOf(type.toLowerCase()) > -1) {
          this.type = type;
          this.headStr = splitArr.join("#!");
        } else { //no explicit type
          this.headStr = wholeHead;
        }
      }
      if (!this.headStr) {
        this.headStr = wholeHead ? wholeHead : "Unknown Head";
      }
    };

    Parser.prototype.clone = function() {
      var obj = Object.create(this);
      var newParser = new Parser();
      for (var key in obj) {
        newParser[key] = obj[key];
      }
      return newParser;
    };

    Parser.prototype.getName = function() {
      return this.name;
    };

    module.exports = Parser;