bytes.umd.js
1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.numerifyBytes = factory());
}(this, (function () { 'use strict';
var DECIMAL = {
base: 1000,
suffixes: ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
};
var BINARY = {
base: 1024,
suffixes: ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
};
var bytes = {
regexp: /[0\s]i?b/,
format: function format(value, formatType, roundingFunction, numerify) {
var output = void 0;
var bytes = ~formatType.indexOf('ib') ? BINARY : DECIMAL;
var suffix = ~formatType.indexOf(' b') || ~formatType.indexOf(' ib') ? ' ' : '';
formatType = formatType.replace(/\s?i?b/, '');
for (var power = 0; power <= bytes.suffixes.length; power++) {
var min = Math.pow(bytes.base, power);
var max = Math.pow(bytes.base, power + 1);
if (value === null || value === 0 || value >= min && value < max) {
suffix += bytes.suffixes[power];
if (min > 0) value = value / min;
break;
}
}
output = numerify._numberToFormat(value, formatType, roundingFunction);
return output + suffix;
}
};
return bytes;
})));