From 15efeab2f5a729d2e56907a5c9e46d1f45199bed Mon Sep 17 00:00:00 2001 From: rithviknishad Date: Mon, 8 Jul 2024 19:02:01 +0530 Subject: [PATCH] Adds utility fn. to humanize strings --- src/Utils/utils.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Utils/utils.ts b/src/Utils/utils.ts index d599a494b1c..d5e2be183e3 100644 --- a/src/Utils/utils.ts +++ b/src/Utils/utils.ts @@ -454,3 +454,16 @@ export const isPostPartum = (data_of_delivery?: string) => { export const isAntenatal = (menstruation_start_date?: string) => { return dayjs().diff(menstruation_start_date, "month") <= 9; }; + +export const humanizeStrings = (...values: readonly string[]) => { + if (values.length === 0) { + throw "Empty array of strings cannot be humanized. Array must contain one or more elements"; + } + + if (values.length === 1) { + return values[0]; + } + + const [last, ...items] = [...values].reverse(); + return `${items.reverse().join(", ")} and ${last}`; +};