Permalink
Browse files

Updated _.isFinite to match ES5 and ES6 spec

The EcmaScript 5 and 6 spec define isFinite as returning false only if
ToNumber(number) is NaN, Infinity, or −Infinity.

Current implementation of _.isFinite acts as if ToNumber('42') is NaN.
This commit fixes it while also catching the special gotcha in the spec
where isFinite(' ') returns true.

It's also much faster than current implementation.
  • Loading branch information...
1 parent 7419c80 commit 143d1373064f79757fdb9d484244e38ba951ea78 @TalAter TalAter committed Oct 20, 2012
Showing with 4 additions and 2 deletions.
  1. +3 −1 test/objects.js
  2. +1 −1 underscore.js
View
@@ -485,7 +485,9 @@ $(document).ready(function() {
ok(!_.isFinite(NaN), 'NaN is not Finite');
ok(!_.isFinite(Infinity), 'Infinity is not Finite');
ok(!_.isFinite(-Infinity), '-Infinity is not Finite');
- ok(!_.isFinite('12'), 'Strings are not numbers');
+ ok(_.isFinite('12'), 'Numeric strings are numbers');
+ ok(!_.isFinite('1a'), 'Non numeric strings are not numbers');
+ ok(!_.isFinite(''), 'Empty strings are not numbers');
var obj = new Number(5);
ok(_.isFinite(obj), 'Number instances can be finite');
ok(_.isFinite(0), '0 is Finite');
View
@@ -951,7 +951,7 @@
// Is a given object a finite number?
_.isFinite = function(obj) {
- return _.isNumber(obj) && isFinite(obj);
+ return isFinite( obj ) && !isNaN( parseFloat(obj) );
};
// Is the given value `NaN`? (NaN is the only number which does not equal itself).

0 comments on commit 143d137

Please sign in to comment.