@openmrs/esm-utils
Version:
Helper utilities for OpenMRS
82 lines (81 loc) • 3.04 kB
JavaScript
/** @module @category Utility */ /**
* Gets the formatted display name for a patient.
*
* The display name will be taken from the patient's 'usual' name,
* or may fall back to the patient's 'official' name.
*
* @param patient The patient details in FHIR format.
* @returns The patient's display name or an empty string if name is not present.
*/ export function getPatientName(patient) {
const name = selectPreferredName(patient, 'usual', 'official');
return formatPatientName(name);
}
/** @deprecated Use `getPatientName` */ export function displayName(patient) {
return getPatientName(patient);
}
/**
* Get a formatted display string for an FHIR name.
* @param name The name to be formatted.
* @returns The formatted display name or an empty string if name is undefined.
*/ export function formatPatientName(name) {
if (name) return name.text ?? defaultFormat(name);
return '';
}
/** @deprecated Use `formatPatientName` */ export function formattedName(name) {
return formatPatientName(name);
}
/**
* Select the preferred name from the collection of names associated with a patient.
*
* Names may be specified with a usage such as 'usual', 'official', 'nickname', 'maiden', etc.
* A name with no usage specified is treated as the 'usual' name.
*
* The chosen name will be selected according to the priority order of `preferredNames`.
*
* @param patient The patient from whom a name will be selected.
* @param preferredNames Optional ordered sequence of preferred name usages; defaults to 'usual' if not specified.
* @returns The preferred name for the patient, or undefined if no acceptable name could be found.
*
* @example
* ```ts
* // normal use case; prefer usual name, fallback to official name
* selectPreferredName(patient, 'usual', 'official')
*
* // prefer usual name over nickname, fallback to official name
* selectPreferredName(patient, 'usual', 'nickname', 'official')
* ```
*/ export function selectPreferredName(patient, ...preferredNames) {
if (preferredNames.length == 0) {
preferredNames = [
'usual'
];
}
for (const usage of preferredNames){
const name = patient.name?.find((name)=>nameUsageMatches(name, usage));
if (name) {
return name;
}
}
return undefined;
}
/**
* Generate a display name by concatenating forenames and surname.
* @param name the person's name.
* @returns the person's name as a string.
*/ function defaultFormat(name) {
const forenames = name.given ?? [];
const names = name.family ? forenames.concat(name.family) : forenames;
return names.join(' ');
}
/**
* Determine whether the usage of a given name matches the given NameUse.
*
* A name with no usage is treated as the 'usual' name.
*
* @param name the name to test.
* @param usage the NameUse to test for.
*/ function nameUsageMatches(name, usage) {
if (!name.use) // a name with no usage is treated as 'usual'
return usage === 'usual';
return name.use === usage;
}