@fhir-dsl/terminology 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.cjs +307 -0
- package/dist/index.d.cts +42 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +275 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Abdelhadi Sabani
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
TerminologyRegistry: () => TerminologyRegistry,
|
|
24
|
+
parseCodeSystem: () => parseCodeSystem,
|
|
25
|
+
parseExpansion: () => parseExpansion,
|
|
26
|
+
parseExpansionsBundle: () => parseExpansionsBundle,
|
|
27
|
+
resolveCompose: () => resolveCompose,
|
|
28
|
+
resolveValueSet: () => resolveValueSet
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(index_exports);
|
|
31
|
+
|
|
32
|
+
// src/codesystem-parser.ts
|
|
33
|
+
function isCodeSystem(value) {
|
|
34
|
+
if (typeof value !== "object" || value === null) return false;
|
|
35
|
+
const record = value;
|
|
36
|
+
return record.resourceType === "CodeSystem";
|
|
37
|
+
}
|
|
38
|
+
function flattenConcepts(concepts) {
|
|
39
|
+
const result = [];
|
|
40
|
+
for (const concept of concepts) {
|
|
41
|
+
if (concept.code) {
|
|
42
|
+
result.push({
|
|
43
|
+
code: concept.code,
|
|
44
|
+
display: concept.display
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
if (concept.concept?.length) {
|
|
48
|
+
result.push(...flattenConcepts(concept.concept));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
function parseCodeSystem(resource) {
|
|
54
|
+
if (!isCodeSystem(resource)) return void 0;
|
|
55
|
+
const content = normalizeContent(resource.content);
|
|
56
|
+
return {
|
|
57
|
+
url: resource.url ?? "",
|
|
58
|
+
name: resource.name ?? "",
|
|
59
|
+
content,
|
|
60
|
+
concepts: content === "complete" && resource.concept?.length ? flattenConcepts(resource.concept) : []
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function normalizeContent(value) {
|
|
64
|
+
switch (value) {
|
|
65
|
+
case "complete":
|
|
66
|
+
case "not-present":
|
|
67
|
+
case "example":
|
|
68
|
+
case "fragment":
|
|
69
|
+
case "supplement":
|
|
70
|
+
return value;
|
|
71
|
+
default:
|
|
72
|
+
return "not-present";
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/expansion-parser.ts
|
|
77
|
+
function isExpandedValueSet(value) {
|
|
78
|
+
if (typeof value !== "object" || value === null) return false;
|
|
79
|
+
const record = value;
|
|
80
|
+
return record.resourceType === "ValueSet" && typeof record.expansion === "object" && record.expansion !== null;
|
|
81
|
+
}
|
|
82
|
+
function flattenContains(contains) {
|
|
83
|
+
const codes = [];
|
|
84
|
+
for (const entry of contains) {
|
|
85
|
+
if (entry.code && !entry.abstract) {
|
|
86
|
+
codes.push({
|
|
87
|
+
code: entry.code,
|
|
88
|
+
display: entry.display,
|
|
89
|
+
system: entry.system
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
if (entry.contains?.length) {
|
|
93
|
+
codes.push(...flattenContains(entry.contains));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return codes;
|
|
97
|
+
}
|
|
98
|
+
function parseExpansion(resource) {
|
|
99
|
+
if (!isExpandedValueSet(resource)) return void 0;
|
|
100
|
+
if (!resource.expansion?.contains?.length) return void 0;
|
|
101
|
+
return {
|
|
102
|
+
url: resource.url ?? "",
|
|
103
|
+
name: resource.name ?? "",
|
|
104
|
+
version: resource.version,
|
|
105
|
+
codes: flattenContains(resource.expansion.contains),
|
|
106
|
+
isComplete: true
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function parseExpansionsBundle(bundle) {
|
|
110
|
+
const result = /* @__PURE__ */ new Map();
|
|
111
|
+
const entries = extractBundleEntries(bundle);
|
|
112
|
+
for (const entry of entries) {
|
|
113
|
+
const resolved = parseExpansion(entry);
|
|
114
|
+
if (resolved?.url && resolved.codes.length > 0) {
|
|
115
|
+
result.set(resolved.url, resolved);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
function extractBundleEntries(bundle) {
|
|
121
|
+
if (typeof bundle !== "object" || bundle === null) return [];
|
|
122
|
+
const record = bundle;
|
|
123
|
+
if (record.resourceType === "Bundle" && Array.isArray(record.entry)) {
|
|
124
|
+
return record.entry.filter((e) => typeof e === "object" && e !== null && "resource" in e).map((e) => e.resource);
|
|
125
|
+
}
|
|
126
|
+
return [];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// src/valueset-parser.ts
|
|
130
|
+
function isComposeValueSet(value) {
|
|
131
|
+
if (typeof value !== "object" || value === null) return false;
|
|
132
|
+
const record = value;
|
|
133
|
+
return record.resourceType === "ValueSet" && typeof record.compose === "object" && record.compose !== null;
|
|
134
|
+
}
|
|
135
|
+
function resolveCompose(resource, codeSystemLookup) {
|
|
136
|
+
if (!isComposeValueSet(resource)) return void 0;
|
|
137
|
+
if (!resource.compose?.include?.length) return void 0;
|
|
138
|
+
const includedCodes = [];
|
|
139
|
+
let isComplete = true;
|
|
140
|
+
for (const include of resource.compose.include) {
|
|
141
|
+
if (include.concept?.length) {
|
|
142
|
+
for (const concept of include.concept) {
|
|
143
|
+
if (concept.code) {
|
|
144
|
+
includedCodes.push({
|
|
145
|
+
code: concept.code,
|
|
146
|
+
display: concept.display,
|
|
147
|
+
system: include.system
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
} else if (include.filter?.length) {
|
|
152
|
+
const cs = include.system ? codeSystemLookup.get(include.system) : void 0;
|
|
153
|
+
if (cs && cs.content === "complete" && cs.concepts.length > 0) {
|
|
154
|
+
const filtered = applyFilters(cs.concepts, include.filter);
|
|
155
|
+
includedCodes.push(...filtered.map((c) => ({ ...c, system: include.system })));
|
|
156
|
+
} else {
|
|
157
|
+
isComplete = false;
|
|
158
|
+
}
|
|
159
|
+
} else if (include.system) {
|
|
160
|
+
const cs = codeSystemLookup.get(include.system);
|
|
161
|
+
if (cs && cs.content === "complete" && cs.concepts.length > 0) {
|
|
162
|
+
includedCodes.push(...cs.concepts.map((c) => ({ ...c, system: include.system })));
|
|
163
|
+
} else {
|
|
164
|
+
isComplete = false;
|
|
165
|
+
}
|
|
166
|
+
} else {
|
|
167
|
+
isComplete = false;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (resource.compose.exclude?.length) {
|
|
171
|
+
const excludeSet = /* @__PURE__ */ new Set();
|
|
172
|
+
for (const exclude of resource.compose.exclude) {
|
|
173
|
+
if (exclude.concept?.length) {
|
|
174
|
+
for (const concept of exclude.concept) {
|
|
175
|
+
if (concept.code) {
|
|
176
|
+
const key = exclude.system ? `${exclude.system}|${concept.code}` : concept.code;
|
|
177
|
+
excludeSet.add(key);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (excludeSet.size > 0) {
|
|
183
|
+
const filtered = includedCodes.filter((c) => {
|
|
184
|
+
const key = c.system ? `${c.system}|${c.code}` : c.code;
|
|
185
|
+
return !excludeSet.has(key);
|
|
186
|
+
});
|
|
187
|
+
return {
|
|
188
|
+
url: resource.url ?? "",
|
|
189
|
+
name: resource.name ?? "",
|
|
190
|
+
version: resource.version,
|
|
191
|
+
codes: filtered,
|
|
192
|
+
isComplete
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (includedCodes.length === 0) return void 0;
|
|
197
|
+
return {
|
|
198
|
+
url: resource.url ?? "",
|
|
199
|
+
name: resource.name ?? "",
|
|
200
|
+
version: resource.version,
|
|
201
|
+
codes: includedCodes,
|
|
202
|
+
isComplete
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
function applyFilters(concepts, filters) {
|
|
206
|
+
let result = concepts;
|
|
207
|
+
for (const filter of filters) {
|
|
208
|
+
if (filter.op === "=" && filter.property === "concept" && filter.value) {
|
|
209
|
+
result = result.filter((c) => c.code === filter.value);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return result;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// src/resolver.ts
|
|
216
|
+
function resolveValueSet(valueSetResource, expansions, codeSystems) {
|
|
217
|
+
const url = extractUrl(valueSetResource);
|
|
218
|
+
if (!url) return void 0;
|
|
219
|
+
const expanded = findExpansion(url, expansions);
|
|
220
|
+
if (expanded) return expanded;
|
|
221
|
+
return resolveCompose(valueSetResource, codeSystems);
|
|
222
|
+
}
|
|
223
|
+
function extractUrl(resource) {
|
|
224
|
+
if (typeof resource !== "object" || resource === null) return void 0;
|
|
225
|
+
const record = resource;
|
|
226
|
+
return typeof record.url === "string" ? record.url : void 0;
|
|
227
|
+
}
|
|
228
|
+
function findExpansion(url, expansions) {
|
|
229
|
+
const exact = expansions.get(url);
|
|
230
|
+
if (exact) return exact;
|
|
231
|
+
const pipeIdx = url.indexOf("|");
|
|
232
|
+
if (pipeIdx > 0) {
|
|
233
|
+
return expansions.get(url.slice(0, pipeIdx));
|
|
234
|
+
}
|
|
235
|
+
return void 0;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/registry.ts
|
|
239
|
+
var TerminologyRegistry = class {
|
|
240
|
+
#codeSystems = /* @__PURE__ */ new Map();
|
|
241
|
+
#expansions = /* @__PURE__ */ new Map();
|
|
242
|
+
#valueSets = /* @__PURE__ */ new Map();
|
|
243
|
+
// raw ValueSet resources
|
|
244
|
+
#resolved = /* @__PURE__ */ new Map();
|
|
245
|
+
// URL → resolved
|
|
246
|
+
loadExpansions(entries) {
|
|
247
|
+
for (const entry of entries) {
|
|
248
|
+
const resolved = parseExpansion(entry);
|
|
249
|
+
if (resolved?.url && resolved.codes.length > 0) {
|
|
250
|
+
this.#expansions.set(resolved.url, resolved);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
loadCodeSystem(resource) {
|
|
255
|
+
const cs = parseCodeSystem(resource);
|
|
256
|
+
if (cs?.url) {
|
|
257
|
+
this.#codeSystems.set(cs.url, cs);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
loadValueSet(resource) {
|
|
261
|
+
if (typeof resource !== "object" || resource === null) return;
|
|
262
|
+
const record = resource;
|
|
263
|
+
if (typeof record.url === "string") {
|
|
264
|
+
this.#valueSets.set(record.url, resource);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
resolveAll() {
|
|
268
|
+
for (const [url, resource] of this.#valueSets) {
|
|
269
|
+
const resolved = resolveValueSet(resource, this.#expansions, this.#codeSystems);
|
|
270
|
+
if (resolved && resolved.codes.length > 0) {
|
|
271
|
+
this.#resolved.set(url, resolved);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
for (const [url, expansion] of this.#expansions) {
|
|
275
|
+
if (!this.#resolved.has(url)) {
|
|
276
|
+
this.#resolved.set(url, expansion);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
resolve(url) {
|
|
281
|
+
const exact = this.#resolved.get(url);
|
|
282
|
+
if (exact) return exact;
|
|
283
|
+
const pipeIdx = url.indexOf("|");
|
|
284
|
+
if (pipeIdx > 0) {
|
|
285
|
+
return this.#resolved.get(url.slice(0, pipeIdx));
|
|
286
|
+
}
|
|
287
|
+
return void 0;
|
|
288
|
+
}
|
|
289
|
+
get resolvedCount() {
|
|
290
|
+
return this.#resolved.size;
|
|
291
|
+
}
|
|
292
|
+
get codeSystemCount() {
|
|
293
|
+
return this.#codeSystems.size;
|
|
294
|
+
}
|
|
295
|
+
allResolved() {
|
|
296
|
+
return this.#resolved.values();
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
300
|
+
0 && (module.exports = {
|
|
301
|
+
TerminologyRegistry,
|
|
302
|
+
parseCodeSystem,
|
|
303
|
+
parseExpansion,
|
|
304
|
+
parseExpansionsBundle,
|
|
305
|
+
resolveCompose,
|
|
306
|
+
resolveValueSet
|
|
307
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
interface ResolvedCode {
|
|
2
|
+
code: string;
|
|
3
|
+
display?: string | undefined;
|
|
4
|
+
system?: string | undefined;
|
|
5
|
+
}
|
|
6
|
+
interface ResolvedValueSet {
|
|
7
|
+
url: string;
|
|
8
|
+
name: string;
|
|
9
|
+
version?: string | undefined;
|
|
10
|
+
codes: ResolvedCode[];
|
|
11
|
+
/** true if all includes were resolved offline; false if some were skipped */
|
|
12
|
+
isComplete: boolean;
|
|
13
|
+
}
|
|
14
|
+
interface CodeSystemModel {
|
|
15
|
+
url: string;
|
|
16
|
+
name: string;
|
|
17
|
+
content: "complete" | "not-present" | "example" | "fragment" | "supplement";
|
|
18
|
+
concepts: ResolvedCode[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare function parseCodeSystem(resource: unknown): CodeSystemModel | undefined;
|
|
22
|
+
|
|
23
|
+
declare function parseExpansion(resource: unknown): ResolvedValueSet | undefined;
|
|
24
|
+
declare function parseExpansionsBundle(bundle: unknown): Map<string, ResolvedValueSet>;
|
|
25
|
+
|
|
26
|
+
declare class TerminologyRegistry {
|
|
27
|
+
#private;
|
|
28
|
+
loadExpansions(entries: unknown[]): void;
|
|
29
|
+
loadCodeSystem(resource: unknown): void;
|
|
30
|
+
loadValueSet(resource: unknown): void;
|
|
31
|
+
resolveAll(): void;
|
|
32
|
+
resolve(url: string): ResolvedValueSet | undefined;
|
|
33
|
+
get resolvedCount(): number;
|
|
34
|
+
get codeSystemCount(): number;
|
|
35
|
+
allResolved(): IterableIterator<ResolvedValueSet>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
declare function resolveValueSet(valueSetResource: unknown, expansions: Map<string, ResolvedValueSet>, codeSystems: Map<string, CodeSystemModel>): ResolvedValueSet | undefined;
|
|
39
|
+
|
|
40
|
+
declare function resolveCompose(resource: unknown, codeSystemLookup: Map<string, CodeSystemModel>): ResolvedValueSet | undefined;
|
|
41
|
+
|
|
42
|
+
export { type CodeSystemModel, type ResolvedCode, type ResolvedValueSet, TerminologyRegistry, parseCodeSystem, parseExpansion, parseExpansionsBundle, resolveCompose, resolveValueSet };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
interface ResolvedCode {
|
|
2
|
+
code: string;
|
|
3
|
+
display?: string | undefined;
|
|
4
|
+
system?: string | undefined;
|
|
5
|
+
}
|
|
6
|
+
interface ResolvedValueSet {
|
|
7
|
+
url: string;
|
|
8
|
+
name: string;
|
|
9
|
+
version?: string | undefined;
|
|
10
|
+
codes: ResolvedCode[];
|
|
11
|
+
/** true if all includes were resolved offline; false if some were skipped */
|
|
12
|
+
isComplete: boolean;
|
|
13
|
+
}
|
|
14
|
+
interface CodeSystemModel {
|
|
15
|
+
url: string;
|
|
16
|
+
name: string;
|
|
17
|
+
content: "complete" | "not-present" | "example" | "fragment" | "supplement";
|
|
18
|
+
concepts: ResolvedCode[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
declare function parseCodeSystem(resource: unknown): CodeSystemModel | undefined;
|
|
22
|
+
|
|
23
|
+
declare function parseExpansion(resource: unknown): ResolvedValueSet | undefined;
|
|
24
|
+
declare function parseExpansionsBundle(bundle: unknown): Map<string, ResolvedValueSet>;
|
|
25
|
+
|
|
26
|
+
declare class TerminologyRegistry {
|
|
27
|
+
#private;
|
|
28
|
+
loadExpansions(entries: unknown[]): void;
|
|
29
|
+
loadCodeSystem(resource: unknown): void;
|
|
30
|
+
loadValueSet(resource: unknown): void;
|
|
31
|
+
resolveAll(): void;
|
|
32
|
+
resolve(url: string): ResolvedValueSet | undefined;
|
|
33
|
+
get resolvedCount(): number;
|
|
34
|
+
get codeSystemCount(): number;
|
|
35
|
+
allResolved(): IterableIterator<ResolvedValueSet>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
declare function resolveValueSet(valueSetResource: unknown, expansions: Map<string, ResolvedValueSet>, codeSystems: Map<string, CodeSystemModel>): ResolvedValueSet | undefined;
|
|
39
|
+
|
|
40
|
+
declare function resolveCompose(resource: unknown, codeSystemLookup: Map<string, CodeSystemModel>): ResolvedValueSet | undefined;
|
|
41
|
+
|
|
42
|
+
export { type CodeSystemModel, type ResolvedCode, type ResolvedValueSet, TerminologyRegistry, parseCodeSystem, parseExpansion, parseExpansionsBundle, resolveCompose, resolveValueSet };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
// src/codesystem-parser.ts
|
|
2
|
+
function isCodeSystem(value) {
|
|
3
|
+
if (typeof value !== "object" || value === null) return false;
|
|
4
|
+
const record = value;
|
|
5
|
+
return record.resourceType === "CodeSystem";
|
|
6
|
+
}
|
|
7
|
+
function flattenConcepts(concepts) {
|
|
8
|
+
const result = [];
|
|
9
|
+
for (const concept of concepts) {
|
|
10
|
+
if (concept.code) {
|
|
11
|
+
result.push({
|
|
12
|
+
code: concept.code,
|
|
13
|
+
display: concept.display
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
if (concept.concept?.length) {
|
|
17
|
+
result.push(...flattenConcepts(concept.concept));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
function parseCodeSystem(resource) {
|
|
23
|
+
if (!isCodeSystem(resource)) return void 0;
|
|
24
|
+
const content = normalizeContent(resource.content);
|
|
25
|
+
return {
|
|
26
|
+
url: resource.url ?? "",
|
|
27
|
+
name: resource.name ?? "",
|
|
28
|
+
content,
|
|
29
|
+
concepts: content === "complete" && resource.concept?.length ? flattenConcepts(resource.concept) : []
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function normalizeContent(value) {
|
|
33
|
+
switch (value) {
|
|
34
|
+
case "complete":
|
|
35
|
+
case "not-present":
|
|
36
|
+
case "example":
|
|
37
|
+
case "fragment":
|
|
38
|
+
case "supplement":
|
|
39
|
+
return value;
|
|
40
|
+
default:
|
|
41
|
+
return "not-present";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/expansion-parser.ts
|
|
46
|
+
function isExpandedValueSet(value) {
|
|
47
|
+
if (typeof value !== "object" || value === null) return false;
|
|
48
|
+
const record = value;
|
|
49
|
+
return record.resourceType === "ValueSet" && typeof record.expansion === "object" && record.expansion !== null;
|
|
50
|
+
}
|
|
51
|
+
function flattenContains(contains) {
|
|
52
|
+
const codes = [];
|
|
53
|
+
for (const entry of contains) {
|
|
54
|
+
if (entry.code && !entry.abstract) {
|
|
55
|
+
codes.push({
|
|
56
|
+
code: entry.code,
|
|
57
|
+
display: entry.display,
|
|
58
|
+
system: entry.system
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
if (entry.contains?.length) {
|
|
62
|
+
codes.push(...flattenContains(entry.contains));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return codes;
|
|
66
|
+
}
|
|
67
|
+
function parseExpansion(resource) {
|
|
68
|
+
if (!isExpandedValueSet(resource)) return void 0;
|
|
69
|
+
if (!resource.expansion?.contains?.length) return void 0;
|
|
70
|
+
return {
|
|
71
|
+
url: resource.url ?? "",
|
|
72
|
+
name: resource.name ?? "",
|
|
73
|
+
version: resource.version,
|
|
74
|
+
codes: flattenContains(resource.expansion.contains),
|
|
75
|
+
isComplete: true
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function parseExpansionsBundle(bundle) {
|
|
79
|
+
const result = /* @__PURE__ */ new Map();
|
|
80
|
+
const entries = extractBundleEntries(bundle);
|
|
81
|
+
for (const entry of entries) {
|
|
82
|
+
const resolved = parseExpansion(entry);
|
|
83
|
+
if (resolved?.url && resolved.codes.length > 0) {
|
|
84
|
+
result.set(resolved.url, resolved);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
function extractBundleEntries(bundle) {
|
|
90
|
+
if (typeof bundle !== "object" || bundle === null) return [];
|
|
91
|
+
const record = bundle;
|
|
92
|
+
if (record.resourceType === "Bundle" && Array.isArray(record.entry)) {
|
|
93
|
+
return record.entry.filter((e) => typeof e === "object" && e !== null && "resource" in e).map((e) => e.resource);
|
|
94
|
+
}
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/valueset-parser.ts
|
|
99
|
+
function isComposeValueSet(value) {
|
|
100
|
+
if (typeof value !== "object" || value === null) return false;
|
|
101
|
+
const record = value;
|
|
102
|
+
return record.resourceType === "ValueSet" && typeof record.compose === "object" && record.compose !== null;
|
|
103
|
+
}
|
|
104
|
+
function resolveCompose(resource, codeSystemLookup) {
|
|
105
|
+
if (!isComposeValueSet(resource)) return void 0;
|
|
106
|
+
if (!resource.compose?.include?.length) return void 0;
|
|
107
|
+
const includedCodes = [];
|
|
108
|
+
let isComplete = true;
|
|
109
|
+
for (const include of resource.compose.include) {
|
|
110
|
+
if (include.concept?.length) {
|
|
111
|
+
for (const concept of include.concept) {
|
|
112
|
+
if (concept.code) {
|
|
113
|
+
includedCodes.push({
|
|
114
|
+
code: concept.code,
|
|
115
|
+
display: concept.display,
|
|
116
|
+
system: include.system
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
} else if (include.filter?.length) {
|
|
121
|
+
const cs = include.system ? codeSystemLookup.get(include.system) : void 0;
|
|
122
|
+
if (cs && cs.content === "complete" && cs.concepts.length > 0) {
|
|
123
|
+
const filtered = applyFilters(cs.concepts, include.filter);
|
|
124
|
+
includedCodes.push(...filtered.map((c) => ({ ...c, system: include.system })));
|
|
125
|
+
} else {
|
|
126
|
+
isComplete = false;
|
|
127
|
+
}
|
|
128
|
+
} else if (include.system) {
|
|
129
|
+
const cs = codeSystemLookup.get(include.system);
|
|
130
|
+
if (cs && cs.content === "complete" && cs.concepts.length > 0) {
|
|
131
|
+
includedCodes.push(...cs.concepts.map((c) => ({ ...c, system: include.system })));
|
|
132
|
+
} else {
|
|
133
|
+
isComplete = false;
|
|
134
|
+
}
|
|
135
|
+
} else {
|
|
136
|
+
isComplete = false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (resource.compose.exclude?.length) {
|
|
140
|
+
const excludeSet = /* @__PURE__ */ new Set();
|
|
141
|
+
for (const exclude of resource.compose.exclude) {
|
|
142
|
+
if (exclude.concept?.length) {
|
|
143
|
+
for (const concept of exclude.concept) {
|
|
144
|
+
if (concept.code) {
|
|
145
|
+
const key = exclude.system ? `${exclude.system}|${concept.code}` : concept.code;
|
|
146
|
+
excludeSet.add(key);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (excludeSet.size > 0) {
|
|
152
|
+
const filtered = includedCodes.filter((c) => {
|
|
153
|
+
const key = c.system ? `${c.system}|${c.code}` : c.code;
|
|
154
|
+
return !excludeSet.has(key);
|
|
155
|
+
});
|
|
156
|
+
return {
|
|
157
|
+
url: resource.url ?? "",
|
|
158
|
+
name: resource.name ?? "",
|
|
159
|
+
version: resource.version,
|
|
160
|
+
codes: filtered,
|
|
161
|
+
isComplete
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (includedCodes.length === 0) return void 0;
|
|
166
|
+
return {
|
|
167
|
+
url: resource.url ?? "",
|
|
168
|
+
name: resource.name ?? "",
|
|
169
|
+
version: resource.version,
|
|
170
|
+
codes: includedCodes,
|
|
171
|
+
isComplete
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function applyFilters(concepts, filters) {
|
|
175
|
+
let result = concepts;
|
|
176
|
+
for (const filter of filters) {
|
|
177
|
+
if (filter.op === "=" && filter.property === "concept" && filter.value) {
|
|
178
|
+
result = result.filter((c) => c.code === filter.value);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// src/resolver.ts
|
|
185
|
+
function resolveValueSet(valueSetResource, expansions, codeSystems) {
|
|
186
|
+
const url = extractUrl(valueSetResource);
|
|
187
|
+
if (!url) return void 0;
|
|
188
|
+
const expanded = findExpansion(url, expansions);
|
|
189
|
+
if (expanded) return expanded;
|
|
190
|
+
return resolveCompose(valueSetResource, codeSystems);
|
|
191
|
+
}
|
|
192
|
+
function extractUrl(resource) {
|
|
193
|
+
if (typeof resource !== "object" || resource === null) return void 0;
|
|
194
|
+
const record = resource;
|
|
195
|
+
return typeof record.url === "string" ? record.url : void 0;
|
|
196
|
+
}
|
|
197
|
+
function findExpansion(url, expansions) {
|
|
198
|
+
const exact = expansions.get(url);
|
|
199
|
+
if (exact) return exact;
|
|
200
|
+
const pipeIdx = url.indexOf("|");
|
|
201
|
+
if (pipeIdx > 0) {
|
|
202
|
+
return expansions.get(url.slice(0, pipeIdx));
|
|
203
|
+
}
|
|
204
|
+
return void 0;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// src/registry.ts
|
|
208
|
+
var TerminologyRegistry = class {
|
|
209
|
+
#codeSystems = /* @__PURE__ */ new Map();
|
|
210
|
+
#expansions = /* @__PURE__ */ new Map();
|
|
211
|
+
#valueSets = /* @__PURE__ */ new Map();
|
|
212
|
+
// raw ValueSet resources
|
|
213
|
+
#resolved = /* @__PURE__ */ new Map();
|
|
214
|
+
// URL → resolved
|
|
215
|
+
loadExpansions(entries) {
|
|
216
|
+
for (const entry of entries) {
|
|
217
|
+
const resolved = parseExpansion(entry);
|
|
218
|
+
if (resolved?.url && resolved.codes.length > 0) {
|
|
219
|
+
this.#expansions.set(resolved.url, resolved);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
loadCodeSystem(resource) {
|
|
224
|
+
const cs = parseCodeSystem(resource);
|
|
225
|
+
if (cs?.url) {
|
|
226
|
+
this.#codeSystems.set(cs.url, cs);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
loadValueSet(resource) {
|
|
230
|
+
if (typeof resource !== "object" || resource === null) return;
|
|
231
|
+
const record = resource;
|
|
232
|
+
if (typeof record.url === "string") {
|
|
233
|
+
this.#valueSets.set(record.url, resource);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
resolveAll() {
|
|
237
|
+
for (const [url, resource] of this.#valueSets) {
|
|
238
|
+
const resolved = resolveValueSet(resource, this.#expansions, this.#codeSystems);
|
|
239
|
+
if (resolved && resolved.codes.length > 0) {
|
|
240
|
+
this.#resolved.set(url, resolved);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
for (const [url, expansion] of this.#expansions) {
|
|
244
|
+
if (!this.#resolved.has(url)) {
|
|
245
|
+
this.#resolved.set(url, expansion);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
resolve(url) {
|
|
250
|
+
const exact = this.#resolved.get(url);
|
|
251
|
+
if (exact) return exact;
|
|
252
|
+
const pipeIdx = url.indexOf("|");
|
|
253
|
+
if (pipeIdx > 0) {
|
|
254
|
+
return this.#resolved.get(url.slice(0, pipeIdx));
|
|
255
|
+
}
|
|
256
|
+
return void 0;
|
|
257
|
+
}
|
|
258
|
+
get resolvedCount() {
|
|
259
|
+
return this.#resolved.size;
|
|
260
|
+
}
|
|
261
|
+
get codeSystemCount() {
|
|
262
|
+
return this.#codeSystems.size;
|
|
263
|
+
}
|
|
264
|
+
allResolved() {
|
|
265
|
+
return this.#resolved.values();
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
export {
|
|
269
|
+
TerminologyRegistry,
|
|
270
|
+
parseCodeSystem,
|
|
271
|
+
parseExpansion,
|
|
272
|
+
parseExpansionsBundle,
|
|
273
|
+
resolveCompose,
|
|
274
|
+
resolveValueSet
|
|
275
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fhir-dsl/terminology",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "Compile-time FHIR terminology resolver for ValueSets and CodeSystems",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@fhir-dsl/utils": "0.9.0"
|
|
26
|
+
},
|
|
27
|
+
"author": "Abdelhadi Sabani",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/awbx/fhir-dsl.git",
|
|
32
|
+
"directory": "packages/terminology"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"fhir",
|
|
39
|
+
"terminology",
|
|
40
|
+
"valueset",
|
|
41
|
+
"codesystem",
|
|
42
|
+
"typescript"
|
|
43
|
+
],
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsup",
|
|
46
|
+
"typecheck": "tsc --noEmit",
|
|
47
|
+
"dev": "tsup --watch"
|
|
48
|
+
}
|
|
49
|
+
}
|