mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
Make _.maxBy, _.minBy, _.sumBy, & _.uniqBy support only arrays and provide only 1 argument to iteratees.
This commit is contained in:
committed by
John-David Dalton
parent
340a6d195f
commit
65d5bba7df
@@ -1504,6 +1504,13 @@
|
|||||||
result = value;
|
result = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
index = result === exValue ? -1 : index;
|
||||||
|
while (++index < length) {
|
||||||
|
value = array[index];
|
||||||
|
if (+iteratee(value) === exValue) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2732,7 +2739,7 @@
|
|||||||
outer:
|
outer:
|
||||||
while (++index < length) {
|
while (++index < length) {
|
||||||
var value = array[index],
|
var value = array[index],
|
||||||
computed = iteratee ? iteratee(value, index, array) : value;
|
computed = iteratee ? iteratee(value) : value;
|
||||||
|
|
||||||
if (isCommon && value === value) {
|
if (isCommon && value === value) {
|
||||||
var seenIndex = seen.length;
|
var seenIndex = seen.length;
|
||||||
@@ -5306,9 +5313,8 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is like `_.uniq` except that it accepts an iteratee which is
|
* This method is like `_.uniq` except that it accepts an iteratee which is
|
||||||
* invoked for each value in `array` to generate the criterion by which
|
* invoked for each element in `array` to generate the criterion by which
|
||||||
* uniqueness is computed. The iteratee is invoked with three arguments:
|
* uniqueness is computed. The iteratee is invoked with one argument: (value).
|
||||||
* (value, index, array).
|
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
@@ -11136,7 +11142,7 @@
|
|||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @category Math
|
* @category Math
|
||||||
* @param {Array|Object|string} collection The collection to iterate over.
|
* @param {Array} array The array to iterate over.
|
||||||
* @returns {*} Returns the maximum value.
|
* @returns {*} Returns the maximum value.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
@@ -11146,20 +11152,19 @@
|
|||||||
* _.max([]);
|
* _.max([]);
|
||||||
* // => -Infinity
|
* // => -Infinity
|
||||||
*/
|
*/
|
||||||
function max(collection) {
|
function max(array) {
|
||||||
return maxBy(collection, identity);
|
return maxBy(array, identity);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is like `_.max` except that it accepts an iteratee which is
|
* This method is like `_.max` except that it accepts an iteratee which is
|
||||||
* invoked for each value in `collection` to generate the criterion by which
|
* invoked for each element in `array` to generate the criterion by which
|
||||||
* the value is ranked. The iteratee is invoked with three arguments:
|
* the value is ranked. The iteratee is invoked with one argument: (value).
|
||||||
* (value, index, collection).
|
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @category Math
|
* @category Math
|
||||||
* @param {Array|Object|string} collection The collection to iterate over.
|
* @param {Array} array The array to iterate over.
|
||||||
* @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration.
|
* @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration.
|
||||||
* @returns {*} Returns the maximum value.
|
* @returns {*} Returns the maximum value.
|
||||||
* @example
|
* @example
|
||||||
@@ -11179,13 +11184,13 @@
|
|||||||
var maxBy = createExtremum(gt, NEGATIVE_INFINITY);
|
var maxBy = createExtremum(gt, NEGATIVE_INFINITY);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the minimum value of `collection`. If `collection` is empty or falsey
|
* Gets the minimum value of `array`. If `array` is empty or falsey
|
||||||
* `Infinity` is returned.
|
* `Infinity` is returned.
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @category Math
|
* @category Math
|
||||||
* @param {Array|Object|string} collection The collection to iterate over.
|
* @param {Array} array The array to iterate over.
|
||||||
* @returns {*} Returns the minimum value.
|
* @returns {*} Returns the minimum value.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
@@ -11195,20 +11200,19 @@
|
|||||||
* _.min([]);
|
* _.min([]);
|
||||||
* // => Infinity
|
* // => Infinity
|
||||||
*/
|
*/
|
||||||
function min(collection) {
|
function min(array) {
|
||||||
return minBy(collection, identity);
|
return minBy(array, identity);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is like `_.min` except that it accepts an iteratee which is
|
* This method is like `_.min` except that it accepts an iteratee which is
|
||||||
* invoked for each value in `collection` to generate the criterion by which
|
* invoked for each element in `array` to generate the criterion by which
|
||||||
* the value is ranked. The iteratee is invoked with three arguments:
|
* the value is ranked. The iteratee is invoked with one argument: (value).
|
||||||
* (value, index, collection).
|
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @category Math
|
* @category Math
|
||||||
* @param {Array|Object|string} collection The collection to iterate over.
|
* @param {Array} array The array to iterate over.
|
||||||
* @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration.
|
* @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration.
|
||||||
* @returns {*} Returns the minimum value.
|
* @returns {*} Returns the minimum value.
|
||||||
* @example
|
* @example
|
||||||
|
|||||||
66
test/test.js
66
test/test.js
@@ -4735,7 +4735,9 @@
|
|||||||
|
|
||||||
var arrayMethods = [
|
var arrayMethods = [
|
||||||
'findIndex',
|
'findIndex',
|
||||||
'findLastIndex'
|
'findLastIndex',
|
||||||
|
'maxBy',
|
||||||
|
'minBy'
|
||||||
];
|
];
|
||||||
|
|
||||||
var collectionMethods = [
|
var collectionMethods = [
|
||||||
@@ -4750,8 +4752,6 @@
|
|||||||
'groupBy',
|
'groupBy',
|
||||||
'indexBy',
|
'indexBy',
|
||||||
'map',
|
'map',
|
||||||
'maxBy',
|
|
||||||
'minBy',
|
|
||||||
'partition',
|
'partition',
|
||||||
'reduce',
|
'reduce',
|
||||||
'reduceRight',
|
'reduceRight',
|
||||||
@@ -4822,6 +4822,7 @@
|
|||||||
_.each(methods, function(methodName) {
|
_.each(methods, function(methodName) {
|
||||||
var array = [1, 2, 3],
|
var array = [1, 2, 3],
|
||||||
func = _[methodName],
|
func = _[methodName],
|
||||||
|
isExtremum = /^(?:max|min)/.test(methodName),
|
||||||
isFind = /^find/.test(methodName),
|
isFind = /^find/.test(methodName),
|
||||||
isSome = methodName == 'some';
|
isSome = methodName == 'some';
|
||||||
|
|
||||||
@@ -4841,6 +4842,9 @@
|
|||||||
if (_.includes(objectMethods, methodName)) {
|
if (_.includes(objectMethods, methodName)) {
|
||||||
expected[1] += '';
|
expected[1] += '';
|
||||||
}
|
}
|
||||||
|
if (isExtremum) {
|
||||||
|
expected.length = 1;
|
||||||
|
}
|
||||||
deepEqual(args, expected);
|
deepEqual(args, expected);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -4854,6 +4858,12 @@
|
|||||||
array[2] = 3;
|
array[2] = 3;
|
||||||
|
|
||||||
var expected = [[1, 0, array], [undefined, 1, array], [3, 2, array]];
|
var expected = [[1, 0, array], [undefined, 1, array], [3, 2, array]];
|
||||||
|
|
||||||
|
if (isExtremum) {
|
||||||
|
expected = _.map(expected, function(args) {
|
||||||
|
return args.slice(0, 1);
|
||||||
|
});
|
||||||
|
}
|
||||||
if (_.includes(objectMethods, methodName)) {
|
if (_.includes(objectMethods, methodName)) {
|
||||||
expected = _.map(expected, function(args) {
|
expected = _.map(expected, function(args) {
|
||||||
args[1] += '';
|
args[1] += '';
|
||||||
@@ -10059,11 +10069,12 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should return `-Infinity` for empty collections', 1, function() {
|
test('should return `-Infinity` for empty collections', 1, function() {
|
||||||
var expected = _.map(empties, _.constant(-Infinity));
|
var values = falsey.concat([[]]),
|
||||||
|
expected = _.map(values, _.constant(-Infinity));
|
||||||
|
|
||||||
var actual = _.map(empties, function(value) {
|
var actual = _.map(values, function(value, index) {
|
||||||
try {
|
try {
|
||||||
return _.max(value);
|
return index ? _.max(value) : _.max();
|
||||||
} catch(e) {}
|
} catch(e) {}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -10071,16 +10082,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should return `-Infinity` for non-numeric collection values', 1, function() {
|
test('should return `-Infinity` for non-numeric collection values', 1, function() {
|
||||||
var collections = [['a', 'b'], { 'a': 'a', 'b': 'b' }],
|
strictEqual(_.max(['a', 'b']), -Infinity);
|
||||||
expected = _.map(collections, _.constant(-Infinity));
|
|
||||||
|
|
||||||
var actual = _.map(collections, function(value) {
|
|
||||||
try {
|
|
||||||
return _.max(value);
|
|
||||||
} catch(e) {}
|
|
||||||
});
|
|
||||||
|
|
||||||
deepEqual(actual, expected);
|
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
@@ -10820,11 +10822,12 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should return `Infinity` for empty collections', 1, function() {
|
test('should return `Infinity` for empty collections', 1, function() {
|
||||||
var expected = _.map(empties, _.constant(Infinity));
|
var values = falsey.concat([[]]),
|
||||||
|
expected = _.map(values, _.constant(Infinity));
|
||||||
|
|
||||||
var actual = _.map(empties, function(value) {
|
var actual = _.map(values, function(value, index) {
|
||||||
try {
|
try {
|
||||||
return _.min(value);
|
return index ? _.min(value) : _.min();
|
||||||
} catch(e) {}
|
} catch(e) {}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -10832,16 +10835,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('should return `Infinity` for non-numeric collection values', 1, function() {
|
test('should return `Infinity` for non-numeric collection values', 1, function() {
|
||||||
var collections = [['a', 'b'], { 'a': 'a', 'b': 'b' }],
|
strictEqual(_.min(['a', 'b']), Infinity);
|
||||||
expected = _.map(collections, _.constant(Infinity));
|
|
||||||
|
|
||||||
var actual = _.map(collections, function(value) {
|
|
||||||
try {
|
|
||||||
return _.min(value);
|
|
||||||
} catch(e) {}
|
|
||||||
});
|
|
||||||
|
|
||||||
deepEqual(actual, expected);
|
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
@@ -10861,24 +10855,16 @@
|
|||||||
strictEqual(func([curr, past]), isMax ? curr : past);
|
strictEqual(func([curr, past]), isMax ? curr : past);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('`_.' + methodName + '` should iterate an object', 1, function() {
|
|
||||||
var actual = func({ 'a': 1, 'b': 2, 'c': 3 });
|
|
||||||
strictEqual(actual, isMax ? 3 : 1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('`_.' + methodName + '` should work with extremely large arrays', 1, function() {
|
test('`_.' + methodName + '` should work with extremely large arrays', 1, function() {
|
||||||
var array = _.range(0, 5e5);
|
var array = _.range(0, 5e5);
|
||||||
strictEqual(func(array), isMax ? 499999 : 0);
|
strictEqual(func(array), isMax ? 499999 : 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('`_.' + methodName + '` should work as an iteratee for methods like `_.map`', 2, function() {
|
test('`_.' + methodName + '` should work as an iteratee for methods like `_.map`', 1, function() {
|
||||||
var arrays = [[2, 1], [5, 4], [7, 8]],
|
var arrays = [[2, 1], [5, 4], [7, 8]],
|
||||||
objects = [{ 'a': 2, 'b': 1 }, { 'a': 5, 'b': 4 }, { 'a': 7, 'b': 8 }],
|
|
||||||
expected = isMax ? [2, 5, 8] : [1, 4, 7];
|
expected = isMax ? [2, 5, 8] : [1, 4, 7];
|
||||||
|
|
||||||
_.each([arrays, objects], function(values) {
|
deepEqual(_.map(arrays, func), expected);
|
||||||
deepEqual(_.map(values, func), expected);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('`_.' + methodName + '` should work when chaining on an array with only one value', 1, function() {
|
test('`_.' + methodName + '` should work when chaining on an array with only one value', 1, function() {
|
||||||
@@ -16106,7 +16092,7 @@
|
|||||||
args || (args = slice.call(arguments));
|
args || (args = slice.call(arguments));
|
||||||
});
|
});
|
||||||
|
|
||||||
deepEqual(args, [objects[0], 0, objects]);
|
deepEqual(args, [objects[0]]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should work with `iteratee` without specifying `isSorted`', 1, function() {
|
test('should work with `iteratee` without specifying `isSorted`', 1, function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user