@cosyte/terminology 0.0.5 → 0.0.7

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/dist/index.cjs CHANGED
@@ -199,6 +199,9 @@ var TerminologyError = class _TerminologyError extends Error {
199
199
  /**
200
200
  * @param code - The stable {@link FatalCode} describing the fault.
201
201
  * @param message - A **value-free** structural description (path + fault; never an input value).
202
+ * Every piece of it must be engine-owned — a literal, a frozen-table entry, or a locus this
203
+ * package built. This string reaches `err.stack`, so interpolating a caller-supplied or
204
+ * document-derived value into it is how a diagnostic becomes a leak.
202
205
  */
203
206
  constructor(code, message) {
204
207
  super(message);
@@ -652,13 +655,16 @@ function parseCsvRows(content) {
652
655
  function parseCsv(content) {
653
656
  return parseCsvRows(content).rows;
654
657
  }
655
- function requireHeader(header, name) {
658
+ var MISSING_COLUMN_MESSAGE = Object.freeze({
659
+ code: "CSV source: the configured 'code' column is not present in the header row",
660
+ display: "CSV source: the configured 'display' column is not present in the header row",
661
+ status: "CSV source: the configured 'status' column is not present in the header row",
662
+ property: "CSV source: a configured property column is not present in the header row"
663
+ });
664
+ function requireHeader(header, name, role) {
656
665
  const idx = header.indexOf(name);
657
666
  if (idx < 0) {
658
- throw new TerminologyError(
659
- FATAL_CODES.TERM_CODESYSTEM_MALFORMED,
660
- `CSV source: required column '${name}' is not present in the header row`
661
- );
667
+ throw new TerminologyError(FATAL_CODES.TERM_CODESYSTEM_MALFORMED, MISSING_COLUMN_MESSAGE[role]);
662
668
  }
663
669
  return idx;
664
670
  }
@@ -682,12 +688,12 @@ function parseCsvSource(source) {
682
688
  })
683
689
  );
684
690
  }
685
- const codeIdx = requireHeader(header, source.columns.code);
686
- const displayIdx = requireHeader(header, source.columns.display);
687
- const statusIdx = source.columns.status !== void 0 ? requireHeader(header, source.columns.status) : void 0;
691
+ const codeIdx = requireHeader(header, source.columns.code, "code");
692
+ const displayIdx = requireHeader(header, source.columns.display, "display");
693
+ const statusIdx = source.columns.status !== void 0 ? requireHeader(header, source.columns.status, "status") : void 0;
688
694
  const propIdx = (source.columns.properties ?? []).map((n) => ({
689
695
  name: n,
690
- index: requireHeader(header, n)
696
+ index: requireHeader(header, n, "property")
691
697
  }));
692
698
  const needCols = Math.max(codeIdx, displayIdx, statusIdx ?? 0, ...propIdx.map((p) => p.index)) + 1;
693
699
  const concepts = /* @__PURE__ */ new Map();
@@ -1291,48 +1297,54 @@ function makeCoding(system, code, display, version) {
1291
1297
  if (display !== void 0) init.display = display;
1292
1298
  return coding(init);
1293
1299
  }
1294
- function cannotExpand(detail, system) {
1300
+ function cannotExpand(detail, path) {
1295
1301
  const d = { code: "TERM_VALUESET_CANNOT_EXPAND", detail };
1296
- if (system !== void 0) d.system = system;
1302
+ if (path !== void 0) d.path = path;
1297
1303
  return Object.freeze(d);
1298
1304
  }
1299
- function truncated(detail, system) {
1305
+ function truncated(detail, path) {
1300
1306
  const d = { code: "TERM_VALUESET_EXPANSION_TRUNCATED", detail };
1301
- if (system !== void 0) d.system = system;
1307
+ d.path = path;
1302
1308
  return Object.freeze(d);
1303
1309
  }
1310
+ function underPath(d, prefix) {
1311
+ const out = { code: d.code, detail: d.detail };
1312
+ out.path = d.path === void 0 ? prefix : `${prefix}/${d.path}`;
1313
+ return Object.freeze(out);
1314
+ }
1304
1315
  function intersectInto(into, other) {
1305
1316
  for (const k of into.keys()) if (!other.has(k)) into.delete(k);
1306
1317
  return into;
1307
1318
  }
1308
- function expandReferencedValueSets(urls, ctx, visited) {
1319
+ function expandReferencedValueSets(urls, ctx, visited, path) {
1309
1320
  const diagnostics = [];
1310
1321
  let complete = true;
1311
1322
  let acc = null;
1312
- for (const url of urls) {
1323
+ for (const [i, url] of urls.entries()) {
1324
+ const refPath = `${path}.valueSet[${String(i)}]`;
1313
1325
  if (visited.has(url)) {
1314
- diagnostics.push(cannotExpand("cyclic value set reference", url));
1326
+ diagnostics.push(cannotExpand("cyclic value set reference", refPath));
1315
1327
  complete = false;
1316
1328
  acc = /* @__PURE__ */ new Map();
1317
1329
  continue;
1318
1330
  }
1319
1331
  const vs = ctx.valueSets?.get(url);
1320
1332
  if (vs === void 0) {
1321
- diagnostics.push(cannotExpand("referenced value set not supplied", url));
1333
+ diagnostics.push(cannotExpand("referenced value set not supplied", refPath));
1322
1334
  complete = false;
1323
1335
  acc = /* @__PURE__ */ new Map();
1324
1336
  continue;
1325
1337
  }
1326
1338
  const exp = expandInternal(vs, ctx, /* @__PURE__ */ new Set([...visited, url]));
1327
1339
  if (!exp.complete) complete = false;
1328
- for (const d of exp.diagnostics) diagnostics.push(d);
1340
+ for (const d of exp.diagnostics) diagnostics.push(underPath(d, refPath));
1329
1341
  const members = /* @__PURE__ */ new Map();
1330
1342
  for (const c of exp.contains) members.set(codingKey(c.system, c.code), c);
1331
1343
  acc = acc === null ? members : intersectInto(acc, members);
1332
1344
  }
1333
1345
  return { members: acc ?? /* @__PURE__ */ new Map(), complete, diagnostics };
1334
1346
  }
1335
- function expandComponent(component, ctx, visited) {
1347
+ function expandComponent(component, ctx, visited, path) {
1336
1348
  const diagnostics = [];
1337
1349
  let complete = true;
1338
1350
  const { system, version, concept, filter, valueSet: valueSet2 } = component;
@@ -1347,18 +1359,18 @@ function expandComponent(component, ctx, visited) {
1347
1359
  }
1348
1360
  } else if (hasFilter || system !== void 0 && !hasVs) {
1349
1361
  if (system === void 0) {
1350
- diagnostics.push(cannotExpand("intensional filter without a code system 'system'"));
1362
+ diagnostics.push(cannotExpand("intensional filter without a code system 'system'", path));
1351
1363
  return { members: /* @__PURE__ */ new Map(), complete: false, diagnostics };
1352
1364
  }
1353
1365
  const cs = ctx.codeSystems?.get(system);
1354
1366
  if (cs === void 0) {
1355
- diagnostics.push(cannotExpand("code system not supplied for intensional include", system));
1367
+ diagnostics.push(cannotExpand("code system not supplied for intensional include", path));
1356
1368
  return { members: /* @__PURE__ */ new Map(), complete: false, diagnostics };
1357
1369
  }
1358
1370
  if (hasFilter) {
1359
1371
  const unsupported = unsupportedOps(filter);
1360
1372
  if (unsupported.length > 0) {
1361
- diagnostics.push(cannotExpand("unsupported filter operator", system));
1373
+ diagnostics.push(cannotExpand("unsupported filter operator", path));
1362
1374
  return { members: /* @__PURE__ */ new Map(), complete: false, diagnostics };
1363
1375
  }
1364
1376
  const sub = buildSubsumption(cs);
@@ -1382,7 +1394,7 @@ function expandComponent(component, ctx, visited) {
1382
1394
  }
1383
1395
  }
1384
1396
  if (hasVs) {
1385
- const refs = expandReferencedValueSets(valueSet2, ctx, visited);
1397
+ const refs = expandReferencedValueSets(valueSet2, ctx, visited, path);
1386
1398
  for (const d of refs.diagnostics) diagnostics.push(d);
1387
1399
  if (!refs.complete) complete = false;
1388
1400
  base = base === null ? refs.members : intersectInto(base, refs.members);
@@ -1407,7 +1419,7 @@ function expandInternal(vs, ctx, visited) {
1407
1419
  if (vs.expansion.truncated) {
1408
1420
  complete = false;
1409
1421
  diagnostics.push(
1410
- truncated("pre-computed expansion is incomplete (truncated or too-costly)", vs.url)
1422
+ truncated("pre-computed expansion is incomplete (truncated or too-costly)", "expansion")
1411
1423
  );
1412
1424
  }
1413
1425
  const out = {
@@ -1422,14 +1434,14 @@ function expandInternal(vs, ctx, visited) {
1422
1434
  const included = /* @__PURE__ */ new Map();
1423
1435
  const diagnostics = [];
1424
1436
  let complete = true;
1425
- for (const inc of vs.compose.include) {
1426
- const r = expandComponent(inc, ctx, visited);
1437
+ for (const [i, inc] of vs.compose.include.entries()) {
1438
+ const r = expandComponent(inc, ctx, visited, `compose.include[${String(i)}]`);
1427
1439
  if (!r.complete) complete = false;
1428
1440
  for (const d of r.diagnostics) diagnostics.push(d);
1429
1441
  for (const [k, c] of r.members) if (!included.has(k)) included.set(k, c);
1430
1442
  }
1431
- for (const exc of vs.compose.exclude) {
1432
- const r = expandComponent(exc, ctx, visited);
1443
+ for (const [i, exc] of vs.compose.exclude.entries()) {
1444
+ const r = expandComponent(exc, ctx, visited, `compose.exclude[${String(i)}]`);
1433
1445
  for (const d of r.diagnostics) diagnostics.push(d);
1434
1446
  for (const k of r.members.keys()) included.delete(k);
1435
1447
  if (!r.complete) {
@@ -1456,12 +1468,17 @@ function expand(vs, ctx = {}) {
1456
1468
  }
1457
1469
 
1458
1470
  // src/valueset/validate.ts
1459
- function cannotExpand2(detail, system) {
1471
+ function cannotExpand2(detail, path) {
1460
1472
  const d = { code: "TERM_VALUESET_CANNOT_EXPAND", detail };
1461
- if (system !== void 0) d.system = system;
1473
+ if (path !== void 0) d.path = path;
1462
1474
  return Object.freeze(d);
1463
1475
  }
1464
- function matchComponent(target, component, ctx, visited) {
1476
+ function underPath2(d, prefix) {
1477
+ const out = { code: d.code, detail: d.detail };
1478
+ out.path = d.path === void 0 ? prefix : `${prefix}/${d.path}`;
1479
+ return Object.freeze(out);
1480
+ }
1481
+ function matchComponent(target, component, ctx, visited, path) {
1465
1482
  const diagnostics = [];
1466
1483
  const { system, concept, filter, valueSet: valueSet2 } = component;
1467
1484
  const hasConcept = concept !== void 0;
@@ -1475,18 +1492,18 @@ function matchComponent(target, component, ctx, visited) {
1475
1492
  baseMatched = concept.some((c) => c.code === target.code);
1476
1493
  } else if (hasFilter || system !== void 0 && !hasVs) {
1477
1494
  if (system === void 0) {
1478
- diagnostics.push(cannotExpand2("intensional filter without a code system 'system'"));
1495
+ diagnostics.push(cannotExpand2("intensional filter without a code system 'system'", path));
1479
1496
  baseMatched = false;
1480
1497
  baseComplete = false;
1481
1498
  } else {
1482
1499
  const cs = ctx.codeSystems?.get(system);
1483
1500
  if (cs === void 0) {
1484
- diagnostics.push(cannotExpand2("code system not supplied for intensional include", system));
1501
+ diagnostics.push(cannotExpand2("code system not supplied for intensional include", path));
1485
1502
  baseMatched = false;
1486
1503
  baseComplete = false;
1487
1504
  } else if (hasFilter) {
1488
1505
  if (unsupportedOps(filter).length > 0) {
1489
- diagnostics.push(cannotExpand2("unsupported filter operator", system));
1506
+ diagnostics.push(cannotExpand2("unsupported filter operator", path));
1490
1507
  baseMatched = false;
1491
1508
  baseComplete = false;
1492
1509
  } else {
@@ -1501,21 +1518,22 @@ function matchComponent(target, component, ctx, visited) {
1501
1518
  let refDefiniteFalse = false;
1502
1519
  let refUndetermined = false;
1503
1520
  if (hasVs) {
1504
- for (const url of valueSet2) {
1521
+ for (const [i, url] of valueSet2.entries()) {
1522
+ const refPath = `${path}.valueSet[${String(i)}]`;
1505
1523
  if (visited.has(url)) {
1506
- diagnostics.push(cannotExpand2("cyclic value set reference", url));
1524
+ diagnostics.push(cannotExpand2("cyclic value set reference", refPath));
1507
1525
  refUndetermined = true;
1508
1526
  continue;
1509
1527
  }
1510
1528
  const vs = ctx.valueSets?.get(url);
1511
1529
  if (vs === void 0) {
1512
- diagnostics.push(cannotExpand2("referenced value set not supplied", url));
1530
+ diagnostics.push(cannotExpand2("referenced value set not supplied", refPath));
1513
1531
  refUndetermined = true;
1514
1532
  continue;
1515
1533
  }
1516
1534
  const m = validateInternal(target, vs, ctx, /* @__PURE__ */ new Set([...visited, url]));
1517
1535
  if (m.undetermined) {
1518
- for (const d of m.diagnostics) diagnostics.push(d);
1536
+ for (const d of m.diagnostics) diagnostics.push(underPath2(d, refPath));
1519
1537
  refUndetermined = true;
1520
1538
  } else if (!m.result) {
1521
1539
  refDefiniteFalse = true;
@@ -1552,7 +1570,7 @@ function validateInternal(target, vs, ctx, visited) {
1552
1570
  if (found) return decided(true, target);
1553
1571
  if (vs.expansion.truncated) {
1554
1572
  return undetermined(target, [
1555
- cannotExpand2("pre-computed expansion is incomplete (truncated or too-costly)", vs.url)
1573
+ cannotExpand2("pre-computed expansion is incomplete (truncated or too-costly)", "expansion")
1556
1574
  ]);
1557
1575
  }
1558
1576
  return decided(false, target);
@@ -1561,16 +1579,16 @@ function validateInternal(target, vs, ctx, visited) {
1561
1579
  const diagnostics = [];
1562
1580
  let includedDefinite = false;
1563
1581
  let anyIncludeUndetermined = false;
1564
- for (const inc of vs.compose.include) {
1565
- const m = matchComponent(target, inc, ctx, visited);
1582
+ for (const [i, inc] of vs.compose.include.entries()) {
1583
+ const m = matchComponent(target, inc, ctx, visited, `compose.include[${String(i)}]`);
1566
1584
  for (const d of m.diagnostics) diagnostics.push(d);
1567
1585
  if (m.complete && m.matched) includedDefinite = true;
1568
1586
  if (!m.complete) anyIncludeUndetermined = true;
1569
1587
  }
1570
1588
  let excludedDefinite = false;
1571
1589
  let anyExcludeUndetermined = false;
1572
- for (const exc of vs.compose.exclude) {
1573
- const m = matchComponent(target, exc, ctx, visited);
1590
+ for (const [i, exc] of vs.compose.exclude.entries()) {
1591
+ const m = matchComponent(target, exc, ctx, visited, `compose.exclude[${String(i)}]`);
1574
1592
  for (const d of m.diagnostics) diagnostics.push(d);
1575
1593
  if (m.complete && m.matched) excludedDefinite = true;
1576
1594
  if (!m.complete) anyExcludeUndetermined = true;
@@ -3715,7 +3733,29 @@ function parseEssence(xml) {
3715
3733
  atoms.sort((a, b) => b.code.length - a.code.length);
3716
3734
  const atomByCode = new Map(atoms.map((a) => [a.code, a]));
3717
3735
  const prefixByCode = new Map(prefixes.map((p) => [p.code, p]));
3718
- return { prefixes, atoms, atomByCode, prefixByCode };
3736
+ return freezeEssence({ prefixes, atoms, atomByCode, prefixByCode });
3737
+ }
3738
+ function refuseTableWrite() {
3739
+ throw new TypeError("the UCUM unit table is immutable");
3740
+ }
3741
+ function sealLookup(map) {
3742
+ return Object.defineProperties(map, {
3743
+ set: { value: refuseTableWrite },
3744
+ delete: { value: refuseTableWrite },
3745
+ clear: { value: refuseTableWrite }
3746
+ });
3747
+ }
3748
+ function freezeEssence(essence) {
3749
+ for (const atom of essence.atoms) {
3750
+ if (atom.value) Object.freeze(atom.value);
3751
+ Object.freeze(atom);
3752
+ }
3753
+ for (const prefix of essence.prefixes) Object.freeze(prefix);
3754
+ Object.freeze(essence.atoms);
3755
+ Object.freeze(essence.prefixes);
3756
+ sealLookup(essence.atomByCode);
3757
+ sealLookup(essence.prefixByCode);
3758
+ return Object.freeze(essence);
3719
3759
  }
3720
3760
  var cached;
3721
3761
  function loadUcumEssence() {
@@ -3893,32 +3933,37 @@ function power(r, p) {
3893
3933
  }
3894
3934
  return { kind: "linear", factor: Math.pow(r.factor, p), dims };
3895
3935
  }
3896
- var DIMENSIONLESS = { kind: "linear", factor: 1, dims: {} };
3897
- var atomMemo = /* @__PURE__ */ new Map();
3898
- var inProgress = /* @__PURE__ */ new Set();
3936
+ var DIMENSIONLESS = Object.freeze({
3937
+ kind: "linear",
3938
+ factor: 1,
3939
+ dims: Object.freeze({})
3940
+ });
3941
+ var atomMemo = /* @__PURE__ */ new WeakMap();
3942
+ var inProgress = /* @__PURE__ */ new WeakSet();
3899
3943
  function reduceAtomLinear(atom) {
3900
3944
  if (atom.base) return { kind: "linear", factor: 1, dims: { [atom.dim ?? atom.code]: 1 } };
3901
3945
  if (atom.arbitrary) return { kind: "linear", factor: 1, dims: { [`arb:${atom.code}`]: 1 } };
3902
- const cached2 = atomMemo.get(atom.code);
3946
+ const cached2 = atomMemo.get(atom);
3903
3947
  if (cached2) return cached2;
3904
- if (inProgress.has(atom.code)) {
3905
- throw new Error(`cyclic UCUM atom definition for '${atom.code}'`);
3948
+ if (inProgress.has(atom)) {
3949
+ throw new Error("cyclic UCUM atom definition in the unit table");
3906
3950
  }
3907
3951
  if (!atom.value) {
3908
- atomMemo.set(atom.code, DIMENSIONLESS);
3909
- return DIMENSIONLESS;
3952
+ throw new Error("UCUM atom has no linear definition");
3910
3953
  }
3911
- inProgress.add(atom.code);
3912
- const parsed = parseUcum(atom.value.unit);
3913
- if (!parsed.ok) {
3914
- inProgress.delete(atom.code);
3915
- throw new Error(`unparseable UCUM essence definition for '${atom.code}'`);
3954
+ inProgress.add(atom);
3955
+ try {
3956
+ const parsed = parseUcum(atom.value.unit);
3957
+ if (!parsed.ok) {
3958
+ throw new Error("unparseable UCUM essence definition in the unit table");
3959
+ }
3960
+ const reduced = linearReduceTerm(parsed.node);
3961
+ const result = multiply({ kind: "linear", factor: atom.value.factor, dims: {} }, reduced);
3962
+ atomMemo.set(atom, result);
3963
+ return result;
3964
+ } finally {
3965
+ inProgress.delete(atom);
3916
3966
  }
3917
- const reduced = linearReduceTerm(parsed.node);
3918
- const result = multiply({ factor: atom.value.factor, dims: {} }, reduced);
3919
- inProgress.delete(atom.code);
3920
- atomMemo.set(atom.code, result);
3921
- return result;
3922
3967
  }
3923
3968
  function reduceSimpleLinear(node) {
3924
3969
  const base = reduceAtomLinear(node.atom);
@@ -3938,7 +3983,7 @@ function linearReduceComponent(comp) {
3938
3983
  }
3939
3984
  }
3940
3985
  function linearReduceTerm(node) {
3941
- let acc = DIMENSIONLESS;
3986
+ let acc = { kind: "linear", factor: 1, dims: {} };
3942
3987
  for (const { sign, node: comp } of node.factors) {
3943
3988
  acc = multiply(acc, power(linearReduceComponent(comp), sign));
3944
3989
  }
@@ -4120,10 +4165,15 @@ function applyGem(map, source) {
4120
4165
  if (combinations !== void 0) matched.combinations = combinations;
4121
4166
  return Object.freeze(matched);
4122
4167
  }
4168
+ var NOT_INVERTIBLE_MESSAGE = /* @__PURE__ */ new Map([
4169
+ ["9-to-10", "GEM map (direction 9-to-10) cannot be inverted \u2014 load the 10-to-9 GEM file instead"],
4170
+ ["10-to-9", "GEM map (direction 10-to-9) cannot be inverted \u2014 load the 9-to-10 GEM file instead"]
4171
+ ]);
4172
+ var NOT_INVERTIBLE_UNKNOWN_DIRECTION = "GEM map cannot be inverted \u2014 load the reverse GEM file instead";
4123
4173
  function invertGem(map) {
4124
4174
  throw new TerminologyError(
4125
4175
  FATAL_CODES.TERM_MAP_NOT_INVERTIBLE,
4126
- `GEM map (direction ${map.direction}) cannot be inverted \u2014 load the reverse GEM file instead`
4176
+ NOT_INVERTIBLE_MESSAGE.get(map.direction) ?? NOT_INVERTIBLE_UNKNOWN_DIRECTION
4127
4177
  );
4128
4178
  }
4129
4179
 
@@ -4703,7 +4753,7 @@ function approximateMatch(graph, query, options = {}) {
4703
4753
  }
4704
4754
 
4705
4755
  // src/index.ts
4706
- var VERSION = "0.0.5";
4756
+ var VERSION = "0.0.7";
4707
4757
 
4708
4758
  exports.COMPLEX_MAP_SOURCE_SYSTEM = COMPLEX_MAP_SOURCE_SYSTEM;
4709
4759
  exports.DIAGNOSTIC_CODES = DIAGNOSTIC_CODES;