Impove Intl formatting (#5372)
* Improve Intl formatting * Additional fixes * Fixed formatters were not updated on locale change * Fixed formatNumber was not updated on locale change * Additional formatting and fixes after merge * Run prepr:frontend * Remove `'` in icon map * Run `pnpm install` * fix: lint + import * Additional fixes --------- Co-authored-by: Calum H. <calum@modrinth.com> Co-authored-by: Calum H. (IMB11) <contact@cal.engineer>
This commit is contained in:
@@ -60,29 +60,6 @@ export const getCurrency = (userCountry) => {
|
||||
return countryCurrency[userCountry] ?? 'USD'
|
||||
}
|
||||
|
||||
export const formatPrice = (locale, price, currency, trimZeros = false) => {
|
||||
let formatter = new Intl.NumberFormat(locale, {
|
||||
style: 'currency',
|
||||
currency,
|
||||
})
|
||||
|
||||
const maxDigits = formatter.resolvedOptions().maximumFractionDigits
|
||||
const convertedPrice = price / Math.pow(10, maxDigits)
|
||||
|
||||
let minimumFractionDigits = maxDigits
|
||||
|
||||
if (trimZeros && Number.isInteger(convertedPrice)) {
|
||||
minimumFractionDigits = 0
|
||||
}
|
||||
|
||||
formatter = new Intl.NumberFormat(locale, {
|
||||
style: 'currency',
|
||||
currency,
|
||||
minimumFractionDigits,
|
||||
})
|
||||
return formatter.format(convertedPrice)
|
||||
}
|
||||
|
||||
export const calculateSavings = (monthlyPlan, plan, months = 12) => {
|
||||
const monthlyAnnualized = monthlyPlan * months
|
||||
|
||||
|
||||
@@ -93,40 +93,6 @@ export const sortedCategories = (tags, formatCategoryName, locale) => {
|
||||
})
|
||||
}
|
||||
|
||||
export const formatNumber = (number, abbreviate = true) => {
|
||||
const x = Number(number)
|
||||
if (x >= 1000000 && abbreviate) {
|
||||
return `${(x / 1000000).toFixed(2).toString()}M`
|
||||
} else if (x >= 10000 && abbreviate) {
|
||||
return `${(x / 1000).toFixed(1).toString()}k`
|
||||
}
|
||||
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
|
||||
}
|
||||
|
||||
export function formatDate(
|
||||
date: dayjs.Dayjs,
|
||||
options: Intl.DateTimeFormatOptions = {
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
year: 'numeric',
|
||||
},
|
||||
): string {
|
||||
return date.toDate().toLocaleDateString(undefined, options)
|
||||
}
|
||||
|
||||
export function formatMoney(number, abbreviate = false) {
|
||||
const x = Number(number)
|
||||
if (x >= 1000000 && abbreviate) {
|
||||
return `$${(x / 1000000).toFixed(2).toString()}M`
|
||||
} else if (x >= 10000 && abbreviate) {
|
||||
return `$${(x / 1000).toFixed(2).toString()}k`
|
||||
}
|
||||
return `$${x
|
||||
.toFixed(2)
|
||||
.toString()
|
||||
.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}`
|
||||
}
|
||||
|
||||
export const formatBytes = (bytes, decimals = 2) => {
|
||||
if (bytes === 0) return '0 Bytes'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user