-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcql-patient.js
141 lines (125 loc) · 3.65 KB
/
cql-patient.js
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
const DT = require('./datatypes/datatypes');
class Record {
constructor(json) {
this.json = json;
this.id = this.json.id;
}
_is(typeSpecifier) {
return this._typeHierarchy().some(
t => t.type === typeSpecifier.type && t.name == typeSpecifier.name
);
}
_typeHierarchy() {
return [
{
name: `{https://github.com/cqframework/cql-execution/simple}${this.json.recordType}`,
type: 'NamedTypeSpecifier'
},
{
name: '{https://github.com/cqframework/cql-execution/simple}Record',
type: 'NamedTypeSpecifier'
},
{ name: '{urn:hl7-org:elm-types:r1}Any', type: 'NamedTypeSpecifier' }
];
}
_recursiveGet(field) {
if (field != null && field.indexOf('.') >= 0) {
const [root, rest] = field.split('.', 2);
return new Record(this._recursiveGet(root))._recursiveGet(rest);
}
return this.json[field];
}
get(field) {
// the model should return the correct type for the field. For this simple model example,
// we just cheat and use the shape of the value to determine it. Real implementations should
// have a more sophisticated approach
const value = this._recursiveGet(field);
if (typeof value === 'string' && /\d{4}-\d{2}-\d{2}(T[\d\-.]+)?/.test(value)) {
return this.getDate(field);
}
if (value != null && typeof value === 'object' && value.code != null && value.system != null) {
return this.getCode(field);
}
if (value != null && typeof value === 'object' && (value.low != null || value.high != null)) {
return this.getInterval(field);
}
return value;
}
getId() {
return this.id;
}
getDate(field) {
const val = this._recursiveGet(field);
if (val != null) {
return DT.DateTime.parse(val);
} else {
return null;
}
}
getInterval(field) {
const val = this._recursiveGet(field);
if (val != null && typeof val === 'object') {
const low = val.low != null ? DT.DateTime.parse(val.low) : null;
const high = val.high != null ? DT.DateTime.parse(val.high) : null;
return new DT.Interval(low, high);
}
}
getDateOrInterval(field) {
const val = this._recursiveGet(field);
if (val != null && typeof val === 'object') {
return this.getInterval(field);
} else {
return this.getDate(field);
}
}
getCode(field) {
const val = this._recursiveGet(field);
if (val != null && typeof val === 'object') {
return new DT.Code(val.code, val.system, val.version);
}
}
}
class Patient extends Record {
constructor(json) {
super(json);
this.name = json.name;
this.gender = json.gender;
this.birthDate = json.birthDate != null ? DT.DateTime.parse(json.birthDate) : undefined;
this.records = {};
(json.records || []).forEach(r => {
if (this.records[r.recordType] == null) {
this.records[r.recordType] = [];
}
this.records[r.recordType].push(new Record(r));
});
}
findRecords(profile) {
if (profile == null) {
return [];
}
const recordType = profile.match(
/(\{https:\/\/github\.com\/cqframework\/cql-execution\/simple\})?(.*)/
)[2];
if (recordType === 'Patient') {
return [this];
} else {
return this.records[recordType] || [];
}
}
}
class PatientSource {
constructor(patients) {
this.patients = patients;
this.nextPatient();
}
currentPatient() {
return this.current;
}
nextPatient() {
const currentJSON = this.patients.shift();
this.current = currentJSON ? new Patient(currentJSON) : undefined;
return this.current;
}
}
module.exports.Patient = Patient;
module.exports.PatientSource = PatientSource;