From 3512e7fc5c1164a5f6d1f4dc450d0e3895770255 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Tue, 24 Jan 2012 10:40:20 -0500 Subject: [PATCH] Fixes #435 -- allows _.max and _.min on arrays of dates without converting them to numbers. --- test/collections.js | 8 +++++++- underscore.js | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/test/collections.js b/test/collections.js index cff9763ce..838c0cf4d 100644 --- a/test/collections.js +++ b/test/collections.js @@ -185,7 +185,9 @@ $(document).ready(function() { // Relevant when using ClojureScript test('collections: invoke when strings have a call method', function() { - String.prototype.call = function(){return 42;} + String.prototype.call = function() { + return 42; + }; var list = [[5, 1, 7], [3, 2, 1]]; var s = "foo"; equals(s.call(), 42, "call function exists"); @@ -219,6 +221,10 @@ $(document).ready(function() { equals(Infinity, _.min({}), 'Minimum value of an empty object'); equals(Infinity, _.min([]), 'Minimum value of an empty array'); + + var now = new Date(9999999999); + var then = new Date(0); + equals(_.min([now, then]), then); }); test('collections: sortBy', function() { diff --git a/underscore.js b/underscore.js index 208d4cd89..a24750d19 100644 --- a/underscore.js +++ b/underscore.js @@ -224,7 +224,7 @@ // Return the maximum element or (element-based computation). _.max = function(obj, iterator, context) { - if (!iterator && _.isArray(obj)) return Math.max.apply(Math, obj); + if (!iterator && _.isArray(obj) && obj[0] === +obj[0]) return Math.max.apply(Math, obj); if (!iterator && _.isEmpty(obj)) return -Infinity; var result = {computed : -Infinity}; each(obj, function(value, index, list) { @@ -236,7 +236,7 @@ // Return the minimum element (or element-based computation). _.min = function(obj, iterator, context) { - if (!iterator && _.isArray(obj)) return Math.min.apply(Math, obj); + if (!iterator && _.isArray(obj) && obj[0] === +obj[0]) return Math.min.apply(Math, obj); if (!iterator && _.isEmpty(obj)) return Infinity; var result = {computed : Infinity}; each(obj, function(value, index, list) {