All files / metadata/src/utilities getNaturalizedField.ts

75% Statements 15/20
66.66% Branches 20/30
100% Functions 3/3
75% Lines 15/20

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                                21537x       21537x 21537x 10761x     10776x             10776x       10776x                       10656x           10656x     10656x                       10881x           10881x 74x   10807x 10807x    
/**
 * Reads an UpperCamelCase field from a NATURALIZED metadata object.
 *
 * `fieldName` must be the exact NATURALIZED key (UpperCamelCase DICOM-style
 * attribute name, for example `PhotometricInterpretation` or `NumberOfFrames`).
 * This helper does not perform any casing fallback.
 *
 * `defaultValue` is returned when the field (or indexed value) is unavailable.
 * `index` applies only to array values; non-array values require `index = 0`.
 */
export function getNaturalizedField<T = unknown>(
  naturalized: Record<string, unknown> | null | undefined,
  fieldName: string,
  defaultValue: T | undefined = undefined,
  index = 0
): unknown | T | undefined {
  Iif (!naturalized) {
    return defaultValue;
  }
 
  const value = naturalized[fieldName];
  if (value === undefined || value === null) {
    return defaultValue;
  }
 
  Iif (Array.isArray(value)) {
    const indexedValue = value[index];
    return indexedValue === undefined || indexedValue === null
      ? defaultValue
      : indexedValue;
  }
 
  Iif (index !== 0) {
    return defaultValue;
  }
 
  return value;
}
 
/**
 * Reads a NATURALIZED field as a string.
 */
export function getNaturalizedString(
  naturalized: Record<string, unknown> | null | undefined,
  fieldName: string,
  defaultValue: string | undefined = undefined,
  index = 0
): string | undefined {
  const value = getNaturalizedField(
    naturalized,
    fieldName,
    defaultValue,
    index
  );
  Iif (value === null || value === undefined) {
    return defaultValue;
  }
  return String(value);
}
 
/**
 * Reads a NATURALIZED field as a finite number.
 */
export function getNaturalizedNumber(
  naturalized: Record<string, unknown> | null | undefined,
  fieldName: string,
  defaultValue: number | undefined = undefined,
  index = 0
): number | undefined {
  const value = getNaturalizedField(
    naturalized,
    fieldName,
    defaultValue,
    index
  );
  if (value === null || value === undefined) {
    return defaultValue;
  }
  const parsedValue = Number(value);
  return Number.isFinite(parsedValue) ? parsedValue : defaultValue;
}