From a40b496ba2838cd7eb9c77910a77132742ebb0b2 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Fri, 29 Aug 2014 10:57:12 -0700 Subject: [PATCH 1/2] 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 --- src/ng/filter/filters.js | 8 ++++++-- test/ng/filter/filtersSpec.js | 6 +++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/ng/filter/filters.js b/src/ng/filter/filters.js index 5757760245b9..4aedb54695cc 100644 --- a/src/ng/filter/filters.js +++ b/src/ng/filter/filters.js @@ -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); }; } diff --git a/test/ng/filter/filtersSpec.js b/test/ng/filter/filtersSpec.js index 7d2acef42601..f1b1732e52d0 100644 --- a/test/ng/filter/filtersSpec.js +++ b/test/ng/filter/filtersSpec.js @@ -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() { From 56b65439371f9647b95c61163536048533fffa2b Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Fri, 29 Aug 2014 10:59:12 -0700 Subject: [PATCH 2/2] 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 --- src/ng/filter/filters.js | 10 +++++++--- test/ng/filter/filtersSpec.js | 6 +++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/ng/filter/filters.js b/src/ng/filter/filters.js index 4aedb54695cc..6a1d2eb16acc 100644 --- a/src/ng/filter/filters.js +++ b/src/ng/filter/filters.js @@ -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); diff --git a/test/ng/filter/filtersSpec.js b/test/ng/filter/filtersSpec.js index f1b1732e52d0..6f48bc826749 100644 --- a/test/ng/filter/filtersSpec.js +++ b/test/ng/filter/filtersSpec.js @@ -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');