54 lines
1.2 KiB
TypeScript
54 lines
1.2 KiB
TypeScript
import ICurrency from '@/models/ICurrency'
|
|
|
|
export const money = (
|
|
value: number,
|
|
currency: ICurrency | null | undefined,
|
|
country: string = 'fr-FR'
|
|
): string => {
|
|
if (!currency) {
|
|
return value.toString()
|
|
}
|
|
|
|
return new Intl.NumberFormat(country, {
|
|
style: 'currency',
|
|
currency: currency.code,
|
|
minimumFractionDigits: 2
|
|
}).format(value)
|
|
}
|
|
|
|
export const moneypad = (
|
|
value: number,
|
|
currency?: ICurrency | null,
|
|
withPadEnd: boolean = true
|
|
): string => {
|
|
const m = money(value, currency)
|
|
const s: string[] = m.split('\xa0')
|
|
const cur: string | undefined = s.pop()
|
|
|
|
const result = `${s.join('\xa0')}\xa0${cur || ''}`
|
|
if (withPadEnd) {
|
|
return result.padEnd(3, '\xa0')
|
|
}
|
|
return result
|
|
}
|
|
|
|
export const date = (value: string, country: string = 'fr-FR') => {
|
|
return new Intl.DateTimeFormat(country).format(new Date(value))
|
|
}
|
|
|
|
export const fulldate = (value: string, country: string = 'fr-FR') => {
|
|
return new Intl.DateTimeFormat(country, {
|
|
weekday: 'long',
|
|
year: 'numeric',
|
|
month: 'long',
|
|
day: 'numeric'
|
|
}).format(new Date(value))
|
|
}
|
|
|
|
export default {
|
|
money,
|
|
moneypad,
|
|
date,
|
|
fulldate
|
|
} as { [key: string]: (...args: any[]) => any }
|