Created
May 26, 2013 18:22
-
-
Save yrezgui/5653591 to your computer and use it in GitHub Desktop.
This is a custom filter to show a bytes filesize in better way.
This file contains 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
// add the filter to your application module | |
angular.module('myApp', ['filters']); | |
/** | |
* Filesize Filter | |
* @Param length, default is 0 | |
* @return string | |
*/ | |
angular.module('filters', []) | |
.filter('Filesize', function () { | |
return function (size) { | |
if (isNaN(size)) | |
size = 0; | |
if (size < 1024) | |
return size + ' Bytes'; | |
size /= 1024; | |
if (size < 1024) | |
return size.toFixed(2) + ' Kb'; | |
size /= 1024; | |
if (size < 1024) | |
return size.toFixed(2) + ' Mb'; | |
size /= 1024; | |
if (size < 1024) | |
return size.toFixed(2) + ' Gb'; | |
size /= 1024; | |
return size.toFixed(2) + ' Tb'; | |
}; | |
}); | |
/** | |
* Usage | |
* var myFile = 5678; | |
* | |
* {{myText|filesize}} | |
* | |
* Output | |
* "5.54 Kb" | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is still in the first Google search results page, so, please don't use a loop for this. Have a look at https://gist.github.com/thomseddon/3511330#gistcomment-1703533 instead.