Implement the new currency config into the graphics and helper methods

For now all the code still uses `formatUSD` but it's getting "forwarded" onto the new `formatCurrency` method which supports overrides as well
This commit is contained in:
Lordmau5 2024-03-14 09:06:20 +01:00
parent e5c993d5dc
commit 4d521b4e12
3 changed files with 50 additions and 11 deletions

View File

@ -33,15 +33,43 @@ export function msToTimeStr(ms: number): string {
}:${padTimeNumber(seconds)}`; }:${padTimeNumber(seconds)}`;
} }
/**
* Simple formatter for displaying currency amounts.
* @param amount Amount as a integer/float.
* @param _symbol The currency symbol
* (defaults to what's set in the config, or `$` if the config isn't set)
* @param _countryCode The country code
* (defaults to what's set in the config, or `en-US` if the config isn't set)
*/
export function formatCurrency(
amount: number,
_symbol?: string,
_countryCode?: string,
): string {
const cfg = (config as DeepWritable<Configschema>).event.currency;
const symbol = _symbol || cfg?.symbol || '$';
const countryCode = _countryCode || cfg?.countryCode || 'en-US';
if (amount >= 100) {
return `${symbol}${Math.floor(amount).toLocaleString(
countryCode,
{
maximumFractionDigits: 0,
},
)}`;
}
return `${symbol}${amount.toFixed(2)}`;
}
/** /**
* Simple formatter for displaying USD amounts. * Simple formatter for displaying USD amounts.
* @param amount Amount as a integer/float. * @param amount Amount as a integer/float.
*/ */
export function formatUSD(amount: number): string { export function formatUSD(amount: number): string {
if (amount >= 100) { return formatCurrency(amount);
return `$${Math.floor(amount).toLocaleString('en-US', { maximumFractionDigits: 0 })}`;
} // We would use this as the fallback, but we have overrides in the config now
return `$${amount.toFixed(2)}`; // return formatCurrency(amount, '$', 'en-US');
} }
/** /**

View File

@ -13,7 +13,9 @@
Prize Available: {{ prize.name }} Prize Available: {{ prize.name }}
</div> </div>
<div :style="{ 'font-size': '20px' }"> <div :style="{ 'font-size': '20px' }">
Provided by {{ prize.provided }}, minimum donation amount: ${{ prize.minimumBid.toFixed(2) }} Provided by {{ prize.provided }}, minimum donation amount: {{ symbol }}{{
prize.minimumBid.toFixed(2)
}}
(donate in the next {{ timeUntilString }}) (donate in the next {{ timeUntilString }})
</div> </div>
</div> </div>
@ -59,6 +61,10 @@ export default class extends Vue {
: ''; : '';
} }
get symbol(): string {
return nodecg.bundleConfig.event.currency?.symbol || '$';
}
async created(): Promise<void> { async created(): Promise<void> {
await wait(this.seconds * 1000); // Wait the specified length. await wait(this.seconds * 1000); // Wait the specified length.
this.$emit('end'); this.$emit('end');

View File

@ -155,8 +155,13 @@ export default class extends Vue {
get totalStr(): string { get totalStr(): string {
// "Reset" value every 10k, specific to ESA Legends 2023. // "Reset" value every 10k, specific to ESA Legends 2023.
const symbol = nodecg.bundleConfig.event.currency?.symbol || '$';
const countryCode = nodecg.bundleConfig.event.currency?.countryCode || 'en-US';
const esal23 = nodecg.bundleConfig.event.shorts === 'esal23'; const esal23 = nodecg.bundleConfig.event.shorts === 'esal23';
return `kr ${Math.floor(esal23 ? this.total % 10000 : this.total).toLocaleString('de-DE', {
return `${symbol}${
Math.floor(esal23 ? this.total % 10000 : this.total).toLocaleString(countryCode, {
maximumFractionDigits: 0, maximumFractionDigits: 0,
minimumIntegerDigits: esal23 && this.total >= 10000 ? 4 : undefined, minimumIntegerDigits: esal23 && this.total >= 10000 ? 4 : undefined,
})}`; })}`;