wrap number localization in try/catch
This commit is contained in:
parent
e9a49e23e8
commit
deabca5a94
1 changed files with 20 additions and 7 deletions
27
app/utils.js
27
app/utils.js
|
@ -105,19 +105,32 @@ function bytes(num) {
|
||||||
}
|
}
|
||||||
const exponent = Math.min(Math.floor(Math.log10(num) / 3), UNITS.length - 1);
|
const exponent = Math.min(Math.floor(Math.log10(num) / 3), UNITS.length - 1);
|
||||||
const n = Number(num / Math.pow(1000, exponent));
|
const n = Number(num / Math.pow(1000, exponent));
|
||||||
const nStr = LOCALIZE_NUMBERS
|
let nStr = n.toFixed(1);
|
||||||
? n.toLocaleString(navigator.languages, {
|
if (LOCALIZE_NUMBERS) {
|
||||||
|
try {
|
||||||
|
nStr = n.toLocaleString(navigator.languages.map(l => l.split(';')[0]), {
|
||||||
minimumFractionDigits: 1,
|
minimumFractionDigits: 1,
|
||||||
maximumFractionDigits: 1
|
maximumFractionDigits: 1
|
||||||
})
|
});
|
||||||
: n.toFixed(1);
|
} catch (e) {
|
||||||
|
// fall through
|
||||||
|
}
|
||||||
|
}
|
||||||
return `${nStr}${UNITS[exponent]}`;
|
return `${nStr}${UNITS[exponent]}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function percent(ratio) {
|
function percent(ratio) {
|
||||||
return LOCALIZE_NUMBERS
|
if (LOCALIZE_NUMBERS) {
|
||||||
? ratio.toLocaleString(navigator.languages, { style: 'percent' })
|
try {
|
||||||
: `${Math.floor(ratio * 100)}%`;
|
return ratio.toLocaleString(
|
||||||
|
navigator.languages.map(l => l.split(';')[0]),
|
||||||
|
{ style: 'percent' }
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
// fall through
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return `${Math.floor(ratio * 100)}%`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function allowedCopy() {
|
function allowedCopy() {
|
||||||
|
|
Loading…
Reference in a new issue