mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-06-19 06:36:15 +08:00
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:
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user