Permalink
Browse files

initial working versio

  • Loading branch information...
mahemoff committed Sep 12, 2011
0 parents commit d1d6fc503f0f725e23064762dfadf1521f4c3583
Showing with 147 additions and 0 deletions.
  1. +24 −0 README
  2. +31 −0 lib/approximate.js
  3. +4 −0 lib/index.js
  4. +4 −0 make.sh
  5. +21 −0 src/approximate.coffee
  6. +4 −0 src/preamble.js
  7. +29 −0 src/test.coffee
  8. +14 −0 web/index.html
  9. +16 −0 web/jquery.js
24 README
@@ -0,0 +1,24 @@
+Small library to format numbers approximately ("7.5K", "280M", etc.)
+
+Test specimens:
+7 ==> '7'
+800 ==> '800'
+1000 ==> '1K'
+1200 ==> '1.2K'
+87821 ==> '88K'
+412011 ==> '412K'
+999949 ==> '999K'
+999950 ==> '1M'
+1000000 ==> '1M'
+6123922 ==> '6.1M'
+27969374 ==> '28M'
+28001341 ==> '28M'
+28061341 ==> '28M'
+812192442 ==> '812M'
+999949999 ==> '999M'
+999950000 ==> '1B'
+1000000000 ==> '1B'
+
+Notes:
+- Supports negative values too
+- Won't format to sub-integer values for now (0.4 becomes "0", 0.8 becomes "1")
@@ -0,0 +1,31 @@
+/* PLEASE DON'T EDIT THIS FILE AS IT'S AUTOGENERATED.
+ * The source is index.coffee. Change that.
+ */
+
+(function() {
+ var approximate, format;
+ approximate = function(num) {
+ num = parseInt(num);
+ if (Math.abs(num) >= 999950000) {
+ return format(num, 1000000000, 'B');
+ } else if (Math.abs(num) >= 999950) {
+ return format(num, 1000000, 'M');
+ } else if (Math.abs(num) >= 1000) {
+ return format(num, 1000, 'K');
+ } else {
+ return num + '';
+ }
+ };
+ format = function(num, divisor, suffix) {
+ var decimals;
+ num = num / divisor;
+ decimals = Math.abs(num) < 10 ? 1 : 0;
+ num = num.toFixed(decimals).replace('1000', '999');
+ return num.replace(/\.0+$/, '') + suffix;
+ };
+ if ((typeof module !== "undefined" && module !== null) && module.exports) {
+ module.exports = approximate;
+ } else {
+ window.approximate = approximate;
+ }
+}).call(this);
@@ -0,0 +1,4 @@
+/* PLEASE DON'T EDIT THIS FILE AS IT'S AUTOGENERATED.
+ * The source is index.coffee. Change that.
+ */
+
@@ -0,0 +1,4 @@
+#!/bin/bash
+cd `dirname $0`
+cp -f src/preamble.js lib/approximate.js
+coffee -p src/approximate.coffee >> lib/approximate.js
@@ -0,0 +1,21 @@
+approximate = (num) ->
+ num = parseInt(num);
+ if Math.abs(num) >=999950000
+ format num, 1000000000, 'B'
+ else if Math.abs(num) >=999950
+ format num, 1000000, 'M'
+ else if Math.abs(num) >= 1000
+ format num, 1000, 'K'
+ else
+ num+''
+
+format = (num, divisor, suffix) ->
+ num = num/divisor
+ decimals = if Math.abs(num)<10 then 1 else 0
+ num = num.toFixed(decimals).replace('1000','999') # sloppy (due to > comparisons)
+ num.replace(/\.0+$/, '')+suffix
+
+if module? and module.exports
+ module.exports = approximate
+else
+ window.approximate = approximate
@@ -0,0 +1,4 @@
+/* PLEASE DON'T EDIT THIS FILE AS IT'S AUTOGENERATED.
+ * The source is index.coffee. Change that.
+ */
+
@@ -0,0 +1,29 @@
+test = (input, expected) ->
+ m = "#{input} "
+ result = approximate input
+ m+= if expected==result then 'ok' else "expected #{expected}, got #{result}"
+ console.log m
+
+approximate = require './approximate'
+test 7,'7'
+test 800,'800'
+test 1200,'1.2K'
+test 1000,'1K'
+test 87821,'88K'
+test 412011,'412K'
+test 999949,'999K'
+test 999950,'1M'
+test 1000000,'1M'
+test 6123922,'6.1M'
+test 27969374,'28M'
+test 28001341,'28M'
+test 28061341,'28M'
+test 812192442,'812M'
+test 999949999,'999M'
+test 999950000,'1B'
+test 1000000000,'1B'
+
+test -8,'-8'
+test -5000,'-5K'
+test -999949999,'-999M'
+test -999950000,'-1B'
@@ -0,0 +1,14 @@
+<script src='jquery.js'></script>
+<script src='../lib/approximate.js'></script>
+<script>
+ $(function() {
+ $('#inp').keyup(function() {
+ $('#out').html(approximate(this.value));
+ });
+ $('#inp').keyup();
+ });
+</script>
+
+<h2>Type a number to format it</h2>
+<input value='1222343' id='inp'></input>
+<p id='out'></p >

Large diffs are not rendered by default.

Oops, something went wrong.

0 comments on commit d1d6fc5

Please sign in to comment.