All files / extensions/default/src/DicomTagBrowser getDatasetEntries.ts

100% Statements 10/10
80% Branches 4/5
100% Functions 4/4
100% Lines 10/10

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                  1998x 8061x 8061x 8053x   8x 8x 8x 42x 26x         8x      
/**
 * Returns the `[key, value]` entries of a naturalized dataset to list. dcmjs 1.0
 * groups a private creator and its elements under one `gggg:creator` key, and
 * keeps the hex tags only as non-enumerable aliases, which a copy of the dataset
 * loses. This expands such a block to the hex tag of the creator and of each
 * element, with the values read from the block, so that the browser lists each
 * private tag also for a copied dataset.
 */
export default function getDatasetEntries(metadata): [string, unknown][] {
  return Object.keys(metadata).flatMap((key): [string, unknown][] => {
    const block = metadata[key];
    if (!/^[0-9A-F]{4}:/i.test(key) || typeof block?.reservedBlock !== 'number') {
      return [[key, block]];
    }
    const group = key.slice(0, 4).toUpperCase();
    const blockHex = block.reservedBlock.toString(16).toUpperCase().padStart(2, '0');
    const elements = Object.keys(block)
      .filter(name => /^x[0-9A-F]{2}$/i.test(name))
      .map((name): [string, unknown] => [
        `${group}${blockHex}${name.slice(1).toUpperCase()}`,
        block[name],
      ]);
    // block.creator is not escaped; the key escapes characters such as \n.
    return [[`${group}00${blockHex}`, block.creator ?? key.slice(5)], ...elements];
  });
}