Skip to content

Commit

Permalink
fix(limitTo): do not convert Infinity to NaN
Browse files Browse the repository at this point in the history
parseInt(Infinity, 10) will result in NaN, which becomes undesirable when the expected behaviour is
to return the entire input.

I believe this is possibly useful as a way to toggle input limiting based on certain factors.

Closes angular#6771
  • Loading branch information
caitp committed Mar 20, 2014
1 parent 64363af commit ac83ebd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ng/filter/limitTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ function limitToFilter(){
return function(input, limit) {
if (!isArray(input) && !isString(input)) return input;

limit = int(limit);
if (Math.abs(Number(limit)) === Infinity) limit = Number(limit);
else limit = int(limit);

if (isString(input)) {
//NaN check on limit
Expand Down
14 changes: 14 additions & 0 deletions test/ng/filter/limitToSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,18 @@ describe('Filter: limitTo', function() {
expect(limitTo(str, -9)).toEqual(str);
expect(limitTo(str, '-9')).toEqual(str);
})

it('should return entire input array when limited by Infinity', function() {
expect(limitTo(items, Infinity)).toEqual(items);
expect(limitTo(items, 'Infinity')).toEqual(items);
expect(limitTo(items, -Infinity)).toEqual(items);
expect(limitTo(items, '-Infinity')).toEqual(items);
});

it('should return the entire string when limited by Infinity', function() {
expect(limitTo(str, Infinity)).toEqual(str);
expect(limitTo(str, 'Infinity')).toEqual(str);
expect(limitTo(str, -Infinity)).toEqual(str);
expect(limitTo(str, '-Infinity')).toEqual(str);
});
});

1 comment on commit ac83ebd

@flashbackzoo
Copy link

Choose a reason for hiding this comment

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

One minor thing - the assignment of limit could be tidier.

limit = Math.abs(Number(limit)) === Infinity ? Number(limit) : int(limit);

Otherwise looks good.

Please sign in to comment.