You've already forked tf2wikipricing
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:
28
src/content/utils/formatting.ts
Normal file
28
src/content/utils/formatting.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user