Skip to content

Instantly share code, notes, and snippets.

@pratikmallya
Forked from thomseddon/gist:3511330
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pratikmallya/319ed573aabd21042a74 to your computer and use it in GitHub Desktop.
Save pratikmallya/319ed573aabd21042a74 to your computer and use it in GitHub Desktop.
CoffeeScript Bytes Formatter
app.filter 'displayBytes', () ->
return (bytes, precision) ->
if isNaN(parseFloat(bytes)) || !isFinite(bytes))
return '-'
if typeof precision == 'undefined'
precision = 1
units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB']
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