All files / src/search-js currencies.ts

100% Statements 26/26
86.36% Branches 19/22
100% Functions 4/4
100% Lines 24/24

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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88  8x   8x                             8x                               8x 6x       6x 4x 4x               30x 30x 30x   30x   12x 12x           12x   46x 39x 29x       12x 6x   6x       18x       18x     6x      
import { CurrencySettingsDTO } from "@nosto/nosto-js/client"
import { nostojs } from "@nosto/nosto-js"
 
const defaultConfig = {
  defaultCurrency: "EUR",
  defaultLocale: "en-US",
  /** @hidden  */
  currencySettings: {}
} satisfies CurrencyConfig
 
type CurrencyFormats = Record<string, CurrencySettingsDTO>
 
export interface CurrencyConfig {
  defaultCurrency: string
  defaultLocale: string
  currencySettings: CurrencyFormats
}
 
const currencyLocales: Record<string, string> = {
  EUR: "de-DE",
  GBP: "en-GB",
  USD: "en-US",
  AUD: "en-AU",
  CAD: "en-CA",
  //India, Afghanistan, Bangladesh, Bhutan, Myanmar, Nepal, and Pakistan uses lakhs and crores notation
  INR: "en-IN",
  AFN: "en-IN",
  BDT: "en-IN",
  BTN: "en-IN",
  MMK: "en-IN",
  NPR: "en-IN",
  PKR: "en-IN"
}
 
export function getCurrencyFormatting(overrides: Partial<CurrencyConfig> = {}) {
  const config: CurrencyConfig = {
    ...defaultConfig,
    ...overrides
  }
  if (!overrides.currencySettings) {
    nostojs(api => {
      config.currencySettings = api.internal.getSettings().currencySettings
    })
  }
 
  /**
   * Format the given monetary value using the Nosto currency settings.
   */
  function formatCurrency(value: number, currency?: string) {
    const { defaultCurrency, currencySettings, defaultLocale } = config
    const currencyCode = currency ?? defaultCurrency
    const locale = currencyLocales[currencyCode] ?? defaultLocale
 
    if (currencyCode in currencySettings) {
      // formatting using Nosto settings
      const settings = currencySettings[currencyCode]!
      const result = new Intl.NumberFormat(locale, {
        useGrouping: !!settings.groupingSeparator,
        minimumFractionDigits: settings.decimalPlaces,
        maximumFractionDigits: settings.decimalPlaces
      }).formatToParts(value)
 
      const normalised = result
        .map(it => {
          if (it.type === "group") return settings.groupingSeparator
          if (it.type === "decimal") return settings.decimalCharacter
          return it.value
        })
        .join("")
 
      if (settings?.currencyBeforeAmount) {
        return `${settings.currencyToken}${normalised}`
      }
      return `${normalised}${settings?.currencyToken}`
    }
 
    // fallback logic
    const numberFormat = new Intl.NumberFormat(locale, {
      style: "currency",
      currency: currencyCode
    })
    return numberFormat.format(value)
  }
 
  return {
    formatCurrency
  }
}