All files / extensions/cornerstone-dicom-sr/src/utils formatContentItem.ts

3.44% Statements 1/29
0% Branches 0/11
0% Functions 0/9
3.44% Lines 1/29

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                    1x                                                                                                                  
import { utils } from '@ohif/core';
 
/**
 * Formatters used to format each of the content items (SR "nodes") which can be
 * text, code, UID ref, number, person name, date, time and date time. Each
 * formatter must be a function with the following signature:
 *
 *    [VALUE_TYPE]: (contentItem) => string
 *
 */
const contentItemFormatters = {
  TEXT: contentItem => contentItem.TextValue,
  CODE: contentItem => contentItem.ConceptCodeSequence?.[0]?.CodeMeaning,
  UIDREF: contentItem => contentItem.UID,
  NUM: contentItem => {
    const measuredValue = contentItem.MeasuredValueSequence?.[0];
 
    Iif (!measuredValue) {
      return;
    }
 
    const { NumericValue, MeasurementUnitsCodeSequence } = measuredValue;
    const { CodeValue } = MeasurementUnitsCodeSequence;
 
    return `${NumericValue} ${CodeValue}`;
  },
  PNAME: contentItem => {
    const personName = contentItem.PersonName?.[0];
    return personName ? utils.formatPN(personName) : undefined;
  },
  DATE: contentItem => {
    const { Date } = contentItem;
    return Date ? utils.formatDate(Date) : undefined;
  },
  TIME: contentItem => {
    const { Time } = contentItem;
    return Time ? utils.formatTime(Time) : undefined;
  },
  DATETIME: contentItem => {
    const { DateTime } = contentItem;
 
    Iif (typeof DateTime !== 'string') {
      return;
    }
 
    // 14 characters because it should be something like 20180614113714
    Iif (DateTime.length < 14) {
      return DateTime;
    }
 
    const dicomDate = DateTime.substring(0, 8);
    const dicomTime = DateTime.substring(8, 14);
    const formattedDate = utils.formatDate(dicomDate);
    const formattedTime = utils.formatTime(dicomTime);
 
    return `${formattedDate} ${formattedTime}`;
  },
};
 
function formatContentItemValue(contentItem) {
  const { ValueType } = contentItem;
  const fnFormat = contentItemFormatters[ValueType];
 
  return fnFormat ? fnFormat(contentItem) : `[${ValueType} is not supported]`;
}
 
export { formatContentItemValue as default, formatContentItemValue };