refactor: moved components to separate modules

`content.ts` is now half less than the size, exported functions can be unit tested
This commit is contained in:
xenticore
2025-03-24 15:39:28 -04:00
parent 493e7b6521
commit 12de4a7148
9 changed files with 326 additions and 351 deletions

View File

@@ -0,0 +1,28 @@
import { conversion_ref_usd } from '../config';
import { $T } from './localization'
function toFixed(num: number, fixed: number) {
var re = new RegExp('^-?\\d+(?:\.\\d{0,' + (fixed || -1) + '})?');
return num.toString().match(re)[0];
}
export function formatPrice(keys: number, metal: number, keyPrice: number, locale: string = 'en') {
const pureMetal = (keys * keyPrice) + metal;
const formattedKeys = +(keys + (metal / keyPrice)).toFixed(2)
var output: string = ''
if(keys > 0) {
output += (formattedKeys == 1.0 ? $T("%@ key") : $T("%@ keys")).replace('%@', formattedKeys.toLocaleString(locale))
} else {
output += `${(+toFixed(metal, 2)).toLocaleString(locale)} ref`
}
const currencyFormatter = new Intl.NumberFormat(locale, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
// Round price up to nearest cent
const price = Math.ceil(pureMetal * conversion_ref_usd * 100) / 100
output += ` (US$${currencyFormatter.format(price)})`
return output;
}