From 23ee7e3f2a0cb484ea845ce88af27ae7d5fece55 Mon Sep 17 00:00:00 2001 From: SoundLogic Date: Thu, 19 Feb 2015 14:00:24 -0500 Subject: [PATCH] made changes as discussed here: https://github.com/a8m/angular-filter/issues/74 and updated tests --- src/_filter/math/math.js | 12 ++++++++- src/_filter/math/short-fmt.js | 42 ++++++++++++++++-------------- test/spec/filter/math/short-fmt.js | 37 +++++++++++++++----------- 3 files changed, 56 insertions(+), 35 deletions(-) diff --git a/src/_filter/math/math.js b/src/_filter/math/math.js index 0ad77b9..97a3af9 100644 --- a/src/_filter/math/math.js +++ b/src/_filter/math/math.js @@ -8,6 +8,16 @@ 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 67b963a..1295875 100644 --- a/src/_filter/math/short-fmt.js +++ b/src/_filter/math/short-fmt.js @@ -11,23 +11,27 @@ 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'; - } +.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"; - } - } -}]); \ No newline at end of file + } 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