From 6846d71f88417905ab0284e89f15e0b0a140ad2c Mon Sep 17 00:00:00 2001 From: shinuza Date: Sat, 7 May 2011 11:24:05 +0200 Subject: [PATCH 1/3] Fixing _.any returning an incorrect result when Array.prototype.some is missing but Array.prototype.forEach exists. --- underscore.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/underscore.js b/underscore.js index a0ad75eec..1119eb400 100644 --- a/underscore.js +++ b/underscore.js @@ -186,7 +186,10 @@ if (obj == null) return result; if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context); each(obj, function(value, index, list) { - if (result = iterator.call(context, value, index, list)) return breaker; + if (iterator.call(context, value, index, list)) { + result = true; + return breaker; + } }); return result; }; From 5141a51298c0c4ef02219ec404736a425ebf3da9 Mon Sep 17 00:00:00 2001 From: shinuza Date: Sat, 7 May 2011 16:29:43 +0200 Subject: [PATCH 2/3] Refactored fix as suggested in https://github.com/documentcloud/underscore/issues/177 --- underscore.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/underscore.js b/underscore.js index 1119eb400..d007edf57 100644 --- a/underscore.js +++ b/underscore.js @@ -180,19 +180,16 @@ // Determine if at least one element in the object matches a truth test. // Delegates to **ECMAScript 5**'s native `some` if available. // Aliased as `any`. - var any = _.some = _.any = function(obj, iterator, context) { - iterator || (iterator = _.identity); + var any = _.some = _.any = function (obj, iterator, context) { + iterator = iterator || _.identity; var result = false; if (obj == null) return result; if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context); - each(obj, function(value, index, list) { - if (iterator.call(context, value, index, list)) { - result = true; - return breaker; - } + each(obj, function (value, index, list) { + if (result |= iterator.call(context, value, index, list)) return breaker; }); - return result; - }; + return !!result; + } // Determine if a given value is included in the array or object using `===`. // Aliased as `contains`. From 40af1652ebb1b07ae1dc7e754e9872b1db5a5a5f Mon Sep 17 00:00:00 2001 From: shinuza Date: Fri, 20 May 2011 09:13:58 +0200 Subject: [PATCH 3/3] Modified any/some test case to demonstrate issue #177 Fixed any/some formatting to be consistent with the rest of underscore.js --- test/collections.js | 3 +++ underscore.js | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/test/collections.js b/test/collections.js index e1dc5ebfc..91027f217 100644 --- a/test/collections.js +++ b/test/collections.js @@ -132,12 +132,15 @@ $(document).ready(function() { }); test('collections: any', function() { + var nativeSome = Array.prototype.some; + Array.prototype.some = null; ok(!_.any([]), 'the empty set'); ok(!_.any([false, false, false]), 'all false values'); ok(_.any([false, false, true]), 'one true value'); ok(!_.any([1, 11, 29], function(num){ return num % 2 == 0; }), 'all odd numbers'); ok(_.any([1, 10, 29], function(num){ return num % 2 == 0; }), 'an even number'); ok(_.some([false, false, true]), 'aliased as "some"'); + Array.prototype.some = nativeSome; }); test('collections: include', function() { diff --git a/underscore.js b/underscore.js index d007edf57..5c707e99f 100644 --- a/underscore.js +++ b/underscore.js @@ -186,10 +186,10 @@ if (obj == null) return result; if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context); each(obj, function (value, index, list) { - if (result |= iterator.call(context, value, index, list)) return breaker; + if (result |= iterator.call(context, value, index, list)) return breaker; }); return !!result; - } + }; // Determine if a given value is included in the array or object using `===`. // Aliased as `contains`.