@ubercode/dcmtk 0.5.0 → 0.6.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/dist/servers.cjs CHANGED
@@ -1557,6 +1557,45 @@ var ChangeSet = class _ChangeSet {
1557
1557
  return result;
1558
1558
  }
1559
1559
  };
1560
+
1561
+ // src/dicom/walkTags.ts
1562
+ var DEFAULT_MAX_DEPTH = 16;
1563
+ function walkTags(data, options) {
1564
+ const ctx = {
1565
+ maxDepth: options?.maxDepth ?? DEFAULT_MAX_DEPTH,
1566
+ vrSet: options?.vrFilter !== void 0 ? new Set(options.vrFilter) : void 0,
1567
+ results: []
1568
+ };
1569
+ walkLevel(data, 0, "", ctx);
1570
+ return ctx.results;
1571
+ }
1572
+ function walkLevel(data, depth, pathPrefix, ctx) {
1573
+ const keys = Object.keys(data);
1574
+ for (let i = 0; i < keys.length; i++) {
1575
+ const tag = keys[i];
1576
+ if (tag === void 0) continue;
1577
+ const element = data[tag];
1578
+ if (element === void 0) continue;
1579
+ const vr = element.vr;
1580
+ const path2 = pathPrefix.length > 0 ? `${pathPrefix}.${tag}` : tag;
1581
+ if (ctx.vrSet === void 0 || ctx.vrSet.has(vr)) {
1582
+ ctx.results.push({ tag, element, vr, depth, path: path2 });
1583
+ }
1584
+ if (vr === "SQ" && element.Value !== void 0 && depth < ctx.maxDepth) {
1585
+ walkSequenceItems(element.Value, depth, path2, ctx);
1586
+ }
1587
+ }
1588
+ }
1589
+ function walkSequenceItems(items, depth, parentPath, ctx) {
1590
+ for (let idx = 0; idx < items.length; idx++) {
1591
+ const item = items[idx];
1592
+ if (typeof item !== "object" || item === null) continue;
1593
+ const itemPath = `${parentPath}[${idx}]`;
1594
+ walkLevel(item, depth + 1, itemPath, ctx);
1595
+ }
1596
+ }
1597
+
1598
+ // src/dicom/DicomDataset.ts
1560
1599
  var HEX_TAG_KEY = /^[0-9A-Fa-f]{8}$/;
1561
1600
  function isDicomJsonElement(value) {
1562
1601
  return typeof value === "object" && value !== null && "vr" in value && typeof value["vr"] === "string";
@@ -1885,6 +1924,15 @@ var DicomDataset = class _DicomDataset {
1885
1924
  const result = collectWildcard(this.data, segments);
1886
1925
  return result.values;
1887
1926
  }
1927
+ /**
1928
+ * Walks all tags in this dataset, optionally filtering by VR and recursing into sequences.
1929
+ *
1930
+ * @param options - Optional VR filter and max depth
1931
+ * @returns A readonly array of all matching tag entries
1932
+ */
1933
+ walkTags(options) {
1934
+ return walkTags(this.data, options);
1935
+ }
1888
1936
  // -----------------------------------------------------------------------
1889
1937
  // Convenience readonly getters
1890
1938
  // -----------------------------------------------------------------------
@@ -2097,15 +2145,32 @@ function convertSequence(attr, element) {
2097
2145
  }
2098
2146
  if (values.length > 0) element.Value = values;
2099
2147
  }
2100
- function convertRegularValue(attr, element) {
2148
+ var NUMERIC_JSON_VRS = /* @__PURE__ */ new Set(["DS", "FL", "FD", "IS", "SL", "SS", "SV", "UL", "US", "UV"]);
2149
+ function unwrapValue(v) {
2150
+ if (typeof v !== "object" || v === null) return v;
2151
+ const obj = v;
2152
+ if ("#text" in obj) return obj["#text"];
2153
+ const keys = Object.keys(obj);
2154
+ if (keys.length === 1 && keys[0] !== void 0 && keys[0].startsWith("@_")) {
2155
+ return obj[keys[0]];
2156
+ }
2157
+ return v;
2158
+ }
2159
+ function coerceNumeric(value) {
2160
+ if (typeof value === "number") return value;
2161
+ if (typeof value !== "string") return value;
2162
+ const trimmed = value.trim();
2163
+ if (trimmed.length === 0) return value;
2164
+ const num = Number(trimmed);
2165
+ return Number.isNaN(num) ? value : num;
2166
+ }
2167
+ function convertRegularValue(attr, element, vr) {
2101
2168
  const valArray = toArray(attr.Value);
2102
2169
  const values = [];
2170
+ const isNumeric = NUMERIC_JSON_VRS.has(vr);
2103
2171
  for (const v of valArray) {
2104
- if (typeof v === "object" && v !== null && "#text" in v) {
2105
- values.push(v["#text"]);
2106
- } else {
2107
- values.push(v);
2108
- }
2172
+ const unwrapped = unwrapValue(v);
2173
+ values.push(isNumeric ? coerceNumeric(unwrapped) : unwrapped);
2109
2174
  }
2110
2175
  if (values.length > 0) element.Value = values;
2111
2176
  }
@@ -2122,7 +2187,7 @@ function convertElement(attr) {
2122
2187
  } else if (element.vr === "SQ" && attr.Item !== void 0) {
2123
2188
  convertSequence(attr, element);
2124
2189
  } else if (attr.Value !== void 0) {
2125
- convertRegularValue(attr, element);
2190
+ convertRegularValue(attr, element, vr);
2126
2191
  }
2127
2192
  return Object.freeze(element);
2128
2193
  }