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

88.3% Statements 287/325
86.56% Branches 58/67
92.85% Functions 13/14
88.3% Lines 287/325

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
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    3261x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    219x
    219x
    219x

    219x
    219x
    219x
    219x
    219x
    219x
    197x
    197x
    219x
    219x
    219x
    1x
    1x
    286x
    5x
    5x
    286x
    286x
    286x
    286x
    286x
    66x
    284x
    215x
    215x
    215x
    215x
    215x
    215x
    193x
    193x
    215x
    281x
    286x
    286x
    1x
    1x
    521x

    521x
    521x
    521x
    521x
    4x
    4x
    521x
    521x
    4x
    4x
    4x
    521x
    1x
    1x
    6x
    6x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    6x
    1x
    1x
    157x
    135x
    135x
    135x
    135x
    135x
    135x
    135x
    157x
    157x
    1x
    1x
    95x
    95x
    67x
    67x
    12x
    67x
    55x
    55x
    67x
    95x
    28x
    52x
    52x
    28x
    28x
    28x
    28x
    24x
    24x
    28x

    28x
    28x
    95x
    95x
    95x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    200x
    89x

    89x
    89x

    89x
    200x
    1x
    1x
    721x
    3x
    3x
    721x
    721x
    721x
    721x
    64x
    64x
    64x
    64x
    64x
    721x
    721x
    624x
    624x
    597x
    597x
    107x
    107x
    64x
    64x
    107x
    597x
    597x
    597x
    597x
    597x
    597x
    624x
    721x
    721x
    721x
    38x
    38x
    721x
    683x
    683x
    721x
    721x

    721x
    1x
    1x

    1x
    1x
    386x
    309x
    309x
    309x
    153x
    153x
    153x
    153x
    153x
    309x
    309x
    142x
    142x
    142x
    142x
    142x
    309x
    309x
    53x
    53x
    53x
    53x
    53x
    309x
    309x
    15x
    15x
    15x
    15x
    15x
    309x
    309x
    309x
    386x
    1x
    1x
    26x
    26x
    26x
    26x
    26x
    1x
    1x
    108x
    22x
    22x
    1x
    22x
    21x
    21x
    21x
    21x
    21x
    21x
    21x
    21x

    22x
    22x
    108x
    108x
    1x
    1x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    8x
    31x
    31x
    31x
    31x
    31x
    31x

    31x

    31x
    22x
    22x
    31x
    31x
    31x
    31x
    8x
     "use strict";

    var bbxml = require("@amida-tech/blue-button-xml");
    var bbm = require("@amida-tech/blue-button-meta");
    var _ = require("lodash");

    var css = bbm.code_systems;

    var includeCleanup = bbxml.cleanup;
    var processor = bbxml.processor;
    var common = bbxml.common;
    var xmlUtil = bbxml.xmlUtil;

    var cleanup = module.exports = Object.create(includeCleanup);

    var lookupReference = function (node, id) {
      // ensure element itself exists
      var resolved = xmlUtil.xpath(node, "//*[@ID='" + id + "']");
      if (!resolved || resolved.length !== 1) {
        return null;
      }

      // check for contained text
      var textNode = xmlUtil.xpath(resolved[0], "./text()")[0];
      var containedText = textNode && processor.asString(textNode);

      if (containedText) {
        return containedText;
      }

      return null;
    };

    var resolveReference = cleanup.resolveReference = function () {
      if (!common.exists(this.js)) {
        return;
      }

      var ret = null;

      // ignore reference if the element itself contains text
      if (this.js.text) {
        ret = this.js.text.join("").match(/\s*(.*)\s*/)[1];
      } else {
        var r = this.js.reference && this.js.reference.match(/#(.*)/);
        var resolved = null;
        if (r && r.length === 2) {
          resolved = lookupReference(this.node, r[1]);
        }
        if (resolved) {
          ret = resolved;
        }
      }

      this.js = ret || null;
    };

    var resolveReferenceOntoName = cleanup.resolveReferenceOntoName = function () {
      if (!common.exists(this.js)) {
        return;
      }

      var r = this.js.reference && this.js.reference.match(/#(.*)/);
      var resolved = null;
      if (r && r.length === 2) {
        resolved = lookupReference(this.node, r[1]);
      }
      var ret = null;
      if (resolved) {
        this.js.name = resolved;
        delete this.js.reference;
      }
    };

    var augmentAge = cleanup.augmentAge = function () {
      var units = this.js && this.js.units;
      if (units) {
        var cs = css.find("2.16.840.1.113883.11.20.9.21");
        if (cs) {
          var value = cs.codeDisplayName(units);
          if (value) {
            this.js = value;
          }
        }
      }
    };

    cleanup.augmentSimpleCode = function (oid) {
      var f = function () {
        if (this.js) {
          var cs = css.find(oid);
          if (cs) {
            this.js = cs.codeDisplayName(this.js);
          }
        }
      };
      return f;
    };

    var augmentIndividualName = cleanup.augmentIndividualName = function () {
      if (this.js) {
        if (this.js.middle && this.js.middle.length > 0) {
          this.js.first = this.js.middle[0];
          if (this.js.middle.length > 1) {
            this.js.middle.splice(0, 1);
          } else {
            delete this.js.middle;
          }
        }
        if (!this.js.first && !this.js.last && this.js.freetext_name) {
          var names = this.js.freetext_name.split(' ').filter(function (piece) {
            return piece.length > 0;
          });
          var n = names.length;
          if (n > 0) {
            this.js.last = names[n - 1];
            if (n > 1) {
              this.js.first = names[0];
            }
            if (n > 2) {
              this.js.middle = names.slice(1, n - 1);
            }
          }
        }
        delete this.js.freetext_name;
      }
    };

    cleanup.nullFlavorDisplayNames = {
      "NI": "no information",
      "NA": "not applicable",
      "UNK": "unknown",
      "ASKU": "asked, but not known",
      "NAV": "temporarily unavailable",
      "NASK": "not asked",
      "TRC": "trace",
      "OTH": "other",
      "PINF": "positive infinity",
      "NINF": "negative infinity",
      "MSK": "masked"
    };

    // if a field is absent or is an object that looks like a nullFlavor code,
    // replace it with the nullFlavor display name from a sibling nullFlavorField
    cleanup.replaceStringFieldWithNullFlavorName = function (field) {
      return function () {
        if (!this.js) {
          return null;
        }

        if (_.get(this, ["js", field, "code_system_name"]) === "Null Flavor") {
          this.js[field] = cleanup.nullFlavorDisplayNames[this.js[field].code];
        }
      };
    };

    cleanup.augmentConcept = function () {
      if (!this.js) {
        this.js = {};
      }

      var nullFlavorDisplayNames = cleanup.nullFlavorDisplayNames;

      if (common.exists(this.js.nullFlavor)) {
        this.js.code = this.js.nullFlavor;
        this.js.code_system_name = "Null Flavor";
        this.js.name = nullFlavorDisplayNames[this.js.nullFlavor];
        delete this.js.nullFlavor;
      }

      if (this.js.system) {
        var cs = css.find(this.js.system);
        if (cs) {
          // Keep existing name if present
          if (!common.exists(this.js.name)) {
            var newName = cs.codeDisplayName(this.js.code);
            if (newName) {
              this.js.name = newName;
            }
          }
          // but preferentially use our canonical names for the coding system
          var systemName = cs.name();
          if (systemName) {
            this.js.code_system_name = systemName;
          }
        }
      }

      //If original text is present w/out name, use it.
      if (this.js.original_text && (!this.js.name || this.js.code_system_name === 'Null Flavor')) {
        this.js.name = this.js.original_text.js;
        delete this.js.original_text;
      } else {
        delete this.js.original_text;
      }

      if (this.js.nullFlavor) {
        delete this.js.nullFlavor;
      }
    };

    cleanup.selectValueFields = function () {
      var ret = null;
      if (this.js) {

        var type = _.get(this, "js.type");

        if (type === "ST") {
          ret = {
            text: _.get(this, "js.textValue")
          };
        } else if (type === "PQ") {
          ret = _.get(this, "js.physicalQuantityValue.js");
        } else if (type === "CD") {
          ret = _.get(this, "js.codeValue.js");
        }
      }

      this.js = ret;
    };

    cleanup.augmentEffectiveTime = function () {
      if (this.js) {
        var returnArray = {};

        if (this.js.point) {
          returnArray.point = {
            "date": this.js.point,
            "precision": this.js.point_resolution
          };
        }

        if (this.js.low) {
          returnArray.low = {
            "date": this.js.low,
            "precision": this.js.low_resolution
          };
        }

        if (this.js.high) {
          returnArray.high = {
            "date": this.js.high,
            "precision": this.js.high_resolution
          };
        }

        if (this.js.center) {
          returnArray.center = {
            "date": this.js.center,
            "precision": this.js.center_resolution
          };
        }

        this.js = returnArray;
      }
    };

    cleanup.augmentSimplifiedCode = function () {
      if (this.js) {
        // TODO: look up; don't trust the name to be present...
        this.js = this.js.name;
      }
    };

    cleanup.augmentSimplifiedCodeOID = function (oid) {
      var f = function () {
        if (this.js) {
          if (this.js.name) {
            this.js = this.js.name;
          } else if (this.js.code) {
            var cs = css.find(oid);
            if (cs) {
              var name = cs.codeDisplayName(this.js.code);
              if (name) {
                this.js = name;
              }
            }
          } else {
            this.js = null;
          }
        }
      };
      return f;
    };

    cleanup.allergiesProblemStatusToHITSP = (function () {
      var dict = {};
      dict.active = {
        "name": "Active",
        "code": "55561003",
        "code_system_name": "SNOMED CT"
      };
      dict.suspended = dict.aborted = {
        "name": "Inactive",
        "code": "73425007",
        "code_system_name": "SNOMED CT"
      };
      dict.completed = {
        "name": "Resolved",
        "code": "413322009",
        "code_system_name": "SNOMED CT"
      };

      return function () {
        var status = this.js && this.js.problemStatus;
        if (status) {
          var value = dict[status];
          if (value) {
            var observation = this.js.observation;
            if (!observation) {
              this.js.observation = {
                status: value
              };
            } else if (!observation.js) {
              observation.js = {
                status: value
              };
            } else if (!observation.js.status) {
              observation.js.status = value;
            }
          }
          delete this.js.problemStatus;
        }
      };
    })();