blob: ebfb72d01e9b4e7ca1c6d8e19c5a085c5c0bdc90 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
export default ({ Vue }) => {
Vue.filter('prettyBytes', function (value) {
const UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
if (!Number.isFinite(value)) {
throw new TypeError(`Expected a finite number, got ${typeof value}: ${value}`)
}
const neg = value < 0
if (neg) {
value = -value
}
if (value < 1) {
return (neg ? '-' : '') + value + ' B'
}
const exponent = Math.min(Math.floor(Math.log10(value) / 3), UNITS.length - 1)
const numStr = Number((value / Math.pow(1000, exponent)).toPrecision(3))
const unit = UNITS[exponent]
return (neg ? '-' : '') + numStr + ' ' + unit
})
}
|