Skip to content

Commit

Permalink
Fixes jashkenas#435 -- allows _.max and _.min on arrays of dates with…
Browse files Browse the repository at this point in the history
…out converting them to numbers.
  • Loading branch information
jashkenas authored and piouPiouM committed Jan 24, 2012
1 parent 21e188b commit fe5cc27
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion test/collections.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ $(document).ready(function() {


// Relevant when using ClojureScript // Relevant when using ClojureScript
test('collections: invoke when strings have a call method', function() { 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 list = [[5, 1, 7], [3, 2, 1]];
var s = "foo"; var s = "foo";
equals(s.call(), 42, "call function exists"); equals(s.call(), 42, "call function exists");
Expand Down Expand Up @@ -219,6 +221,10 @@ $(document).ready(function() {


equals(Infinity, _.min({}), 'Minimum value of an empty object'); equals(Infinity, _.min({}), 'Minimum value of an empty object');
equals(Infinity, _.min([]), 'Minimum value of an empty array'); 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() { test('collections: sortBy', function() {
Expand Down
4 changes: 2 additions & 2 deletions underscore.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@


// Return the maximum element or (element-based computation). // Return the maximum element or (element-based computation).
_.max = function(obj, iterator, context) { _.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; if (!iterator && _.isEmpty(obj)) return -Infinity;
var result = {computed : -Infinity}; var result = {computed : -Infinity};
each(obj, function(value, index, list) { each(obj, function(value, index, list) {
Expand All @@ -239,7 +239,7 @@


// Return the minimum element (or element-based computation). // Return the minimum element (or element-based computation).
_.min = function(obj, iterator, context) { _.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; if (!iterator && _.isEmpty(obj)) return Infinity;
var result = {computed : Infinity}; var result = {computed : Infinity};
each(obj, function(value, index, list) { each(obj, function(value, index, list) {
Expand Down

0 comments on commit fe5cc27

Please sign in to comment.