All files / src utils.js

90% Statements 9/10
60% Branches 3/5
100% Functions 2/2
100% Lines 9/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25                2x 2x 2x 2x     2x         19x 19x 19x 19x    
// @flow
import LANGUAGE_CODES from './json/language_codes.json';
 
type Currency = {
  format: string,
  subunit: number,
};
 
export const getCurrencyFromBrowserLocale = (): string => {
  Iif (typeof window === 'undefined') return 'USD';
  const lang = window.navigator.language.toLowerCase();
  return LANGUAGE_CODES[lang] || 'USD';
};
 
export const formatAmountForCurrency = (
  amount: number,
  currency: Currency,
  rounding?: number => number = Math.round
): string => {
  const multiplier = Math.pow(10, currency.subunit);
  const price = rounding(amount * multiplier) / multiplier;
  const readablePrice = price.toLocaleString().replace(/(\.[\d]{1})$/, '$10');
  return currency.format.replace('{amount}', readablePrice);
};