-
-
Save openam/3c46586001bef21f1e0f to your computer and use it in GitHub Desktop.
AngularJS byte format filter
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
| app.filter( 'bytes', function bytesFilter () { | |
| 'use strict'; | |
| return function bytesReturn ( bytes, precision ) { | |
| if ( bytes === 0 ) { | |
| return '0 bytes'; | |
| } | |
| if ( isNaN( parseFloat( bytes ) ) || !isFinite( bytes ) ) { | |
| return '-'; | |
| } | |
| if ( typeof precision === 'undefined') { | |
| 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