-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (94 loc) · 3.2 KB
/
index.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const FORMAT_SI = 1024;
const FORMAT_IEC = 1000;
const UNITS_SI = ['B','kB','MB','GB','TB','PB','EB','ZB','YB'];
const UNITS_IEC = ['b','KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
const REGEX_SI = "([0-9\.]+)\s*([a-zA-Z]{1,2})";
const REGEX_IEC = "([0-9\.]+)\s*([a-zA-Z]{1,3})";
let DECIMALS = 2;
let DEFAULT_FORMAT = FORMAT_SI;
/**
* Convert machine-readable sizes (integer) to human-readable sizes (string)
*
* Examples:
* 3000000 to '2.86 MB' (using FORMAT.SI)
* 3000000 to '3 MiB' (using FORMAT.IEC)
*
* @param {number} size The actual size in bytes
* @param {?number} format The format in which to make the transformation (IEC/SI)
* @return {string}
*/
export function humanReadableSize(size, format= undefined){
if(typeof size !== 'number'){
throw new TypeError("Invalid `size` parameter");
}
if(typeof format !== 'number' || [FORMAT_SI, FORMAT_IEC].indexOf(format) === -1){
format = DEFAULT_FORMAT;
}
let units = (format === FORMAT_IEC) ? UNITS_IEC : UNITS_SI;
let unitSize = format;
if(Math.abs(size) < format) {
return size.toFixed(DECIMALS) + ' ' + units[0];
}
let divisions = 0;
do {
size /= unitSize;
++divisions;
} while(Math.abs(size) >= unitSize && divisions < units.length);
return size.toFixed(DECIMALS) + ' ' + units[divisions];
}
/**
* Convert human-readable sizes (string) to machine-readable sizes (integer)
*
* Examples:
* '2.86MB' to 3000000 to (using FORMAT.SI)
* '3MiB' to 3000000 (using FORMAT.IEC)
*
* @param {string} size The human-readable size
* @param {?number} format The format in which to make the transformation (IEC/SI)
* @return {number}
*/
export function machineReadableSize(size, format = undefined){
if(typeof size !== 'string'){
throw new TypeError("Invalid `size` parameter");
}
if(typeof format !== 'number' || [FORMAT_SI, FORMAT_IEC].indexOf(format) === -1){
format = DEFAULT_FORMAT;
}
let unitSize = format, units, regex;
if(format === FORMAT_IEC){
regex = REGEX_IEC;
units = UNITS_IEC;
} else {
regex = REGEX_SI;
units = UNITS_SI;
}
let matches = size.match(regex);
if(!matches) {
throw new TypeError("Invalid `size` parameter");
}
let foundSize = matches[1];
let unit = matches[2];
let unitIndex = units.indexOf(unit);
if(unitIndex === -1){
throw new TypeError("Invalid `size` parameter");
}
let multiplier = Math.pow(unitSize, unitIndex);
let computedSize = parseFloat(foundSize) * multiplier;
return Number.isSafeInteger(computedSize) ? parseInt(computedSize) : computedSize;
}
/**
* Sets the default format for the converting functions (IEC/SI)
*
* @param {number} format
*/
export function setDefaultSizeFormat(format){
if(typeof format !== 'number' || [FORMAT_SI, FORMAT_IEC].indexOf(format) === -1){
throw new TypeError("Invalid `format` parameter");
}
DEFAULT_FORMAT = format;
}
export const SIZE_FORMAT = {
"SI" : FORMAT_SI,
"JEDEC" : FORMAT_SI,
"IEC" : FORMAT_IEC
}