Skip to content

Commit

Permalink
update humanizeStrings implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad committed Jul 8, 2024
1 parent 73a4a39 commit 1680eda
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Components/Form/FormFields/PhoneNumberFormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ const PhoneNumberTypesHelp = (props: { types: PhoneNumberType[] }) => {
<div className="tooltip-text tooltip-left text-sm">
Supports only{" "}
<span className="font-bold lowercase">
{humanizeStrings(...props.types.map((item) => t(item)))}
{humanizeStrings(props.types.map((item) => t(item)))}
</span>{" "}
numbers.
</div>
Expand Down
16 changes: 11 additions & 5 deletions src/Utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,15 +455,21 @@ 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) {
/**
* A utility method to format an array of string to human readable format.
*
* @param values Array of strings to be made human readable.
* @returns Human readable version of the list of strings
*/
export const humanizeStrings = (strings: readonly string[]) => {
if (strings.length === 0) {
throw "Empty array of strings cannot be humanized. Array must contain one or more elements";
}

if (values.length === 1) {
return values[0];
if (strings.length === 1) {
return strings[0];
}

const [last, ...items] = [...values].reverse();
const [last, ...items] = [...strings].reverse();
return `${items.reverse().join(", ")} and ${last}`;
};

0 comments on commit 1680eda

Please sign in to comment.