mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-26 22:35:15 +08:00
fix(numberFilter): convert all non-finite/non-numbers/non-numeric strings to the empty string
The previous code for filtering out non-finite numbers was broken, as it would convert `null` to `0`, as well as arrays. This change fixes this by converting null/undefined/NaN/Infinity/any object to the empty string. Closes #6188 Closes #6261
This commit is contained in:
@@ -118,7 +118,7 @@ function numberFilter($locale) {
|
||||
|
||||
var DECIMAL_SEP = '.';
|
||||
function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
|
||||
if (isNaN(number) || !isFinite(number)) return '';
|
||||
if (number == null || !isFinite(number) || isObject(number)) return '';
|
||||
|
||||
var isNegative = number < 0;
|
||||
number = Math.abs(number);
|
||||
|
||||
@@ -128,6 +128,11 @@ 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('');
|
||||
expect(number(-Infinity)).toEqual('');
|
||||
expect(number("1234.5678")).toEqual('1,234.568');
|
||||
expect(number(1/0)).toEqual("");
|
||||
expect(number(1, 2)).toEqual("1.00");
|
||||
|
||||
Reference in New Issue
Block a user