Teren-app/resources/js/Pages/Imports/useCurrencyFormat.js
Simon Pocrnjič fe91c7e4bc Mass changes
2025-10-04 23:36:18 +02:00

69 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Generic currency formatting composable with fallback chain.
// Usage:
// const { formatMoney, currentCurrency } = useCurrencyFormat({
// primary: clientCurrency, // e.g. 'EUR'
// fallbacks: ['EUR'],
// locale: 'sl-SI'
// })
// formatMoney(123.45) -> '123,45 €'
import { ref, computed } from 'vue'
export function useCurrencyFormat(options = {}) {
const {
primary = 'EUR',
fallbacks = ['EUR'],
locale = 'sl-SI',
minimumFractionDigits = 2,
maximumFractionDigits = 2,
} = options
const primaryCurrency = ref(primary)
const fallbackList = ref(Array.isArray(fallbacks) && fallbacks.length ? fallbacks : ['EUR'])
const currencyChain = computed(() => [primaryCurrency.value, ...fallbackList.value].filter(Boolean))
const formatterByCode = new Map()
function getFormatter(code) {
if (!code) return null
if (!formatterByCode.has(code)) {
try {
formatterByCode.set(
code,
new Intl.NumberFormat(locale, {
style: 'currency',
currency: code,
minimumFractionDigits,
maximumFractionDigits,
})
)
} catch (e) {
// invalid currency code skip
formatterByCode.set(code, null)
}
}
return formatterByCode.get(code)
}
const activeCurrency = computed(() => {
for (const c of currencyChain.value) {
if (getFormatter(c)) return c
}
return 'EUR'
})
function formatMoney(val, overrideCurrency) {
if (val === null || val === undefined || val === '' || isNaN(val)) return '—'
const code = overrideCurrency || activeCurrency.value
const fmt = getFormatter(code) || getFormatter('EUR')
return fmt ? fmt.format(Number(val)) : Number(val).toFixed(2) + ' ' + code
}
return {
formatMoney,
activeCurrency,
primaryCurrency,
setCurrency(code) { primaryCurrency.value = code },
}
}