Skip to content

Instantly share code, notes, and snippets.

@marcoberri
Created February 4, 2016 11:16
Show Gist options
  • Save marcoberri/a45b3287f30b6c15a3b2 to your computer and use it in GitHub Desktop.
Save marcoberri/a45b3287f30b6c15a3b2 to your computer and use it in GitHub Desktop.
Script Utils for Mongo DB
function bytesToSize(bytes, precision) {
var kilobyte = 1024;
var megabyte = kilobyte * 1024;
var gigabyte = megabyte * 1024;
var terabyte = gigabyte * 1024;
if ((bytes >= 0) && (bytes < kilobyte)) {
return bytes + ' B';
} else if ((bytes >= kilobyte) && (bytes < megabyte)) {
return (bytes / kilobyte).toFixed(precision) + ' KB';
} else if ((bytes >= megabyte) && (bytes < gigabyte)) {
return (bytes / megabyte).toFixed(precision) + ' MB';
} else if ((bytes >= gigabyte) && (bytes < terabyte)) {
return (bytes / gigabyte).toFixed(precision) + ' GB';
} else if (bytes >= terabyte) {
return (bytes / terabyte).toFixed(precision) + ' TB';
} else {
return bytes + ' B';
}
};
function print_debug(pretext, text){
if(text === 'object' || typeof text === 'object' || text === '[object Object]')
print(pretext + "==>" + JSON.stringify(text));
else
print(pretext + "==>" + text);
};
function convertDate(d) {
function pad(s) {
return (s < 10) ? '0' + s : s;
}
return [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('/');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment