-
-
Save handleman/e6fbb53a2e236d22f0ee 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