daily pastebin goal
43%
SHARE
TWEET

Untitled

a guest Nov 17th, 2018 75 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. var Vue = require('vue');
  2.  
  3. var UNITS = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
  4. var STEP = 1024;
  5.  
  6. function format(value, power) {
  7.     return (value / Math.pow(STEP, power)).toFixed(2) + UNITS[power];
  8. }
  9.  
  10. Vue.filter('smart-file-size', {
  11.     read: function (value) {
  12.         value = parseFloat(value, 10);
  13.         for (var i = 0; i < UNITS.length; i++) {
  14.             if (value < Math.pow(STEP, i)) {
  15.                 if (UNITS[i - 1]) {
  16.                     return format(value, i - 1);
  17.                 }
  18.                 return value + UNITS[i];
  19.             }
  20.         }
  21.         return format(value, i - 1);
  22.     },
  23.     write: function (value, oldValue) {
  24.         var exp = new RegExp('^(\\d+(?:\\.\\d+)?)(' + UNITS.join('|') +')$', 'i');
  25.         var ret = value.match(exp);
  26.         if (ret) {
  27.             var i = UNITS.indexOf(ret[2].toUpperCase());
  28.             if (i >= 0) {
  29.                 return parseFloat(ret[1], 10) * Math.pow(STEP, i);
  30.             }
  31.         }
  32.         return oldValue;
  33.     }
  34. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top