All files / metadata/src/utilities Tags.ts

88.23% Statements 60/68
74.35% Branches 29/39
85.71% Functions 6/7
88.05% Lines 59/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183                    128x 128x                                         15616x                                   693730x     693730x     693730x   693730x 618666x     75064x                   3514958x                     128x   128x           15616x         15616x 15616x 15616x 15616x 15616x 15616x 15616x 15616x 15616x 15616x 15616x 15616x     15616x 15616x 15616x 15616x 15616x       15616x 13952x 14976x 14976x 2560x 2560x 2560x   88192x 12416x 12416x                         15225x 15225x     15225x             128x       128x   128x 2688x 17280x 17280x 17280x 17280x 15616x 15616x 15616x   17280x 15616x           128x 15616x       15616x    
import * as metaData from '../metaData';
import dcmjs from 'dcmjs';
import {
  moduleDefinitions,
  USRegionChild,
  CLINICAL_TRIAL,
  RadiopharmaceuticalInfoModule,
} from './modules';
import type { ModuleTagEntry } from './modules';
 
const dicomDictionary = dcmjs.data.DicomMetaDictionary.dictionary;
const nameMap = dcmjs.data.DicomMetaDictionary.nameMap;
 
// Re-export custom module name constants for backward compatibility
export { USRegionChild, CLINICAL_TRIAL, RadiopharmaceuticalInfoModule };
 
export interface TagEntry {
  name: string;
  lowerName: string;
  xTag: string;
  vm: number;
  tag: string;
  vr: string;
  primaryGroup: string;
  groups: string[];
}
 
/**
 * Creates a tag entry defining module membership only.
 * VR and VM are resolved from the dcmjs dictionary in addTag().
 */
export function createTagEntry(hexTag: string, ...groups: string[]): TagEntry {
  return {
    tag: hexTag,
    groups,
    xTag: null,
    primaryGroup: null,
    name: null,
    lowerName: null,
    vr: null,
    vm: null,
  };
}
 
/**
 * Parses a DICOM VM string (e.g. "1", "1-n", "2") into a numeric value.
 * Returns 1 for single-valued, the exact count for fixed multi-valued,
 * or 0 for variable-length multi-valued.
 */
export function parseVm(vm: string | number | undefined): number | null {
  Iif (vm === undefined || vm === null) {
    return null;
  }
  Iif (typeof vm === 'number') {
    return vm;
  }
  const n = parseInt(vm, 10);
  // If the string is exactly a number (like "1", "2", "3"), return it
  if (String(n) === vm) {
    return n;
  }
  // Otherwise it's a range like "1-n", "2-n", "1-3" → multi-valued
  return 0;
}
 
/**
 * Looks up a tag in the dcmjs dictionary by hex string (e.g. "00080005").
 * Returns { name, vr, vm } or undefined if not found.
 */
export function dictionaryLookup(
  hexTag: string
): { name: string; vr: string; vm: string } | undefined {
  return dicomDictionary[hexTag.toUpperCase()];
}
 
/**
 * Looks up the hex tag code for a natural tag name (e.g. "SOPInstanceUID" → "00080018").
 * Uses the mapTagInfo registry which is populated from the Tags definitions.
 */
export function getTagCodeByName(name: string): string | undefined {
  return mapTagInfo.get(name)?.tag;
}
 
export const mapModuleTags = new Map<string, TagEntry[]>();
 
export const mapTagInfo = new Map<string, TagEntry>();
 
/**
 * Adds a tag name/type, resolving vr/vm from the dcmjs dictionary.
 */
export function addTag(name: string, value: TagEntry) {
  Iif (name && value.name && name !== value.name) {
    throw new Error(
      `Tag name provided and value don't match: ${name} !== ${value.name}`
    );
  }
  value.name ||= name;
  value.lowerName ||= metaData.toLowerCamelTag(name);
  Tags[name] = value;
  const { tag: hexTag } = value;
  value.primaryGroup ||= value.groups?.[0];
  const { groups } = value;
  mapTagInfo.set(name, value);
  Eif (hexTag) {
    value.xTag = `x${hexTag.toLowerCase()}`;
    value.tag = hexTag.toUpperCase();
    mapTagInfo.set(value.xTag, value);
    mapTagInfo.set(value.tag, value);
 
    // Resolve vr/vm from dcmjs dictionary if not already set
    Eif (!value.vr) {
      const dictEntry = dicomDictionary[value.tag];
      Eif (dictEntry) {
        value.vr = dictEntry.vr;
        value.vm = parseVm(dictEntry.vm);
      }
    }
  }
  if (groups?.length) {
    for (const group of groups) {
      let moduleEntries = mapModuleTags.get(group);
      if (!moduleEntries) {
        moduleEntries = [value];
        mapModuleTags.set(group, moduleEntries);
        return;
      }
      const foundIndex = moduleEntries.findIndex((it) => it.name === name);
      if (foundIndex === -1) {
        moduleEntries.push(value);
      } else E{
        moduleEntries[foundIndex] = value;
      }
    }
  }
}
 
/**
 * Resolves a tag keyword to its hex code using dcmjs nameMap.
 * nameMap entries have tag in "(GGGG,EEEE)" format; we unpunctuate to "GGGGEEEE".
 */
export function resolveHexFromKeyword(keyword: string): string | undefined {
  const entry = nameMap[keyword];
  Iif (!entry) {
    return undefined;
  }
  return entry.tag.substring(1, 5) + entry.tag.substring(6, 10);
}
 
/**
 * The Tags object. Built at module load time from moduleDefinitions
 * and dcmjs nameMap lookups.
 */
export const Tags: Record<string, TagEntry> = {};
 
// Accumulate groups per keyword across all module definitions.
// Tags appearing in multiple modules get all their groups collected.
const tagGroups = new Map<string, { hex: string; groups: string[] }>();
 
for (const [moduleName, keywords] of moduleDefinitions) {
  for (const entry of keywords as ModuleTagEntry[]) {
    const keyword = typeof entry === 'string' ? entry : entry[0];
    const hexOverride = typeof entry === 'string' ? undefined : entry[1];
    let data = tagGroups.get(keyword);
    if (!data) {
      const hex = hexOverride ?? resolveHexFromKeyword(keyword);
      data = { hex, groups: [] };
      tagGroups.set(keyword, data);
    }
    if (moduleName !== null) {
      data.groups.push(moduleName);
    }
  }
}
 
// Create TagEntry objects and register them
for (const [keyword, { hex, groups }] of tagGroups) {
  Iif (!hex) {
    console.warn(`Tags: keyword "${keyword}" not found in dcmjs nameMap`);
    continue;
  }
  addTag(keyword, createTagEntry(hex, ...groups));
}