Skip to content

Instantly share code, notes, and snippets.

@subzey
Forked from 140bytes/LICENSE.txt
Created June 18, 2011 14:25
Show Gist options
  • Save subzey/1033134 to your computer and use it in GitHub Desktop.
Save subzey/1033134 to your computer and use it in GitHub Desktop.
unit display

unit display

Shows values with units and multipliers, i.e., 760 Mibytes or 12 km.

If the unit is binary, «i» is appended to multiplier as suggested by IEC and IEEE.

Parameters

  1. Number. A value, amount of something that need to be displayed for human;
  2. Non-negative number. Amount of decimal digits. Default: 0;
  3. String. Unit to be used. Default: "bytes";
  4. Boolean. Is this unit binary (false) or decial (true). Default: binary (false).
function(
a, /* value */
b, /* number of decimal digits */
c, /* unit */
d /* isDecimal */
){
with(Math) /* Lookup properties in Math object */
return
/* Get the multipler: log(n,m) = log(n,e) / log(m,e) */
/* I can hardly make it readable */
/* Divide the original value into power of.. */
(a / pow(
/* ..base that becomes 1024 if unit is binary and 1000 if decimal */
d = 1E3 + 24*!d,
/* `a` is used already, so we can assign to it. It is the multiplier */
/* log via log use the d calculated just before */
/* And then coerce to integer n^0 is like ~~n but has MUCH lower priority
so we can omit the parens */
a = log(a)/log(d) ^0
/* Then convert to fixed number using b as parameter */
)).toFixed(b) +
/* Just a typography: add the whitespace.*/
' ' +
/* Split string by regexp: use dash or nothing */
/* ['','k','G','T'] Hope, T it is enough for now */
/* then picking a letter depending on `a` */
'kMGT'.charAt(a-1) + // Thanks @maettig for getting rid of regexp here
/* Append "i" if unit is binary and multiplier is more than 0 */
/* In details:
We're already redefined d to be 1000 or 1024.
1000 & 8 makes 8
1024 & 8 makes 0
!a makes 0 or 1
So the possible values of d&8|!a are:
0, 1, 8, 9
then it is coerced to boolean
*/
(d & 8 | !a ? '' : 'i') +
/* Append a unit or "bytes" if it is undefined */
/* This function is primarily designed for bytes */
(c || 'bytes')
/* -- end of return -- */
/* -- end of with -- */
}
function(a,b,c,d){with(Math)return(a/pow(d=1E3+24*!d,a=log(a)/log(d)^0)).toFixed(b)+' '+'kMGT'.charAt(a-1)+(d&8|!a?'':'i')+(c||'bytes')}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 subzey <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "unitDisplay",
"description": "Displays value with unit and multipler",
"keywords": [
"units",
"multipliers"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>140 bytes,11.77 Mibytes,9 km</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(a,b,c,d){with(Math)return(a/pow(d=1E3+24*!d,a=log(a)/log(d)^0)).toFixed(b)+' '+'kMGT'.charAt(a-1)+(d&8|!a?'':'i')+(c||'bytes')}
document.getElementById( "ret" ).innerHTML = [myFunction(140), myFunction(12345678,2), myFunction(9001,null,'m',true)]
</script>
@subzey
Copy link
Author

subzey commented Nov 18, 2011

It is! Thanks, @maettig!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment