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:
@@ -1,26 +0,0 @@
|
||||
const formatters = new WeakMap<object, Intl.NumberFormat>()
|
||||
|
||||
export function useCompactNumber(truncate = false, fractionDigits = 2, locale?: string) {
|
||||
const context = {}
|
||||
|
||||
let formatter = formatters.get(context)
|
||||
|
||||
if (!formatter) {
|
||||
formatter = new Intl.NumberFormat(locale, {
|
||||
notation: 'compact',
|
||||
maximumFractionDigits: fractionDigits,
|
||||
})
|
||||
formatters.set(context, formatter)
|
||||
}
|
||||
|
||||
function format(value: number): string {
|
||||
let formattedValue = value
|
||||
if (truncate) {
|
||||
const scale = Math.pow(10, fractionDigits)
|
||||
formattedValue = Math.floor(value * scale) / scale
|
||||
}
|
||||
return formatter!.format(formattedValue)
|
||||
}
|
||||
|
||||
return format
|
||||
}
|
||||
Reference in New Issue
Block a user