-
Notifications
You must be signed in to change notification settings - Fork 52
/
util.js
57 lines (49 loc) · 1.3 KB
/
util.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
const sizeInMbytes = (obj) => {
let bytes = 0;
const sizeOf = (obj) => {
let objClass;
if (obj !== null && obj !== undefined) {
switch (typeof obj) {
case 'number':
bytes += 8;
break;
case 'string':
bytes += obj.length * 2;
break;
case 'boolean':
bytes += 4;
break;
case 'object':
objClass = Object.prototype.toString.call(obj).slice(8, -1);
if (objClass === 'Object' || objClass === 'Array') {
for (const key in obj) {
if (!obj.hasOwnProperty(key)) continue;
sizeOf(obj[key]);
}
} else bytes += obj.toString().length * 2;
break;
}
}
return bytes;
};
return sizeOf(obj) / 1024;
};
const guidGenerator = () => {
const s4 = () => (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
return (s4() + s4() + s4() + s4() + s4() + s4() + s4() + s4());
};
const blobToPlain = (blob) => {
let uri = URL.createObjectURL(blob);
let xhr = new XMLHttpRequest();
xhr.open('GET', uri, false);
xhr.send();
URL.revokeObjectURL(uri);
return blob.type === 'application/json'
? JSON.parse(xhr.response)
: xhr.response;
}
module.exports = {
sizeInMbytes,
guidGenerator,
blobToPlain
};