aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/prettyBytes.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/prettyBytes.js')
-rw-r--r--src/plugins/prettyBytes.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/plugins/prettyBytes.js b/src/plugins/prettyBytes.js
new file mode 100644
index 0000000..ebfb72d
--- /dev/null
+++ b/src/plugins/prettyBytes.js
@@ -0,0 +1,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
+ })
+}