This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
/
storage_helper.js
65 lines (59 loc) · 1.65 KB
/
storage_helper.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
define(function(require) {
'use strict';
/**
* Helper class for getting available/used storage
* required by *_storage.js
*/
var StorageHelper = {
/**
* formatting file size strings.
*
* @private
* @param {Number} bytes size in Bytes
* @param {Number} digits fixed float digits
* @returns {Object}
*/
_getReadableFileSize: function getReadableFileSize(bytes, digits) {
if (bytes === undefined) {
return {};
}
var units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
var size, e;
if (bytes) {
e = Math.floor(Math.log(bytes) / Math.log(1024));
size = (bytes / Math.pow(1024, e)).toFixed(digits || 0);
} else {
e = 0;
size = '0';
}
return {
size: size,
unit: units[e]
};
},
/**
* attach formated size to certain element.
*
* @public
* @param {Object} element html element
* @param {String} l10nId l10n string id
* @param {Number} sizesize in Bytes
*/
showFormatedSize: function showFormatedSize(element, l10nId, size) {
if (size === undefined || isNaN(size)) {
element.textContent = '';
return;
}
// KB - 3 KB (nearest ones), MB, GB - 1.29 MB (nearest hundredth)
var fixedDigits = (size < 1024 * 1024) ? 0 : 2;
var sizeInfo = this._getReadableFileSize(size, fixedDigits);
document.l10n.formatValue('byteUnit-' + sizeInfo.unit).then(unit => {
document.l10n.setAttributes(element, l10nId, {
size: sizeInfo.size,
unit: unit
});
});
}
};
return StorageHelper;
});