Built motion from commit 5b01f56.|0.0.106
[motion.git] / public / bower_components / pretty-bytes / pretty-bytes.js
1 /*!
2         pretty-bytes
3         Convert bytes to a human readable string: 1337 → 1.34 kB
4         https://github.com/sindresorhus/pretty-bytes
5         by Sindre Sorhus
6         MIT License
7 */
8 (function () {
9         'use strict';
10
11         // Number.isNaN() polyfill
12         var isNaN = function (val) {
13                 return val !== val;
14         };
15
16         var prettyBytes = function (num) {
17                 if (typeof num !== 'number' || isNaN(num)) {
18                         throw new TypeError('Expected a number');
19                 }
20
21                 var exponent;
22                 var unit;
23                 var neg = num < 0;
24                 var units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
25
26                 if (neg) {
27                         num = -num;
28                 }
29
30                 if (num < 1) {
31                         return (neg ? '-' : '') + num + ' B';
32                 }
33
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];
37
38                 return (neg ? '-' : '') + num + ' ' + unit;
39         };
40
41         if (typeof module !== 'undefined' && module.exports) {
42                 module.exports = prettyBytes;
43         } else {
44                 self.prettyBytes = prettyBytes;
45         }
46 })();