@phema/cql-execution
Version:
An execution framework for the Clinical Quality Language (CQL)
481 lines (348 loc) • 15.8 kB
JavaScript
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _construct(Parent, args, Class) { if (_isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
var _require = require('./expression'),
Expression = _require.Expression;
var _require2 = require('./builder'),
build = _require2.build;
var _require3 = require('./literal'),
Literal = _require3.Literal;
var DT = require('../datatypes/datatypes');
var DateTime = /*#__PURE__*/function (_Expression) {
_inherits(DateTime, _Expression);
var _super = _createSuper(DateTime);
function DateTime(json) {
var _this;
_classCallCheck(this, DateTime);
_this = _super.call(this, json);
_this.json = json;
return _this;
}
_createClass(DateTime, [{
key: "exec",
value: function exec(ctx) {
var _this2 = this;
var _iterator = _createForOfIteratorHelper(DateTime.PROPERTIES),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var property = _step.value;
// if json does not contain 'timezoneOffset' set it to the executionDateTime from the context
if (this.json[property] != null) {
this[property] = build(this.json[property]);
} else if (property === 'timezoneOffset' && ctx.getTimezoneOffset() != null) {
this[property] = Literal.from({
type: 'Literal',
value: ctx.getTimezoneOffset(),
valueType: '{urn:hl7-org:elm-types:r1}Integer'
});
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
var args = DateTime.PROPERTIES.map(function (p) {
return _this2[p] != null ? _this2[p].execute(ctx) : undefined;
});
return _construct(DT.DateTime, _toConsumableArray(args));
}
}]);
return DateTime;
}(Expression);
DateTime.PROPERTIES = ['year', 'month', 'day', 'hour', 'minute', 'second', 'millisecond', 'timezoneOffset'];
var _Date = /*#__PURE__*/function (_Expression2) {
_inherits(_Date, _Expression2);
var _super2 = _createSuper(_Date);
function _Date(json) {
var _this3;
_classCallCheck(this, _Date);
_this3 = _super2.call(this, json);
_this3.json = json;
return _this3;
}
_createClass(_Date, [{
key: "exec",
value: function exec(ctx) {
var _this4 = this;
var _iterator2 = _createForOfIteratorHelper(_Date.PROPERTIES),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var property = _step2.value;
if (this.json[property] != null) {
this[property] = build(this.json[property]);
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
var args = _Date.PROPERTIES.map(function (p) {
return _this4[p] != null ? _this4[p].execute(ctx) : undefined;
});
return _construct(DT.Date, _toConsumableArray(args));
}
}]);
return _Date;
}(Expression);
_Date.PROPERTIES = ['year', 'month', 'day'];
var Time = /*#__PURE__*/function (_Expression3) {
_inherits(Time, _Expression3);
var _super3 = _createSuper(Time);
function Time(json) {
var _this5;
_classCallCheck(this, Time);
_this5 = _super3.call(this, json);
var _iterator3 = _createForOfIteratorHelper(Time.PROPERTIES),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var property = _step3.value;
if (json[property] != null) {
_this5[property] = build(json[property]);
}
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
return _this5;
}
_createClass(Time, [{
key: "exec",
value: function exec(ctx) {
var _this6 = this;
var args = Time.PROPERTIES.map(function (p) {
return _this6[p] != null ? _this6[p].execute(ctx) : undefined;
});
return _construct(DT.DateTime, [0, 1, 1].concat(_toConsumableArray(args))).getTime();
}
}]);
return Time;
}(Expression);
Time.PROPERTIES = ['hour', 'minute', 'second', 'millisecond'];
var Today = /*#__PURE__*/function (_Expression4) {
_inherits(Today, _Expression4);
var _super4 = _createSuper(Today);
function Today(json) {
_classCallCheck(this, Today);
return _super4.call(this, json);
}
_createClass(Today, [{
key: "exec",
value: function exec(ctx) {
return ctx.getExecutionDateTime().getDate();
}
}]);
return Today;
}(Expression);
var Now = /*#__PURE__*/function (_Expression5) {
_inherits(Now, _Expression5);
var _super5 = _createSuper(Now);
function Now(json) {
_classCallCheck(this, Now);
return _super5.call(this, json);
}
_createClass(Now, [{
key: "exec",
value: function exec(ctx) {
return ctx.getExecutionDateTime();
}
}]);
return Now;
}(Expression);
var TimeOfDay = /*#__PURE__*/function (_Expression6) {
_inherits(TimeOfDay, _Expression6);
var _super6 = _createSuper(TimeOfDay);
function TimeOfDay(json) {
_classCallCheck(this, TimeOfDay);
return _super6.call(this, json);
}
_createClass(TimeOfDay, [{
key: "exec",
value: function exec(ctx) {
return ctx.getExecutionDateTime().getTime();
}
}]);
return TimeOfDay;
}(Expression);
var DateTimeComponentFrom = /*#__PURE__*/function (_Expression7) {
_inherits(DateTimeComponentFrom, _Expression7);
var _super7 = _createSuper(DateTimeComponentFrom);
function DateTimeComponentFrom(json) {
var _this7;
_classCallCheck(this, DateTimeComponentFrom);
_this7 = _super7.call(this, json);
_this7.precision = json.precision;
return _this7;
}
_createClass(DateTimeComponentFrom, [{
key: "exec",
value: function exec(ctx) {
var arg = this.execArgs(ctx);
if (arg != null) {
return arg[this.precision.toLowerCase()];
} else {
return null;
}
}
}]);
return DateTimeComponentFrom;
}(Expression);
var DateFrom = /*#__PURE__*/function (_Expression8) {
_inherits(DateFrom, _Expression8);
var _super8 = _createSuper(DateFrom);
function DateFrom(json) {
_classCallCheck(this, DateFrom);
return _super8.call(this, json);
}
_createClass(DateFrom, [{
key: "exec",
value: function exec(ctx) {
var date = this.execArgs(ctx);
if (date != null) {
return date.getDate();
} else {
return null;
}
}
}]);
return DateFrom;
}(Expression);
var TimeFrom = /*#__PURE__*/function (_Expression9) {
_inherits(TimeFrom, _Expression9);
var _super9 = _createSuper(TimeFrom);
function TimeFrom(json) {
_classCallCheck(this, TimeFrom);
return _super9.call(this, json);
}
_createClass(TimeFrom, [{
key: "exec",
value: function exec(ctx) {
var date = this.execArgs(ctx);
if (date != null) {
return date.getTime();
} else {
return null;
}
}
}]);
return TimeFrom;
}(Expression);
var TimezoneOffsetFrom = /*#__PURE__*/function (_Expression10) {
_inherits(TimezoneOffsetFrom, _Expression10);
var _super10 = _createSuper(TimezoneOffsetFrom);
function TimezoneOffsetFrom(json) {
_classCallCheck(this, TimezoneOffsetFrom);
return _super10.call(this, json);
}
_createClass(TimezoneOffsetFrom, [{
key: "exec",
value: function exec(ctx) {
var date = this.execArgs(ctx);
if (date != null) {
return date.timezoneOffset;
} else {
return null;
}
}
}]);
return TimezoneOffsetFrom;
}(Expression); // Delegated to by overloaded#After
function doAfter(a, b, precision) {
return a.after(b, precision);
} // Delegated to by overloaded#Before
function doBefore(a, b, precision) {
return a.before(b, precision);
}
var DifferenceBetween = /*#__PURE__*/function (_Expression11) {
_inherits(DifferenceBetween, _Expression11);
var _super11 = _createSuper(DifferenceBetween);
function DifferenceBetween(json) {
var _this8;
_classCallCheck(this, DifferenceBetween);
_this8 = _super11.call(this, json);
_this8.precision = json.precision;
return _this8;
}
_createClass(DifferenceBetween, [{
key: "exec",
value: function exec(ctx) {
var args = this.execArgs(ctx); // Check to make sure args exist and that they have differenceBetween functions so that they can be compared to one another
if (args[0] == null || args[1] == null || typeof args[0].differenceBetween !== 'function' || typeof args[1].differenceBetween !== 'function') {
return null;
}
var result = args[0].differenceBetween(args[1], this.precision != null ? this.precision.toLowerCase() : undefined);
if (result != null && result.isPoint()) {
return result.low;
} else {
return result;
}
}
}]);
return DifferenceBetween;
}(Expression);
var DurationBetween = /*#__PURE__*/function (_Expression12) {
_inherits(DurationBetween, _Expression12);
var _super12 = _createSuper(DurationBetween);
function DurationBetween(json) {
var _this9;
_classCallCheck(this, DurationBetween);
_this9 = _super12.call(this, json);
_this9.precision = json.precision;
return _this9;
}
_createClass(DurationBetween, [{
key: "exec",
value: function exec(ctx) {
var args = this.execArgs(ctx); // Check to make sure args exist and that they have durationBetween functions so that they can be compared to one another
if (args[0] == null || args[1] == null || typeof args[0].durationBetween !== 'function' || typeof args[1].durationBetween !== 'function') {
return null;
}
var result = args[0].durationBetween(args[1], this.precision != null ? this.precision.toLowerCase() : undefined);
if (result != null && result.isPoint()) {
return result.low;
} else {
return result;
}
}
}]);
return DurationBetween;
}(Expression);
module.exports = {
Date: _Date,
DateFrom: DateFrom,
DateTime: DateTime,
DateTimeComponentFrom: DateTimeComponentFrom,
DifferenceBetween: DifferenceBetween,
DurationBetween: DurationBetween,
Now: Now,
Time: Time,
TimeFrom: TimeFrom,
TimeOfDay: TimeOfDay,
TimezoneOffsetFrom: TimezoneOffsetFrom,
Today: Today,
doAfter: doAfter,
doBefore: doBefore
};