Visual improvements on classic template

This commit is contained in:
lowlighter
2021-03-21 18:54:39 +01:00
parent 08d43c2dbd
commit 18c7fa1138
3 changed files with 6 additions and 11 deletions

View File

@@ -28,14 +28,14 @@
}
/**Formatter */
export function format(n, {sign = false, unit = true} = {}) {
export function format(n, {sign = false, unit = true, fixed} = {}) {
if (unit) {
for (const {u, v} of [{u:"b", v:10**9}, {u:"m", v:10**6}, {u:"k", v:10**3}]) {
if (n/v >= 1)
return `${(sign)&&(n > 0) ? "+" : ""}${(n/v).toFixed(2).substr(0, 4).replace(/[.]0*$/, "")}${u}`
return `${(sign)&&(n > 0) ? "+" : ""}${(n/v).toFixed(fixed ?? 2).substr(0, 4).replace(/[.]0*$/, "")}${u}`
}
}
return `${(sign)&&(n > 0) ? "+" : ""}${n}`
return `${(sign)&&(n > 0) ? "+" : ""}${fixed ? n.toFixed(fixed) : n}`
}
/**Bytes formatter */