diff options
| author | Arne Schauf <git@asw.io> | 2018-03-03 12:17:42 +0100 |
|---|---|---|
| committer | Arne Schauf <git@asw.io> | 2018-03-03 12:17:42 +0100 |
| commit | e9743b14c2fb597487836e8446a3d14281a45eba (patch) | |
| tree | 1311483dfb6c2aff43108619f472b77454ab4c16 /src/plugins/prettyBytes.js | |
| parent | d70b8f09ce1fa97cb7c4e2be66775e5d3d7e82cd (diff) | |
| download | parry-ui-e9743b14c2fb597487836e8446a3d14281a45eba.tar.gz parry-ui-e9743b14c2fb597487836e8446a3d14281a45eba.zip | |
fix stuff that broke on upgrade
Diffstat (limited to 'src/plugins/prettyBytes.js')
| -rw-r--r-- | src/plugins/prettyBytes.js | 25 |
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 + }) +} |
