Skip to content

Commit

Permalink
luifNumber [enh] issue #35, added a placeholder arg to use when the _…
Browse files Browse the repository at this point in the history
…input is undefined, enhanced comportment to always return integralPart<span>separator and decimalPart</span> to allow for selecting decimal part via css selector and appluy custom style, also added support for non numeric inputs
  • Loading branch information
lucienbertin committed Oct 13, 2015
1 parent 04e96da commit 1af0f15
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
27 changes: 20 additions & 7 deletions js/filters/genericFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,30 @@
};
})
.filter('luifNumber', ['$sce', '$filter', function($sce, $filter) {
return function(_input, _precision) {
return function(_input, _precision, _placeholder) {
var placeholder = _placeholder === undefined ? '' : _placeholder;
var input = _input === undefined ? placeholder : _input;
var separator = $filter("number")(1.1,1)[1];
var precision = _precision || 2;
var precision = _precision === undefined ? 2 : _precision;

var text = $filter("number")(_input, precision);
var text = $filter("number")(input, precision);
var decimalPart = (text || $filter("number")(0, precision)).split(separator)[1];
var rightSpan;

var details = text.split(separator);
if ( parseInt(details[1]) === 0) {
return $sce.trustAsHtml(details[0] + "<span style=\"opacity:0\">" + separator + details[1] + "</span>");
if(decimalPart === undefined){
rightSpan = "<span style=\"opacity:0\"></span>";
}else if(parseInt(decimalPart) === 0){
rightSpan = "<span style=\"opacity:0\">" + separator + decimalPart + "</span>";
}else{
rightSpan = "<span>" + separator + decimalPart + "</span>";
}
return $sce.trustAsHtml(details[0] + separator + details[1]);
if(input === '' || !text){
// the _input or the _placeholder was not parsable by the number $filter, just return input but trusted as html
return $sce.trustAsHtml(input + rightSpan);
}

var integerPart = text.split(separator)[0];
return $sce.trustAsHtml(integerPart + rightSpan);
};
}]);
})();
28 changes: 25 additions & 3 deletions tests/spec/genericFilters.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,32 @@ describe('luif.timefilters', function(){
beforeEach(function(){
luifNumber = $filter('luifNumber');
});
it('should work', function(){
expect(luifNumber(10.5).$$unwrapTrustedValue()).toEqual($filter("number")(10.5,2));
expect(luifNumber(10.5,3).$$unwrapTrustedValue()).toEqual($filter("number")(10.5,3));
it('should work with numeric values', function(){
expect(luifNumber(10.5).$$unwrapTrustedValue()).toEqual("10<span>.50</span>");
expect(luifNumber(10.5,3).$$unwrapTrustedValue()).toEqual("10<span>.500</span>");
expect(luifNumber(10,1).$$unwrapTrustedValue()).toEqual("10<span style=\"opacity:0\">.0</span>");
expect(luifNumber(undefined,undefined,undefined).$$unwrapTrustedValue()).toEqual("<span style=\"opacity:0\">.00</span>");
expect(luifNumber(undefined,undefined,0).$$unwrapTrustedValue()).toEqual("0<span style=\"opacity:0\">.00</span>");
expect(luifNumber(undefined,undefined,'nothing').$$unwrapTrustedValue()).toEqual("nothing<span style=\"opacity:0\">.00</span>");
expect(luifNumber(undefined,4,undefined).$$unwrapTrustedValue()).toEqual("<span style=\"opacity:0\">.0000</span>");
expect(luifNumber(undefined,4,0).$$unwrapTrustedValue()).toEqual("0<span style=\"opacity:0\">.0000</span>");
expect(luifNumber(undefined,4,'nothing').$$unwrapTrustedValue()).toEqual("nothing<span style=\"opacity:0\">.0000</span>");
});
it('should add a margin right to string values', function(){
expect(luifNumber('a string').$$unwrapTrustedValue()).toEqual("a string<span style=\"opacity:0\">.00</span>");
expect(luifNumber('a string',4).$$unwrapTrustedValue()).toEqual("a string<span style=\"opacity:0\">.0000</span>");
});
it('should work with precision = 0', function(){
expect(luifNumber(12.4,0).$$unwrapTrustedValue()).toEqual("12<span style=\"opacity:0\"></span>");
expect(luifNumber('a string',0).$$unwrapTrustedValue()).toEqual("a string<span style=\"opacity:0\"></span>");
});
it('should work with value = undefined', function(){
expect(luifNumber(undefined).$$unwrapTrustedValue()).toEqual("<span style=\"opacity:0\">.00</span>");
expect(luifNumber(undefined,undefined,0).$$unwrapTrustedValue()).toEqual("0<span style=\"opacity:0\">.00</span>");
expect(luifNumber(undefined,undefined,'nothing').$$unwrapTrustedValue()).toEqual("nothing<span style=\"opacity:0\">.00</span>");
expect(luifNumber(undefined,4).$$unwrapTrustedValue()).toEqual("<span style=\"opacity:0\">.0000</span>");
expect(luifNumber(undefined,4,0).$$unwrapTrustedValue()).toEqual("0<span style=\"opacity:0\">.0000</span>");
expect(luifNumber(undefined,4,'nothing').$$unwrapTrustedValue()).toEqual("nothing<span style=\"opacity:0\">.0000</span>");
});
});
});

0 comments on commit 1af0f15

Please sign in to comment.