From c124a15d303b14f089a7de91de124b2fde083455 Mon Sep 17 00:00:00 2001 From: vytdev <123163428+vytdev@users.noreply.github.com> Date: Fri, 23 Feb 2024 10:07:57 +0800 Subject: [PATCH] added toRomanNumeral() utility --- src/catalyst/core/utils.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/catalyst/core/utils.ts b/src/catalyst/core/utils.ts index 4d4a892..ccd92a0 100644 --- a/src/catalyst/core/utils.ts +++ b/src/catalyst/core/utils.ts @@ -74,3 +74,38 @@ export function formatNumber(n: number): string { // thx: https://stackoverflow.com/questions/2901102 return n.toString().replace(/\B(? 3999) return num.toString(); + + // in minecraft, unicode diacritic `\u0305` (macron) may not display + // correctly, better to not add them + + // i used recursion because its easy to debug :) + if(num >= 1000) return 'M' + toRomanNumeral(num - 1000); + if(num >= 900) return 'CM' + toRomanNumeral(num - 900); + if(num >= 500) return 'D' + toRomanNumeral(num - 500); + if(num >= 400) return 'CD' + toRomanNumeral(num - 400); + if(num >= 100) return 'C' + toRomanNumeral(num - 100); + if(num >= 90) return 'XC' + toRomanNumeral(num - 90); + if(num >= 50) return 'L' + toRomanNumeral(num - 50); + if(num >= 40) return 'XL' + toRomanNumeral(num - 40); + if(num >= 10) return 'X' + toRomanNumeral(num - 10); + if(num >= 9) return 'IX' + toRomanNumeral(num - 9); + if(num >= 5) return 'V' + toRomanNumeral(num - 5); + if(num >= 4) return 'IV' + toRomanNumeral(num - 4); + if(num >= 1) return 'I' + toRomanNumeral(num - 1); + + return ''; +}