# All files / blue-button/lib parser.js

79.38% Statements 104/131
45% Branches 9/20
100% Functions 5/5
79.38% Lines 104/131

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

    11x
    11x
    10x
    10x
    10x
    11x
    11x
    11x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    10x
    10x
    10x

    10x
    10x

    10x
    10x
    2x
    2x
    10x
    10x
    10x
    10x

    10x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    1x
    2x
    2x
    2x

    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    1x
    1x
    9x
    9x

    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    8x

    8x
    9x

    1x
    1x
    1x
    9x

    9x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
     "use strict";

    var parseCMS = require("@amida-tech/blue-button-cms");
    var parseNCPDP;
    var _ = require('lodash');

    try {
      parseNCPDP = require("@amida-tech/blue-button-ncpdp").parseXml;
    } catch (ex) {
      parseNCPDP = null;
    }

    var componentRouter = require("./parser/router").componentRouter;
    var xmlParser = require("@amida-tech/blue-button-xml").xmlUtil;
    var util = require("util");
    var sense = require("./sense.js");

    //add model/package version to metadata
    var version = require("../package.json").version;

    //insert sections list into metadata
    function sections(data) {
      if (!data.meta) {
        data.meta = {};
      }

      if (data.data.meta) {
        _.extend(data.meta, data.data.meta);
        delete data.data.meta;
      }
      data.meta.sections = Object.keys(data.data);
      return data;
    }

    function parseText(txt) {
      //txt must be a string
      if (!txt || typeof (txt) !== "string") {
        //TODO: throw a proper error here
        return null;
      }

      return sections(parseCMS.parseText(txt));
    }

    function parseXml(doc, options, sensed) {
      //data must be an object
      if (!doc || typeof (doc) !== "object") {
        //TODO: throw a proper error here
        return null;
      }

      if (arguments.length === 1) {
        options = {};
      }

      if (!sensed) {
        sensed = sense.senseXml(doc);
      }

      var componentParser = componentRouter(options.component, sensed);

      if (!componentParser) {
        var msg = util.format("Component %s is not supported.", options.component);
        //callback(new Error(msg)); //TODO:revise this use of callbacks
        return {
          "errors": new Error(msg)
        };
      }
      var ret = componentParser.instance();

      ret.run(doc, options.sourceKey);
      ret.cleanupTree(options.sourceKey); // first build the data objects up
      return sections({
        "data": ret.toJSON(),
        "meta": {
          "version": version
        },
        "errors": ret.errors
      });
    }

    function parseString(data, options) {
      //data must be a string
      if (!data || typeof (data) !== "string") {
        //TODO: throw a proper error here
        return null;
      }

      if (arguments.length === 1) {
        options = {};
      }

      var doc = xmlParser.parse(data);
      return parseXml(doc, options);
    }

    var parse = function (data, options) {
      //data must be a string
      if (!data || typeof (data) !== "string") {
        //TODO: throw a proper error here
        return null;
      }

      if (arguments.length === 1) {
        options = {};
      }

      var sensed = sense.senseString(data);

      if (sensed) {
        if (sensed.xml) {
          if (sensed.type === 'ncpdp' && parseNCPDP) {
            return parseNCPDP(sensed.xml);
          }
          return parseXml(sensed.xml, options, sensed);
        } else if (sensed.json) {
          return sensed.json;
        } else {
          return parseText(data);
        }
      } else {
        return null;
      }
    };

    module.exports = {
      parse: parse,
      parseXml: parseXml,
      parseString: parseString,
      parseText: parseText
    };