mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-12 03:47:50 +00:00
Issue #272 ... min and max of empty objects.
This commit is contained in:
@@ -177,6 +177,9 @@ $(document).ready(function() {
|
|||||||
|
|
||||||
var neg = _.max([1, 2, 3], function(num){ return -num; });
|
var neg = _.max([1, 2, 3], function(num){ return -num; });
|
||||||
equals(neg, 1, 'can perform a computation-based max');
|
equals(neg, 1, 'can perform a computation-based max');
|
||||||
|
|
||||||
|
equals(-Infinity, _.max({}), 'Maximum value of an empty object');
|
||||||
|
equals(-Infinity, _.max([]), 'Maximum value of an empty array');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('collections: min', function() {
|
test('collections: min', function() {
|
||||||
@@ -184,6 +187,9 @@ $(document).ready(function() {
|
|||||||
|
|
||||||
var neg = _.min([1, 2, 3], function(num){ return -num; });
|
var neg = _.min([1, 2, 3], function(num){ return -num; });
|
||||||
equals(neg, 3, 'can perform a computation-based min');
|
equals(neg, 3, 'can perform a computation-based min');
|
||||||
|
|
||||||
|
equals(Infinity, _.min({}), 'Minimum value of an empty object');
|
||||||
|
equals(Infinity, _.min([]), 'Minimum value of an empty array');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('collections: sortBy', function() {
|
test('collections: sortBy', function() {
|
||||||
|
|||||||
@@ -220,6 +220,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)) return Math.max.apply(Math, obj);
|
||||||
|
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) {
|
||||||
var computed = iterator ? iterator.call(context, value, index, list) : value;
|
var computed = iterator ? iterator.call(context, value, index, list) : value;
|
||||||
@@ -231,6 +232,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)) return Math.min.apply(Math, obj);
|
||||||
|
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) {
|
||||||
var computed = iterator ? iterator.call(context, value, index, list) : value;
|
var computed = iterator ? iterator.call(context, value, index, list) : value;
|
||||||
|
|||||||
Reference in New Issue
Block a user