diff --git a/components/helpers/home.js b/components/helpers/home.js index 4851091b0313e4c4a421dea2d5dbb12f67250dcc..e5b75c30a2eecd4274179bfe38086050cb9097e8 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) +}