From 943d8cdfe682bf2cb77f0c9abd7a6ba4a72f182d Mon Sep 17 00:00:00 2001 From: Lordmau5 Date: Thu, 14 Mar 2024 17:12:59 +0100 Subject: [PATCH] Add `formatCurrency` method to shared package as well --- shared/browser_shared/helpers.ts | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/shared/browser_shared/helpers.ts b/shared/browser_shared/helpers.ts index fd71ad9..8f3a206 100644 --- a/shared/browser_shared/helpers.ts +++ b/shared/browser_shared/helpers.ts @@ -1,3 +1,8 @@ +import { Configschema } from '@esa-layouts/types/schemas'; +import { DeepWritable } from 'ts-essentials'; + +const config = nodecg.bundleConfig; + /** * Checks if number needs a 0 adding to the start and does so if needed. * @param num Number which you want to turn into a padded string. @@ -15,8 +20,23 @@ export function msToTimeStr(ms: number): string { const minutes = Math.floor((ms / (1000 * 60)) % 60); const hours = Math.floor(ms / (1000 * 60 * 60)); return `${padTimeNumber(hours) - }:${padTimeNumber(minutes) - }:${padTimeNumber(seconds)}`; + }:${padTimeNumber(minutes) + }:${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) + */ +export function formatCurrency( + amount: number, + _symbol?: string, +): string { + const cfg = (config as DeepWritable).event.currency; + const symbol = _symbol || cfg.symbol; + + return `${symbol}${amount.toFixed(2)}`; } /** @@ -24,5 +44,5 @@ export function msToTimeStr(ms: number): string { * @param amount Amount as a integer/float. */ export function formatUSD(amount: number): string { - return `$${amount.toFixed(2)}`; + return formatCurrency(amount); }