Raw
1 /**
2 * Providers only have an ID to identify them. This function formats the ID
3 * into a human-readable string.
4 *
5 * Format example: pp_stripe-blik_dkk
6 *
7 * @param id - The ID of the provider
8 * @returns A formatted string
9 */
10 export const formatProvider = (id: string) => {
11 const [_, name, ...rest] = id.split("_")
12
13 if (!name) {
14 return id
15 }
16
17 const formattedName = name
18 .split("-")
19 .map((s) => s.charAt(0).toUpperCase() + s.slice(1))
20 .join(" ")
21
22 if (!rest.length) {
23 return formattedName
24 }
25
26 // A single short part (eg. a currency code) reads better uppercased, while longer
27 // identifiers (eg. a payment account ID like acct_123) are kept as they are.
28 const type = rest.length === 1 ? rest[0].toUpperCase() : rest.join("_")
29
30 return `${formattedName} (${type})`
31 }