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

Fix for number filter checking for numbers #6261

Closed
wants to merge 1 commit into from
Closed

Fix for number filter checking for numbers #6261

wants to merge 1 commit into from

Conversation

sagens42
Copy link
Contributor

Fix for number filter when passing not a number to a filter. isNaN is
broken, so we should use isNumber function in formatNumber function.
Close #6188

@mary-poppins
Copy link

Thanks for the PR! Please check the items below to help us merge this faster. See the contributing docs for more information.

  • Uses the issue template (#6261)

If you need to make changes to your pull request, you can update the commit with git commit --amend.
Then, update the pull request with git push -f.

Thanks again for your help!

@@ -118,7 +118,7 @@ function numberFilter($locale) {

var DECIMAL_SEP = '.';
function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
if (isNaN(number) || !isFinite(number)) return '';
if (!isNumber(number) || !isFinite(number)) return '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break numbers which happen to be strings.

Better solution: if ( (number !== number) || !isFinite(number) ), or even just if (!isFinite(number)), because isFinite(NaN) should return false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this suppose to fix issue when null converted by numberFilter to 0 instead of empty string. Refer to docs (http://docs.angularjs.org/api/ng.filter:number) this should be If the input is not a number an empty string is returned.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced, I see no reason why the filter should return an empty string for "123"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree with you my fix is not fully applicable. It should work for numbers which is strings also. Trying to find more efficient solution

@sagens42
Copy link
Contributor Author

Updated solution for #6188. Another problem I found is that currently isNumber(NaN) returns true that seems not to be correct. With this fix everything works fine

@@ -118,7 +118,7 @@ function numberFilter($locale) {

var DECIMAL_SEP = '.';
function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
if (isNaN(number) || !isFinite(number)) return '';
if (!isNumber(parseFloat(number))) return '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is getting even weirder than it was this morning.. This is not right :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, tried to make it work as expected with any possible data types

@sagens42
Copy link
Contributor Author

Ok, final version. Now it works with passing null, numbers as strings, and numbers. And returns empty string for null.

@@ -118,7 +118,7 @@ function numberFilter($locale) {

var DECIMAL_SEP = '.';
function formatNumber(number, pattern, groupSep, decimalSep, fractionSize) {
if (isNaN(number) || !isFinite(number)) return '';
if (!isFinite(number) || !number) return '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will turn 0 into the empty string....

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (number == null || !isFinite(number)) return '';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to also add || isObject(number) to prevent processing arrays as numbers

@sagens42
Copy link
Contributor Author

Fixed also for arrays

@caitp
Copy link
Contributor

caitp commented Feb 14, 2014

Okay, this is relatively good, now all you need is some tests. It might be worth re-ordering those tests at the beginning of the filter for perf reasons, but I'm not terribly concerned with it.

@sagens42
Copy link
Contributor Author

Ok, done :)

@caitp
Copy link
Contributor

caitp commented Feb 14, 2014

Okay, again, we need tests! I want to see a test verifying that the number filter on null doesn't get turned into 0.

EG:

expect(number(null)).toBe('');
expect(number(undefined)).toBe('');
expect(number({})).toBe('');
expect(number([])).toBe('');
expect(number(+Infinity)).toBe('');
expect(number(-Infinity)).toBe('');

under the it('should do basic filter', function() { ... } spec in test/ng/filter/filtersSpec.js

Fix for number filter when passing not a number to a filter. isNaN is
broken, so we should use isNumber function in formatNumber function.
Close #6188
@sagens42
Copy link
Contributor Author

Sorry, did not get the first time

@caitp
Copy link
Contributor

caitp commented Feb 14, 2014

Good stuff, LGTM =) I think we can probably get this in today, lemmec

@sagens42
Copy link
Contributor Author

Nice :) Thank you

@IgorMinar
Copy link
Contributor

Lgtm

@caitp caitp closed this in cceb455 Feb 15, 2014
@caitp
Copy link
Contributor

caitp commented Feb 15, 2014

Thanks for the work. Unfortunately I just realized this will break non-primitive Number/String objects, but I don't think that's going to be a problem, and if anyone complains about it, it can be sorted out later.

khepin pushed a commit to khepin/angular.js that referenced this pull request Feb 19, 2014
…ings 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 angular#6188
Closes angular#6261
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

number filter converts null to "0"
4 participants