Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit c2aaddb

Browse files
committed
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
1 parent 6f7018d commit c2aaddb

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/ng/filter/filters.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ function currencyFilter($locale) {
5353
var formats = $locale.NUMBER_FORMATS;
5454
return function(amount, currencySymbol){
5555
if (isUndefined(currencySymbol)) currencySymbol = formats.CURRENCY_SYM;
56-
return formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, 2).
57-
replace(/\u00A4/g, currencySymbol);
56+
57+
// if null or undefined pass it through
58+
return (amount == null)
59+
? amount
60+
: formatNumber(amount, formats.PATTERNS[1], formats.GROUP_SEP, formats.DECIMAL_SEP, 2).
61+
replace(/\u00A4/g, currencySymbol);
5862
};
5963
}
6064

test/ng/filter/filtersSpec.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,14 @@ describe('filters', function() {
9898
expect(currency(1234.5678, "USD$")).toEqual('USD$1,234.57');
9999
});
100100

101+
it('should pass through null and undefined to be compatible with one-time binding', function() {
102+
expect(currency(undefined)).toBe(undefined);
103+
expect(currency(null)).toBe(null);
104+
});
101105

102106
it('should return empty string for non-numbers', function() {
103-
expect(currency()).toBe('');
104107
expect(currency('abc')).toBe('');
108+
expect(currency({})).toBe('');
105109
});
106110

107111
it('should handle zero and nearly-zero values properly', function() {

0 commit comments

Comments
 (0)