fix(numberFilter): pass through null and undefined values

When these special values are passed through one-time binding will work correctly.

BREAKING CHANGE: previously the number filter would convert null and undefined values into empty string, after this change
these values will be passed through.

Only cases when the number filter is chained with another filter that doesn't expect null/undefined will be affected. This
should be very rare.

This change will not change the visual output of the filter because the interpolation will convert the null/undefined to
an empty string.

Closes #8605
Closes #8842
This commit is contained in:
Igor Minar
2014-08-29 10:59:12 -07:00
parent c2aaddbe4b
commit 2ae10f67fc
2 changed files with 12 additions and 4 deletions

View File

@@ -117,14 +117,18 @@ numberFilter.$inject = ['$locale'];
function numberFilter($locale) {
var formats = $locale.NUMBER_FORMATS;
return function(number, fractionSize) {
return formatNumber(number, formats.PATTERNS[0], formats.GROUP_SEP, formats.DECIMAL_SEP,
fractionSize);
// if null or undefined pass it through
return (number == null)
? number
: formatNumber(number, formats.PATTERNS[0], formats.GROUP_SEP, formats.DECIMAL_SEP,
fractionSize);
};
}
var DECIMAL_SEP = '.';
function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
if (number == null || !isFinite(number) || isObject(number)) return '';
if (!isFinite(number) || isObject(number)) return '';
var isNegative = number < 0;
number = Math.abs(number);

View File

@@ -134,7 +134,6 @@ describe('filters', function() {
expect(number(1234)).toEqual('1,234');
expect(number(1234.5678)).toEqual('1,234.568');
expect(number(Number.NaN)).toEqual('');
expect(number(null)).toEqual('');
expect(number({})).toEqual('');
expect(number([])).toEqual('');
expect(number(+Infinity)).toEqual('');
@@ -165,6 +164,11 @@ describe('filters', function() {
expect(number(0, 8)).toEqual("0.00000000");
});
it('should pass through null and undefined to be compatible with one-time binding', function() {
expect(number(null)).toBe(null);
expect(number(undefined)).toBe(undefined);
});
it('should filter exponentially large numbers', function() {
expect(number(1e50)).toEqual('1e+50');
expect(number(-2e100)).toEqual('-2e+100');