You've already forked tf2wikipricing
refactor: split currency and key/metal formatting into separate units
this simplifies functions and tests, and will make currency optional/variable later
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { ItemPriceData } from "./priceService";
|
||||
import { convertTF2PriceToUSD } from "./utils/currency";
|
||||
import { formatPrice } from "./utils/formatting";
|
||||
import { $T } from "./utils/localization";
|
||||
|
||||
@@ -19,8 +20,22 @@ export function createPriceRow(qualityName: string, data: ItemPriceData, keyPric
|
||||
|
||||
const priceData = document.createElement("td");
|
||||
const priceLink = document.createElement("span");
|
||||
const priceString = data ? formatPrice(data.keys, data.metal, keyPrice.metal, locale).trim() : $T('Data unavailable')
|
||||
priceLink.innerHTML = priceString // + `<br>$${data.scmPrice}`
|
||||
var priceString: string = ''
|
||||
|
||||
if(data) {
|
||||
const currencyFormatter = new Intl.NumberFormat(locale, {
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
});
|
||||
|
||||
priceString += [
|
||||
formatPrice(data.keys, data.metal, keyPrice.metal, locale).trim(),
|
||||
'(US$' + currencyFormatter.format(convertTF2PriceToUSD(data.keys, data.metal, keyPrice.metal)) + ')'
|
||||
].join(' ').trim()
|
||||
} else {
|
||||
priceString += $T('Data unavailable')
|
||||
}
|
||||
priceLink.innerHTML = priceString
|
||||
priceData.appendChild(priceLink);
|
||||
priceRow.appendChild(priceData);
|
||||
return priceRow;
|
||||
|
||||
8
src/content/utils/currency.ts
Normal file
8
src/content/utils/currency.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { conversion_ref_usd } from '../config';
|
||||
|
||||
export function convertTF2PriceToUSD(keys: number, metal: number, keyPrice: number): number {
|
||||
const pureMetal = (keys * keyPrice) + metal;
|
||||
|
||||
// Round price up to nearest cent
|
||||
return Math.ceil(pureMetal * conversion_ref_usd * 100) / 100
|
||||
}
|
||||
@@ -7,7 +7,6 @@ function toFixed(num: number, fixed: number) {
|
||||
}
|
||||
|
||||
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 = ''
|
||||
@@ -16,13 +15,6 @@ export function formatPrice(keys: number, metal: number, keyPrice: number, local
|
||||
} else {
|
||||
output += $T("%@ ref").replace('%@', (+toFixed(metal, 2)).toLocaleString(locale))
|
||||
}
|
||||
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