From 3c5686f7842a2f52e1692348a848124d3578b993 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Fri, 16 Dec 2011 12:41:20 -0500 Subject: [PATCH] Fixes #403 --- test/collections.js | 7 +++++-- test/functions.js | 7 +++---- underscore.js | 1 + 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/test/collections.js b/test/collections.js index 499432ac1..e66dac48b 100644 --- a/test/collections.js +++ b/test/collections.js @@ -49,6 +49,9 @@ $(document).ready(function() { var ifnull = _.map(null, function(){}); ok(_.isArray(ifnull) && ifnull.length === 0, 'handles a null properly'); + + var length = _.map(Array(2), function(v) { return v; }).length; + equals(length, 2, "can preserve a sparse array's length"); }); test('collections: reduce', function() { @@ -83,7 +86,7 @@ $(document).ready(function() { var sparseArray = []; sparseArray[0] = 20; sparseArray[2] = -5; - equals(_.reduce(sparseArray, function(a, b){ return a - b }), 25, 'initially-sparse arrays with no memo'); + equals(_.reduce(sparseArray, function(a, b){ return a - b; }), 25, 'initially-sparse arrays with no memo'); }); test('collections: reduceRight', function() { @@ -112,7 +115,7 @@ $(document).ready(function() { var sparseArray = []; sparseArray[0] = 20; sparseArray[2] = -5; - equals(_.reduceRight(sparseArray, function(a, b){ return a - b }), -25, 'initially-sparse arrays with no memo'); + equals(_.reduceRight(sparseArray, function(a, b){ return a - b; }), -25, 'initially-sparse arrays with no memo'); }); test('collections: detect', function() { diff --git a/test/functions.js b/test/functions.js index f084b015e..fdaddcc96 100644 --- a/test/functions.js +++ b/test/functions.js @@ -112,10 +112,9 @@ $(document).ready(function() { throttledUpdate(1); throttledUpdate(2); throttledUpdate(3); setTimeout(function(){ throttledUpdate(4); }, 120); setTimeout(function(){ throttledUpdate(5); }, 140); - setTimeout(function(){ throttledUpdate(6); }, 260); - setTimeout(function(){ throttledUpdate(7); }, 270); - _.delay(function(){ ok(value == 1, "updated to latest value"); }, 40); - _.delay(function(){ ok(value == 7, "updated to latest value"); start(); }, 400); + setTimeout(function(){ throttledUpdate(6); }, 250); + _.delay(function(){ equals(value, 1, "updated to latest value"); }, 40); + _.delay(function(){ equals(value, 6, "updated to latest value"); start(); }, 400); }); asyncTest("functions: throttle once", 1, function() { diff --git a/underscore.js b/underscore.js index b6b28137d..748ea920c 100644 --- a/underscore.js +++ b/underscore.js @@ -102,6 +102,7 @@ each(obj, function(value, index, list) { results[results.length] = iterator.call(context, value, index, list); }); + if (obj.length === +obj.length) results.length = obj.length; return results; };