Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge filipe's unit options

Conflicts:
	src/approximate.coffee
  • Loading branch information...
commit 63571d2ca1b20878b040389ac38b52a8a0db2930 2 parents 8215fd0 + 695dea1
Michael Mahemoff authored
Showing with 92 additions and 12 deletions.
  1. +24 −4 lib/approximate.js
  2. +17 −6 src/approximate.coffee
  3. +51 −2 src/test.coffee
28 lib/approximate.js
View
@@ -4,14 +4,34 @@
(function() {
var approximate, format;
- approximate = function(num) {
+ approximate = function(num, options) {
+ var units;
+ if (options == null) {
+ options = {};
+ }
+ units = {
+ 'thousand': 'K',
+ 'million': 'M',
+ 'billion': 'B'
+ };
+ if (options['units'] != null) {
+ if (options['units']['billion'] != null) {
+ units['billion'] = options['units']['billion'];
+ }
+ if (options['units']['million'] != null) {
+ units['million'] = options['units']['million'];
+ }
+ if (options['units']['thousand'] != null) {
+ units['thousand'] = options['units']['thousand'];
+ }
+ }
num = parseInt(num);
if (Math.abs(num) >= 999950000) {
- return format(num, 1000000000, 'B');
+ return format(num, 1000000000, units['billion']);
} else if (Math.abs(num) >= 999950) {
- return format(num, 1000000, 'M');
+ return format(num, 1000000, units['million']);
} else if (Math.abs(num) >= 1000) {
- return format(num, 1000, 'K');
+ return format(num, 1000, units['thousand']);
} else {
return num + '';
}
23 src/approximate.coffee
View
@@ -1,11 +1,22 @@
-approximate = (num) ->
+approximate = (num, options = {}) ->
+
+ units = { 'thousand' : 'K', 'million' : 'M', 'billion' : 'B'}
+
+ if options['units']?
+ if options['units']['billion']?
+ units['billion'] = options['units']['billion']
+ if options['units']['million']?
+ units['million'] = options['units']['million']
+ if options['units']['thousand']?
+ units['thousand'] = options['units']['thousand']
+
num = parseInt(num);
- if Math.abs(num) >= 999950000
- format num, 1000000000, 'B'
- else if Math.abs(num) >= 999950
- format num, 1000000, 'M'
+ if Math.abs(num) >=999950000
+ format num, 1000000000, units['billion']
+ else if Math.abs(num) >=999950
+ format num, 1000000, units['million']
else if Math.abs(num) >= 1000
- format num, 1000, 'K'
+ format num, 1000, units['thousand']
else
num + ''
53 src/test.coffee
View
@@ -1,6 +1,6 @@
-test = (input, expected) ->
+test = (input, expected, options = {}) ->
m = "#{input} "
- result = approximate input
+ result = approximate input, options
m+= if expected==result then 'ok' else "expected #{expected}, got #{result}"
console.log m
@@ -27,3 +27,52 @@ test -8,'-8'
test -5000,'-5K'
test -999949999,'-999M'
test -999950000,'-1B'
+
+units = {units: {thousand:'e3', million:'e6', billion:'e9'}}
+
+test 7, '7', units
+test 800, '800', units
+test 1200, '1.2e3', units
+test 1000, '1e3', units
+test 87821, '88e3', units
+test 412011, '412e3', units
+test 999949, '999e3', units
+test 999950, '1e6', units
+test 1000000, '1e6', units
+test 6123922, '6.1e6', units
+test 27969374, '28e6', units
+test 28001341, '28e6', units
+test 28061341, '28e6', units
+test 812192442, '812e6', units
+test 999949999, '999e6', units
+test 999950000, '1e9', units
+test 1000000000, '1e9', units
+
+test -8, '-8', units
+test -5000, '-5e3', units
+test -999949999, '-999e6', units
+test -999950000, '-1e9', units
+
+metrics = {units: { billion:'G'}}
+test 7, '7', metrics
+test 800, '800', metrics
+test 1200, '1.2K', metrics
+test 1000, '1K', metrics
+test 87821, '88K', metrics
+test 412011,'412K', metrics
+test 999949, '999K', metrics
+test 999950, '1M', metrics
+test 1000000, '1M', metrics
+test 6123922, '6.1M', metrics
+test 27969374, '28M', metrics
+test 28001341, '28M', metrics
+test 28061341, '28M', metrics
+test 812192442, '812M', metrics
+test 999949999, '999M', metrics
+test 999950000, '1G', metrics
+test 1000000000, '1G', metrics
+
+test -8, '-8', metrics
+test -5000, '-5K', metrics
+test -999949999, '-999M', metrics
+test -999950000, '-1G', metrics
Please sign in to comment.
Something went wrong with that request. Please try again.