fix(currencyFilter): pass through null and undefined values

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

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

Only cases when the currency 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
This commit is contained in:
Igor Minar
2014-08-29 10:57:12 -07:00
parent 6f7018d52f
commit c2aaddbe4b
2 changed files with 11 additions and 3 deletions

View File

@@ -53,8 +53,12 @@ function currencyFilter($locale) {
var formats = $locale.NUMBER_FORMATS;
return function(amount, currencySymbol){
if (isUndefined(currencySymbol)) currencySymbol = formats.CURRENCY_SYM;
return formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, 2).
replace(/\u00A4/g, currencySymbol);
// if null or undefined pass it through
return (amount == null)
? amount
: formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, 2).
replace(/\u00A4/g, currencySymbol);
};
}

View File

@@ -98,10 +98,14 @@ describe('filters', function() {
expect(currency(1234.5678, "USD$")).toEqual('USD$1,234.57');
});
it('should pass through null and undefined to be compatible with one-time binding', function() {
expect(currency(undefined)).toBe(undefined);
expect(currency(null)).toBe(null);
});
it('should return empty string for non-numbers', function() {
expect(currency()).toBe('');
expect(currency('abc')).toBe('');
expect(currency({})).toBe('');
});
it('should handle zero and nearly-zero values properly', function() {