From 0550d99db54d17d3646d232237bd2142cef54293 Mon Sep 17 00:00:00 2001 From: Siyasanga Date: Thu, 24 Oct 2024 00:46:20 +0200 Subject: [PATCH] Create a utility function getLocalisedname() To make the name more usable we had to extract the name formatting logic into it's own function. https://github.com/opencrvs/opencrvs-core/issues/6830 --- packages/client/src/utils/data-formatting.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/client/src/utils/data-formatting.ts b/packages/client/src/utils/data-formatting.ts index 57082f2a4fb..cf88ee37ce7 100644 --- a/packages/client/src/utils/data-formatting.ts +++ b/packages/client/src/utils/data-formatting.ts @@ -11,6 +11,8 @@ import { getDefaultLanguage } from '@client/i18n/utils' import type { GQLComment } from '@client/utils/gateway-deprecated-do-not-use' import { HumanName } from './gateway' +import { constantsMessages } from '@client/i18n/messages' +import { IntlShape } from 'react-intl' interface INamesMap { [key: string]: string @@ -86,3 +88,17 @@ export const mergeArraysRemovingEmptyStrings = ( export function getPercentage(total: number, current: number) { return current === 0 || total === 0 ? 0 : (current / total) * 100 } + +export function getLocalisedName( + intl: IntlShape, + nameObject: HumanName +): string { + return intl + .formatMessage(constantsMessages.humanName, { + firstName: nameObject.firstNames, + middleName: nameObject.middleName, + lastName: nameObject.familyName + }) + .replace(/\s+/g, ' ') // Remove extra spaces + .trim() +}