-
-
Save eddiemonge/6470132 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('filterHelper', []); | |
angular.module('filterHelper').filter('bytes', function() { | |
return function(bytes, precision) { | |
var parsedBytes = parseFloat(bytes); | |
if (isNaN(parsedBytes) || !isFinite(bytes)) { | |
return '--'; | |
} | |
if ( parsedBytes === 0 ) { | |
return '0'; | |
} | |
precision = precision || 1; | |
var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB']; | |
var number = Math.floor( Math.log(bytes) / Math.log(1024) ); | |
return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number]; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment