Skip to content

Commit

Permalink
number checking in .compact
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewplummer committed Aug 13, 2011
1 parent 1369892 commit 81692a2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/sugar.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@
};

var toIntegerWithDefault = function(i, d) {
if(i === undefined || isNaN(i)) {
if(isNaN(i)) {
return d;
} else {
return parseInt(i >> 0);
Expand Down Expand Up @@ -1553,7 +1553,7 @@
this.each(function(el, i) {
if(Array.isArray(el)) {
result.push(el.compact());
} else if(el !== undefined && el !== null && !isNaN(el)) {
} else if(el !== undefined && el !== null && (!Object.isNumber(el) || !isNaN(el))) {
result.push(el);
}
});
Expand Down
4 changes: 4 additions & 0 deletions unit_tests/sugar/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,8 @@ test('Array', function () {
same(['wherever','you','go','whatever','you','do'].split(/^[gd]o/), [['wherever','you'],['whatever','you']], 'Array#split | split on regex | split on strings with length of 2');


var f1 = function(){};
var f2 = function(){};

same([1,2,3].compact(), [1,2,3], 'Array#compact | 1,2,3');
same([1,2,null,3].compact(), [1,2,3], 'Array#compact | 1,2,null,3');
Expand All @@ -958,6 +960,8 @@ test('Array', function () {
same([false,false,false].compact(), [false,false,false], 'Array#compact | false is left alone');
same([0,1,2].compact(), [0,1,2], 'Array#compact | 0,1,2');
same([].compact(), [], 'Array#compact | empty array');
same(['a','b','c'].compact(), ['a','b','c'], 'Array#compact | a,b,c');
same([f1, f2].compact(), [f1, f2], 'Array#compact | functions');
sameWithException([null,[null],[false,[null,undefined,3]]].compact(), [[],[false,[3]]], { prototype: [[null],[false,[null,undefined,3]]] }, 'Array#compact | deep compacts as well');
sameWithException([null,null,null,[null],null].compact(), [[]], { prototype: [[null]] }, "Array#compact | deep compact doesn't have index conflicts");

Expand Down

0 comments on commit 81692a2

Please sign in to comment.