Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated shortFmt filter to be more "forgiving" with various inputs #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/_filter/math/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}]);
43 changes: 25 additions & 18 deletions src/_filter/math/short-fmt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}]);
.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';
}
})
}]);
37 changes: 22 additions & 15 deletions test/spec/filter/math/short-fmt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});

});
});