factored out progress into progress.js

This commit is contained in:
Danny Coates 2017-08-05 18:06:43 -07:00
parent 5ac013ca40
commit c91d24cd86
No known key found for this signature in database
GPG key ID: 4C442633C62E00CB
12 changed files with 229 additions and 240 deletions

View file

@ -104,9 +104,29 @@ function copyToClipboard(str) {
return result;
}
const LOCALIZE_NUMBERS = !!(
typeof Intl === 'object' &&
Intl &&
typeof Intl.NumberFormat === 'function'
);
const UNITS = ['B', 'kB', 'MB', 'GB'];
function bytes(num) {
const exponent = Math.min(Math.floor(Math.log10(num) / 3), UNITS.length - 1);
const n = Number(num / Math.pow(1000, exponent));
const nStr = LOCALIZE_NUMBERS
? n.toLocaleString(navigator.languages, {
minimumFractionDigits: 1,
maximumFractionDigits: 1
})
: n.toFixed(1);
return `${nStr}${UNITS[exponent]}`;
}
const ONE_DAY_IN_MS = 86400000;
module.exports = {
bytes,
copyToClipboard,
arrayToHex,
hexToArray,