Skip to content

Instantly share code, notes, and snippets.

@lyuehh
Created September 11, 2014 02:01
Show Gist options
  • Save lyuehh/74c382632906cb8ef682 to your computer and use it in GitHub Desktop.
Save lyuehh/74c382632906cb8ef682 to your computer and use it in GitHub Desktop.
getParameterByName, readablizeBytes, formatSize
function getParameterByName(name, search) {
var match = RegExp('[?&]' + name + '=([^&]*)')
.exec(search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
function readablizeBytes(size) {
if (size <= 100000){
return '' + size;
}
var SizePrefixes = ' KMGTPEZYXWVU';
if(size <= 0) return '0';
var t2 = Math.min(Math.round(Math.log(size)/Math.log(1000)), 12);
return (Math.round(size * 100 / Math.pow(1000, t2)) / 100) +
SizePrefixes.charAt(t2).replace(' ', '') + '';
}
function formatSize(size1, standard) {
var size = parseFloat(size1);
if (standard) {
standard = standard.toLowerCase();
}
if(size<=1){
return size.toFixed(3);
}
var n = 0,
base = standard == 'si' ? 1000 : 1024,
prefixes = ' KMGTPEZY';
if (size >= base) {
n = Math.floor( Math.log(size) / Math.log(base) );
if (n >= prefixes.length) {
return 'N/A';
}
size = ( size / Math.pow(base, n) ).toFixed(3) * 1 + '';
}else{
size = size.toFixed(3)
}
return size + prefixes[n] + ( n && standard == 'iec' ? 'i' : '' ) + '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment