From 9d3c332453cd726d61592c23342da0772aeb94db Mon Sep 17 00:00:00 2001 From: Sofiya Tepikin <sofiya.tepikin@gmail.com> Date: Fri, 4 Dec 2020 19:30:15 +0100 Subject: [PATCH] Fix getOrdinalSuffix and add a formatting function --- components/helpers/home.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/components/helpers/home.js b/components/helpers/home.js index 4851091b..e5b75c30 100644 --- a/components/helpers/home.js +++ b/components/helpers/home.js @@ -64,14 +64,24 @@ export function getBleedingPredictionRange(prediction) { } export function getOrdinalSuffix(num) { - const suffixes = { - 1: 'st', - 2: 'nd', - 3: 'rd', - default: 'th' + const j = num % 10 + const k = num % 100 + + if (j === 1 && k !== 11) { + return 'st' + } + + if (j === 2 && k !== 12) { + return 'nd' + } + + if (j === 3 && k !== 13) { + return 'rd' } - const numAsString = num.toString() - const lastNumber = numAsString[numAsString.length - 1] - return suffixes[lastNumber] || suffixes.default -} \ No newline at end of file + return 'th' +} + +export function formatWithOrdinalSuffix(num) { + return num + getOrdinalSuffix(num) +} -- GitLab