All files / src/util price.tsx

100% Statements 8/8
75% Branches 3/4
100% Functions 3/3
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23  5x   5x           5x 5x 5x   1x         5x        
function getCurrencySymbol(currencyCode: string): string | null {
  try {
    // Create a new formatter with the given currency code
    const formatter = new Intl.NumberFormat("en", {
      style: "currency",
      currency: currencyCode,
    });
 
    // Format a dummy number (123) and extract the currency symbol
    const formatted = formatter.formatToParts(123);
    const currencyPart = formatted.find((part) => part.type === "currency");
    return currencyPart ? currencyPart.value : null;
  } catch (error) {
    return null; // Return null if the currency code is invalid or unsupported
  }
}
 
export function getPrice(currencyCode: string, price: number): string {
  return `${
    getCurrencySymbol(currencyCode) || currencyCode
  }${price.toLocaleString()}`;
}