diff --git a/src/_filter/math/math.js b/src/_filter/math/math.js index 091e750..97a3af9 100644 --- a/src/_filter/math/math.js +++ b/src/_filter/math/math.js @@ -4,7 +4,20 @@ * @description * reference to global Math object */ + angular.module('a8m.math', []) .factory('$math', ['$window', function ($window) { - return $window.Math; + + var math = $window.Math; + math.filterFactory = function(fn){ + return function(input){ + input = Number(input); + return input != input + ? input + : fn.apply(this, arguments); + } + } + + return math; + }]); diff --git a/src/_filter/math/short-fmt.js b/src/_filter/math/short-fmt.js index fa819b9..1295875 100644 --- a/src/_filter/math/short-fmt.js +++ b/src/_filter/math/short-fmt.js @@ -8,23 +8,30 @@ * i.e: K for one thousand, M for Million, B for billion * e.g: number of users:235,221, decimal:1 => 235.2 K */ + angular.module('a8m.math.shortFmt', ['a8m.math']) - .filter('shortFmt', ['$math', function ($math) { - return function (number, decimal) { - if(isNumber(decimal) && isFinite(decimal) && decimal%1===0 && decimal >= 0 && - isNumber(number) && isFinite(number)){ - - if(number < 1e3) { - return number; - } else if(number < 1e6) { - return convertToDecimal((number / 1e3), decimal, $math) + ' K'; - } else if(number < 1e9){ - return convertToDecimal((number / 1e6), decimal, $math) + ' M'; - } else { - return convertToDecimal((number / 1e9), decimal, $math) + ' B'; - } - } - return "NaN"; - } -}]); \ No newline at end of file +.filter('shortFmt', ['$math', function($math) { + return $math.filterFactory(function(number, decimal) { + if (isNumber(decimal) + && isFinite(decimal) + && decimal % 1 === 0 + && decimal >= 0 + && isNumber(number) + && isFinite(number)) { + var sign = 1; + if(number < 0){ + sign = -1; + number = number * sign; + } else if (number == 0) { return '0'; } + var k = 1000, sizes = ['',' K', ' M', ' B', ' T', ' Q']; + // i = nth exponent of k by way of the change of base formula for logs + // then taking the floor of that to get only the integer value of the number + var i = $math.floor($math.log(number) / $math.log(k)); + return (sign * (number / $math.pow(k, i))).toFixed(decimal) + sizes[i]; + + } else { + return 'NaN'; + } + }) + }]); diff --git a/test/spec/filter/math/short-fmt.js b/test/spec/filter/math/short-fmt.js index 3308a8c..d07c5c7 100644 --- a/test/spec/filter/math/short-fmt.js +++ b/test/spec/filter/math/short-fmt.js @@ -11,28 +11,35 @@ describe('shortFmtFilter', function () { })); it('should return the correct display from the number', function() { - expect(filter(0,2)).toEqual(0); - expect(filter(5,2)).toEqual(5); + expect(filter(0,2)).toEqual('0'); + expect(filter(5,2)).toEqual('5.00'); expect(filter(1024,0)).toEqual("1 K"); + expect(filter(-1024,0)).toEqual("-1 K"); expect(filter(1993,2)).toEqual("1.99 K"); - expect(filter(1049901,5)).toEqual("1.0499 M"); + expect(filter(-1993,2)).toEqual("-1.99 K"); + expect(filter(1049901,5)).toEqual("1.04990 M"); + expect(filter(-1049901,5)).toEqual("-1.04990 M"); expect(filter(1909234901,2)).toEqual("1.91 B"); - + expect(filter(-1909234901,2)).toEqual("-1.91 B"); }); - it('should return NaN if bytes is not a number', function(){ - expect(filter("0",2)).toEqual("NaN"); - expect(filter([0],2)).toEqual("NaN"); - expect(filter({number:0},0)).toEqual("NaN"); + it('should return correct display if input can be parsed as a number', function(){ + expect(filter("0",2)).toEqual('0'); + expect(filter([0],2)).toEqual('0'); + expect(filter([],2)).toEqual('0'); + }); + + it('should return NaN if input cannot be parsed as a number', function(){ + expect(filter({number:0},0)).toEqual(NaN); }); it('should return NaN if decimal point is less than zero or not a number', function(){ - expect(filter(0.45,-1)).toEqual("NaN"); - expect(filter(-0.25,-101)).toEqual("NaN"); - expect(filter(0.45,1.3)).toEqual("NaN"); - expect(filter(0.45,"0")).toEqual("NaN"); - expect(filter(0.45,[3])).toEqual("NaN"); - expect(filter(0.45,{num : 4})).toEqual("NaN"); + expect(filter(0.45,-1)).toEqual("NaN"); + expect(filter(-0.25,-101)).toEqual("NaN"); + expect(filter(0.45,1.3)).toEqual("NaN"); + expect(filter(0.45,"0")).toEqual("NaN"); + expect(filter(0.45,[3])).toEqual("NaN"); + expect(filter(0.45,{num : 4})).toEqual("NaN"); }); -}); +}); \ No newline at end of file