Make _.xyzBy methods provide just a single param to their callbacks.

This commit is contained in:
John-David Dalton
2015-08-29 10:52:02 -07:00
parent 1258eef213
commit dfac7b4261
2 changed files with 52 additions and 45 deletions

View File

@@ -2339,8 +2339,8 @@
*/ */
function basePickBy(object, predicate) { function basePickBy(object, predicate) {
var result = {}; var result = {};
baseForIn(object, function(value, key, object) { baseForIn(object, function(value, key) {
if (predicate(value, key, object)) { if (predicate(value)) {
result[key] = value; result[key] = value;
} }
}); });
@@ -2576,12 +2576,12 @@
index = -1; index = -1;
iteratees = arrayMap(iteratees.length ? iteratees : Array(1), function(iteratee) { iteratees = arrayMap(iteratees.length ? iteratees : Array(1), function(iteratee) {
return toIteratee(iteratee, 3); return toIteratee(iteratee, 1);
}); });
var result = baseMap(collection, function(value, key, collection) { var result = baseMap(collection, function(value, key, collection) {
var criteria = arrayMap(iteratees, function(iteratee) { var criteria = arrayMap(iteratees, function(iteratee) {
return iteratee(value, key, collection); return iteratee(value);
}); });
return { 'criteria': criteria, 'index': ++index, 'value': value }; return { 'criteria': criteria, 'index': ++index, 'value': value };
}); });
@@ -3029,11 +3029,11 @@
while (++index < length) { while (++index < length) {
var value = collection[index]; var value = collection[index];
setter(result, value, iteratee(value, index, collection), collection); setter(result, value, iteratee(value), collection);
} }
} else { } else {
baseEach(collection, function(value, key, collection) { baseEach(collection, function(value, key, collection) {
setter(result, value, iteratee(value, key, collection), collection); setter(result, value, iteratee(value), collection);
}); });
} }
return result; return result;
@@ -5892,7 +5892,7 @@
* Creates an object composed of keys generated from the results of running * Creates an object composed of keys generated from the results of running
* each element of `collection` through `iteratee`. The corresponding value * each element of `collection` through `iteratee`. The corresponding value
* of each key is the number of times the key was returned by `iteratee`. * of each key is the number of times the key was returned by `iteratee`.
* The iteratee is invoked with three arguments: (value, index|key, collection). * The iteratee is invoked with one argument: (value).
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -6128,7 +6128,7 @@
* Creates an object composed of keys generated from the results of running * Creates an object composed of keys generated from the results of running
* each element of `collection` through `iteratee`. The corresponding value * each element of `collection` through `iteratee`. The corresponding value
* of each key is an array of the elements responsible for generating the key. * of each key is an array of the elements responsible for generating the key.
* The iteratee is invoked with three arguments: (value, index|key, collection). * The iteratee is invoked with one argument: (value).
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -6201,7 +6201,7 @@
* Creates an object composed of keys generated from the results of running * Creates an object composed of keys generated from the results of running
* each element of `collection` through `iteratee`. The corresponding value * each element of `collection` through `iteratee`. The corresponding value
* of each key is the last element responsible for generating the key. The * of each key is the last element responsible for generating the key. The
* iteratee is invoked with three arguments: (value, index|key, collection). * iteratee is invoked with one argument: (value).
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -6596,8 +6596,7 @@
* Creates an array of elements, sorted in ascending order by the results of * Creates an array of elements, sorted in ascending order by the results of
* running each element in a collection through each iteratee. This method * running each element in a collection through each iteratee. This method
* performs a stable sort, that is, it preserves the original sort order of * performs a stable sort, that is, it preserves the original sort order of
* equal elements. The iteratees are invoked with three arguments: * equal elements. The iteratees are invoked with one argument: (value).
* (value, index|key, collection).
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -9425,9 +9424,9 @@
* // => { 'user': 'fred' } * // => { 'user': 'fred' }
*/ */
function omitBy(object, predicate) { function omitBy(object, predicate) {
predicate = getIteratee(predicate, 3); predicate = getIteratee(predicate);
return basePickBy(object, function(value, key, object) { return basePickBy(object, function(value) {
return !predicate(value, key, object); return !predicate(value);
}); });
} }
@@ -9475,7 +9474,7 @@
/** /**
* Creates an object composed of the `object` properties `predicate` returns * Creates an object composed of the `object` properties `predicate` returns
* truthy for. The predicate is invoked with three arguments: (value, key, object). * truthy for. The predicate is invoked with one argument: (value).
* *
* @static * @static
* @memberOf _ * @memberOf _
@@ -9491,7 +9490,7 @@
* // => { 'user': 'fred' } * // => { 'user': 'fred' }
*/ */
function pickBy(object, predicate) { function pickBy(object, predicate) {
return object == null ? {} : basePickBy(object, getIteratee(predicate, 3)); return object == null ? {} : basePickBy(object, getIteratee(predicate));
} }
/** /**

View File

@@ -4747,7 +4747,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), isBy = /(^partition|By)$/.test(methodName),
isFind = /^find/.test(methodName), isFind = /^find/.test(methodName),
isSome = methodName == 'some'; isSome = methodName == 'some';
@@ -4767,7 +4767,7 @@
if (_.includes(objectMethods, methodName)) { if (_.includes(objectMethods, methodName)) {
expected[1] += ''; expected[1] += '';
} }
if (isExtremum) { if (isBy) {
expected.length = 1; expected.length = 1;
} }
deepEqual(args, expected); deepEqual(args, expected);
@@ -4784,12 +4784,12 @@
var expected = [[1, 0, array], [undefined, 1, array], [3, 2, array]]; var expected = [[1, 0, array], [undefined, 1, array], [3, 2, array]];
if (isExtremum) { if (isBy) {
expected = _.map(expected, function(args) { expected = _.map(expected, function(args) {
return args.slice(0, 1); return args.slice(0, 1);
}); });
} }
if (_.includes(objectMethods, methodName)) { else if (_.includes(objectMethods, methodName)) {
expected = _.map(expected, function(args) { expected = _.map(expected, function(args) {
args[1] += ''; args[1] += '';
return args; return args;
@@ -4888,9 +4888,9 @@
Foo.prototype.b = 2; Foo.prototype.b = 2;
if (func) { if (func) {
var keys = []; var values = [];
func(new Foo, function(value, key) { keys.push(key); }); func(new Foo, function(value) { values.push(value); });
deepEqual(keys, ['a']); deepEqual(values, [1]);
} }
else { else {
skipTest(); skipTest();
@@ -11204,29 +11204,33 @@
_.each(['omit', 'omitBy'], function(methodName) { _.each(['omit', 'omitBy'], function(methodName) {
var expected = { 'b': 2, 'd': 4 }, var expected = { 'b': 2, 'd': 4 },
func = _[methodName], func = _[methodName],
object = { 'a': 1, 'b': 2, 'c': 3, 'd': 4 }; object = { 'a': 1, 'b': 2, 'c': 3, 'd': 4 },
prop = function(object, props) { return props; };
var prop = methodName == 'omit' ? _.identity : function(props) { if (methodName == 'omitBy') {
props = typeof props == 'string' ? [props] : props; prop = function(object, props) {
return function(value, key) { props = typeof props == 'string' ? [props] : props;
return _.includes(props, key); return function(value) {
return _.some(props, function(key) { return object[key] === value; });
};
}; };
}; }
test('`_.' + methodName + '` should create an object with omitted properties', 2, function() { test('`_.' + methodName + '` should create an object with omitted properties', 2, function() {
deepEqual(func(object, prop('a')), { 'b': 2, 'c': 3, 'd': 4 }); deepEqual(func(object, prop(object, 'a')), { 'b': 2, 'c': 3, 'd': 4 });
deepEqual(func(object, prop(['a', 'c'])), expected); deepEqual(func(object, prop(object, ['a', 'c'])), expected);
}); });
test('`_.' + methodName + '` should iterate over inherited properties', 1, function() { test('`_.' + methodName + '` should iterate over inherited properties', 1, function() {
function Foo() {} function Foo() {}
Foo.prototype = object; Foo.prototype = object;
deepEqual(func(new Foo, prop(['a', 'c'])), expected); var foo = new Foo;
deepEqual(func(foo, prop(object, ['a', 'c'])), expected);
}); });
test('`_.' + methodName + '` should work with an array `object` argument', 1, function() { test('`_.' + methodName + '` should work with an array `object` argument', 1, function() {
deepEqual(func([1, 2, 3], prop(['0', '2'])), { '1': 2 }); var array = [1, 2, 3];
deepEqual(func(array, prop(array, ['0', '2'])), { '1': 2 });
}); });
}); });
@@ -11921,29 +11925,33 @@
_.each(['pick', 'pickBy'], function(methodName) { _.each(['pick', 'pickBy'], function(methodName) {
var expected = { 'a': 1, 'c': 3 }, var expected = { 'a': 1, 'c': 3 },
func = _[methodName], func = _[methodName],
object = { 'a': 1, 'b': 2, 'c': 3, 'd': 4 }; object = { 'a': 1, 'b': 2, 'c': 3, 'd': 4 },
prop = function(object, props) { return props; };
var prop = methodName == 'pick' ? _.identity : function(props) { if (methodName == 'pickBy') {
props = typeof props == 'string' ? [props] : props; prop = function(object, props) {
return function(value, key) { props = typeof props == 'string' ? [props] : props;
return _.includes(props, key); return function(value) {
return _.some(props, function(key) { return object[key] === value; });
};
}; };
}; }
test('`_.' + methodName + '` should create an object of picked properties', 2, function() { test('`_.' + methodName + '` should create an object of picked properties', 2, function() {
deepEqual(func(object, prop('a')), { 'a': 1 }); deepEqual(func(object, prop(object, 'a')), { 'a': 1 });
deepEqual(func(object, prop(['a', 'c'])), expected); deepEqual(func(object, prop(object, ['a', 'c'])), expected);
}); });
test('`_.' + methodName + '` should iterate over inherited properties', 1, function() { test('`_.' + methodName + '` should iterate over inherited properties', 1, function() {
function Foo() {} function Foo() {}
Foo.prototype = object; Foo.prototype = object;
deepEqual(func(new Foo, prop(['a', 'c'])), expected); var foo = new Foo;
deepEqual(func(foo, prop(foo, ['a', 'c'])), expected);
}); });
test('`_.' + methodName + '` should work with an array `object` argument', 1, function() { test('`_.' + methodName + '` should work with an array `object` argument', 1, function() {
deepEqual(func([1, 2, 3], prop('1')), { '1': 2 }); var array = [1, 2, 3];
deepEqual(func(array, prop(array, '1')), { '1': 2 });
}); });
}); });