All files / adapters/src/utilities createInstance.ts

0% Statements 0/19
0% Branches 0/11
0% Functions 0/2
0% Lines 0/19

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                                                                                                                       
import { metaData, Enums } from '@cornerstonejs/core';
 
const { MetadataModules } = Enums;
 
/**
 * Assign only defined values in source into destination.
 * Optionally requires an existing key in the result too.
 */
export function assignDefined(dest, source, options?) {
  if (!source) {
    return;
  }
  for (const [key, value] of Object.entries(source)) {
    if (value === undefined) {
      continue;
    }
    if (dest[key] === undefined && options?.requireDestinationKey) {
      continue;
    }
    dest[key] = value;
  }
}
 
/**
 * Creates a new instance example tag, based on the metadata key `instanceKey`
 * and using the imageId from studyExemplarImageId as the base for the study
 * information, but NOT the instance information.  This produces a valid DICOM
 * instance data, particularly when the image id refers to a very different type
 * from the new instance being created.
 *
 * @param instanceKey used to get the default data for this type of object
 * @param studyExemplarImageId - used to get the study data for this object
 * @param base - additional defaults to include in the result
 * @param options - get the predecessorImageId and include a predecessor sequence
 *     as well as putting the new object into the same series as the old one.
 *       As well for Options, any attributes defined in it as well as in the newly
 *       created object will be assigned to the newly created object.
 */
export function createInstance<T>(
  instanceKey,
  studyExemplarImageId,
  base,
  options
) {
  const { metadataProvider = metaData, predecessorImageId } = options;
  const result = <T>{};
  const instanceBase = metadataProvider.get(instanceKey, studyExemplarImageId);
  Object.assign(result, instanceBase);
  assignDefined(result, base);
  assignDefined(result, options, { requireDestinationKey: true });
  if (predecessorImageId) {
    const predecessor = metadataProvider.get(
      MetadataModules.PREDECESSOR_SEQUENCE,
      predecessorImageId
    );
    Object.assign(result, predecessor);
  }
  return result;
}