From 705eff4826660d11ec2af4fa7aafd4bed92e66a1 Mon Sep 17 00:00:00 2001 From: Raymond May Jr Date: Sun, 25 Mar 2012 10:43:30 -0500 Subject: [PATCH 1/6] add _.isFinite - verifies is a number between Inf && -Inf --- underscore.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/underscore.js b/underscore.js index aeaa8b1b0..98ffea186 100644 --- a/underscore.js +++ b/underscore.js @@ -812,6 +812,11 @@ return toString.call(obj) == '[object Number]'; }; + // Is a givin number finite? + _.isFinite = function(obj) { + return obj > -1/0 && obj < 1/0 && obj === -obj; + }; + // Is the given value `NaN`? _.isNaN = function(obj) { // `NaN` is the only value for which `===` is not reflexive. From e27423586528b0947407b36dba6916cc33c61e9b Mon Sep 17 00:00:00 2001 From: Raymond May Jr Date: Sun, 25 Mar 2012 11:08:23 -0500 Subject: [PATCH 2/6] _.isFinite fix and tests --- test/objects.js | 11 +++++++++++ underscore.js | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/test/objects.js b/test/objects.js index 1745d8a3f..072cfb537 100644 --- a/test/objects.js +++ b/test/objects.js @@ -481,6 +481,17 @@ $(document).ready(function() { ok(_.isRegExp(iRegExp), 'even from another frame'); }); + test("objects: isFinite", function() { + ok(!_.isFinite(undefined), 'undefined is not Finite'); + ok(!_.isFinite(null), 'null 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(0), '0 is Finite'); + ok(_.isFinite(123), 'Ints are Finite'); + ok(_.isFinite(-12.44), 'Floats are Finite'); + }); + test("objects: isNaN", function() { ok(!_.isNaN(undefined), 'undefined is not NaN'); ok(!_.isNaN(null), 'null is not NaN'); diff --git a/underscore.js b/underscore.js index 98ffea186..5060147e3 100644 --- a/underscore.js +++ b/underscore.js @@ -814,7 +814,7 @@ // Is a givin number finite? _.isFinite = function(obj) { - return obj > -1/0 && obj < 1/0 && obj === -obj; + return obj > -1/0 && obj < 1/0 && obj === +obj; }; // Is the given value `NaN`? From 55b68fdabaa28688ac037530b842dcf4ccf19d2e Mon Sep 17 00:00:00 2001 From: Raymond May Jr Date: Sun, 25 Mar 2012 11:20:57 -0500 Subject: [PATCH 3/6] formatting --- test/objects.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/objects.js b/test/objects.js index 072cfb537..a5e3c7c62 100644 --- a/test/objects.js +++ b/test/objects.js @@ -490,7 +490,7 @@ $(document).ready(function() { ok(_.isFinite(0), '0 is Finite'); ok(_.isFinite(123), 'Ints are Finite'); ok(_.isFinite(-12.44), 'Floats are Finite'); - }); + }); test("objects: isNaN", function() { ok(!_.isNaN(undefined), 'undefined is not NaN'); From bf6cb03519b1237c5d48039299bc3a6aa0134435 Mon Sep 17 00:00:00 2001 From: Raymond May Jr Date: Sun, 25 Mar 2012 14:10:35 -0500 Subject: [PATCH 4/6] test against NaN --- test/objects.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/objects.js b/test/objects.js index a5e3c7c62..630e2de30 100644 --- a/test/objects.js +++ b/test/objects.js @@ -484,6 +484,7 @@ $(document).ready(function() { test("objects: isFinite", function() { ok(!_.isFinite(undefined), 'undefined is not Finite'); ok(!_.isFinite(null), 'null is not Finite'); + 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'); From 4266d9180578f027a48655d8966fc771db6192e1 Mon Sep 17 00:00:00 2001 From: Raymond May Jr Date: Mon, 26 Mar 2012 13:00:25 -0500 Subject: [PATCH 5/6] test _.isFinite against new Number --- test/objects.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/objects.js b/test/objects.js index 630e2de30..62a74e731 100644 --- a/test/objects.js +++ b/test/objects.js @@ -488,6 +488,8 @@ $(document).ready(function() { ok(!_.isFinite(Infinity), 'Infinity is not Finite'); ok(!_.isFinite(-Infinity), '-Infinity is not Finite'); ok(!_.isFinite('12'), 'Strings are not numbers'); + var obj = new Number(5); + ok(_.isFinite(obj), 'Number instances can be finite'); ok(_.isFinite(0), '0 is Finite'); ok(_.isFinite(123), 'Ints are Finite'); ok(_.isFinite(-12.44), 'Floats are Finite'); From ee86820473b355af0a7fede78d7d948083a4ea46 Mon Sep 17 00:00:00 2001 From: Raymond May Jr Date: Mon, 26 Mar 2012 13:02:40 -0500 Subject: [PATCH 6/6] use _.isNumber for correctly working check --- underscore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/underscore.js b/underscore.js index 5060147e3..eb279ae52 100644 --- a/underscore.js +++ b/underscore.js @@ -814,7 +814,7 @@ // Is a givin number finite? _.isFinite = function(obj) { - return obj > -1/0 && obj < 1/0 && obj === +obj; + return _.isNumber(obj) && isFinite(obj); }; // Is the given value `NaN`?