@phema/cql-execution
Version:
An execution framework for the Clinical Quality Language (CQL)
190 lines (155 loc) • 6.44 kB
JavaScript
"use strict";
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var _require = require('../util/util'),
typeIsArray = _require.typeIsArray;
var Code = /*#__PURE__*/function () {
function Code(code, system, version, display) {
_classCallCheck(this, Code);
this.code = code;
this.system = system;
this.version = version;
this.display = display;
}
_createClass(Code, [{
key: "isCode",
get: function get() {
return true;
}
}, {
key: "hasMatch",
value: function hasMatch(code) {
if (typeof code === 'string') {
// the specific behavior for this is not in the specification. Matching codesystem behavior.
return code === this.code;
} else {
return codesInList(toCodeList(code), [this]);
}
}
}]);
return Code;
}();
var Concept = /*#__PURE__*/function () {
function Concept(codes, display) {
_classCallCheck(this, Concept);
this.codes = codes || [];
this.display = display;
}
_createClass(Concept, [{
key: "isConcept",
get: function get() {
return true;
}
}, {
key: "hasMatch",
value: function hasMatch(code) {
return codesInList(toCodeList(code), this.codes);
}
}]);
return Concept;
}();
var ValueSet = /*#__PURE__*/function () {
function ValueSet(oid, version, codes) {
_classCallCheck(this, ValueSet);
this.oid = oid;
this.version = version;
this.codes = codes || [];
}
_createClass(ValueSet, [{
key: "isValueSet",
get: function get() {
return true;
}
}, {
key: "hasMatch",
value: function hasMatch(code) {
var codesList = toCodeList(code); // InValueSet String Overload
if (codesList.length === 1 && typeof codesList[0] === 'string') {
var matchFound = false;
var multipleCodeSystemsExist = false;
var _iterator = _createForOfIteratorHelper(this.codes),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var codeItem = _step.value;
// Confirm all code systems match
if (codeItem.system !== this.codes[0].system) {
multipleCodeSystemsExist = true;
}
if (codeItem.code === codesList[0]) {
matchFound = true;
}
if (multipleCodeSystemsExist && matchFound) {
throw new Error('In (valueset) is ambiguous -- multiple codes with multiple code systems exist in value set.');
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return matchFound;
} else {
return codesInList(codesList, this.codes);
}
}
}]);
return ValueSet;
}();
function toCodeList(c) {
if (c == null) {
return [];
} else if (typeIsArray(c)) {
var list = [];
var _iterator2 = _createForOfIteratorHelper(c),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var c2 = _step2.value;
list = list.concat(toCodeList(c2));
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
return list;
} else if (typeIsArray(c.codes)) {
return c.codes;
} else {
return [c];
}
}
function codesInList(cl1, cl2) {
// test each code in c1 against each code in c2 looking for a match
return cl1.some(function (c1) {
return cl2.some(function (c2) {
// only the left argument (cl1) can contain strings. cl2 will only contain codes.
if (typeof c1 === 'string') {
// for "string in codesystem" this should compare the string to
// the code's "code" field according to the specification.
return c1 === c2.code;
} else {
return codesMatch(c1, c2);
}
});
});
}
function codesMatch(code1, code2) {
return code1.code === code2.code && code1.system === code2.system;
}
var CodeSystem = function CodeSystem(id, version) {
_classCallCheck(this, CodeSystem);
this.id = id;
this.version = version;
};
module.exports = {
Code: Code,
Concept: Concept,
ValueSet: ValueSet,
CodeSystem: CodeSystem
};