@ubercode/dcmtk 0.4.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/tools.cjs CHANGED
@@ -407,15 +407,32 @@ function convertSequence(attr, element) {
407
407
  }
408
408
  if (values.length > 0) element.Value = values;
409
409
  }
410
- function convertRegularValue(attr, element) {
410
+ var NUMERIC_JSON_VRS = /* @__PURE__ */ new Set(["DS", "FL", "FD", "IS", "SL", "SS", "SV", "UL", "US", "UV"]);
411
+ function unwrapValue(v) {
412
+ if (typeof v !== "object" || v === null) return v;
413
+ const obj = v;
414
+ if ("#text" in obj) return obj["#text"];
415
+ const keys = Object.keys(obj);
416
+ if (keys.length === 1 && keys[0] !== void 0 && keys[0].startsWith("@_")) {
417
+ return obj[keys[0]];
418
+ }
419
+ return v;
420
+ }
421
+ function coerceNumeric(value) {
422
+ if (typeof value === "number") return value;
423
+ if (typeof value !== "string") return value;
424
+ const trimmed = value.trim();
425
+ if (trimmed.length === 0) return value;
426
+ const num = Number(trimmed);
427
+ return Number.isNaN(num) ? value : num;
428
+ }
429
+ function convertRegularValue(attr, element, vr) {
411
430
  const valArray = toArray(attr.Value);
412
431
  const values = [];
432
+ const isNumeric = NUMERIC_JSON_VRS.has(vr);
413
433
  for (const v of valArray) {
414
- if (typeof v === "object" && v !== null && "#text" in v) {
415
- values.push(v["#text"]);
416
- } else {
417
- values.push(v);
418
- }
434
+ const unwrapped = unwrapValue(v);
435
+ values.push(isNumeric ? coerceNumeric(unwrapped) : unwrapped);
419
436
  }
420
437
  if (values.length > 0) element.Value = values;
421
438
  }
@@ -432,7 +449,7 @@ function convertElement(attr) {
432
449
  } else if (element.vr === "SQ" && attr.Item !== void 0) {
433
450
  convertSequence(attr, element);
434
451
  } else if (attr.Value !== void 0) {
435
- convertRegularValue(attr, element);
452
+ convertRegularValue(attr, element, vr);
436
453
  }
437
454
  return Object.freeze(element);
438
455
  }