use constant

This commit is contained in:
sinsong 2023-06-30 23:21:54 +08:00
parent a67b4c3a9e
commit 403d260b91

View file

@ -1,14 +1,16 @@
const DATA_UNIT_FACTOR = 1024
export function calculateSize(size) { export function calculateSize(size) {
function wellSized(num) { function wellSized(num) {
return num > 1 && num < 1024 return num > 1 && num < DATA_UNIT_FACTOR
} }
if (size === undefined) { return } if (size === undefined) { return }
if (wellSized(size)) { return `${size.toFixed(1)} B` } if (wellSized(size)) { return `${size.toFixed(1)} B` }
size /= 1024 size /= DATA_UNIT_FACTOR
if (wellSized(size)) { return `${size.toFixed(1)} KB` } if (wellSized(size)) { return `${size.toFixed(1)} KB` }
size /= 1024 size /= DATA_UNIT_FACTOR
if (wellSized(size)) { return `${size.toFixed(1)} MB` } if (wellSized(size)) { return `${size.toFixed(1)} MB` }
size /= 1024 size /= DATA_UNIT_FACTOR
if (wellSized(size)) { return `${size.toFixed(1)} GB` } if (wellSized(size)) { return `${size.toFixed(1)} GB` }
console.error('size too big') console.error('size too big')
} }