3 Convert bytes to a human readable string: 1337 → 1.34 kB
4 https://github.com/sindresorhus/pretty-bytes
11 // Number.isNaN() polyfill
12 var isNaN = function (val) {
16 var prettyBytes = function (num) {
17 if (typeof num !== 'number' || isNaN(num)) {
18 throw new TypeError('Expected a number');
24 var units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
31 return (neg ? '-' : '') + num + ' B';
34 exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), units.length - 1);
35 num = (num / Math.pow(1000, exponent)).toFixed(2) * 1;
36 unit = units[exponent];
38 return (neg ? '-' : '') + num + ' ' + unit;
41 if (typeof module !== 'undefined' && module.exports) {
42 module.exports = prettyBytes;
44 self.prettyBytes = prettyBytes;