Allow _.omit and _.pick to work as a callback for _.map when combined with _.partialRight.

This commit is contained in:
John-David Dalton
2014-02-04 00:35:14 -08:00
parent 143e9bd210
commit 201d17bc31
8 changed files with 237 additions and 152 deletions

View File

@@ -2392,8 +2392,7 @@
callback = isShallow;
isShallow = false;
// allows working with functions like `_.map` without using
// their `index` argument as a callback
// enables use as a callback for functions like `_.map`
if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === array) {
callback = null;
}
@@ -3074,8 +3073,7 @@
callback = isSorted;
isSorted = false;
// allows working with functions like `_.map` without using
// their `index` argument as a callback
// enables use as a callback for functions like `_.map`
if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === array) {
callback = null;
}
@@ -3358,8 +3356,7 @@
length = props.length,
type = typeof guard;
// allows working with functions like `_.map` without using
// their `index` arguments
// enables use as a callback for functions like `_.map`
if ((type == 'number' || type == 'string') && args[2] && args[2][guard] === collection) {
length = 1;
}
@@ -3996,8 +3993,7 @@
result = computed,
type = typeof callback;
// allows working with functions like `_.map` without using
// their `index` argument as a callback
// enables use as a callback for functions like `_.map`
if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === collection) {
callback = null;
}
@@ -4072,8 +4068,7 @@
result = computed,
type = typeof callback;
// allows working with functions like `_.map` without using
// their `index` argument as a callback
// enables use as a callback for functions like `_.map`
if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === collection) {
callback = null;
}
@@ -4263,8 +4258,7 @@
* @category Collections
* @param {Array|Object|string} collection The collection to sample.
* @param {number} [n] The number of elements to sample.
* @param- {Object} [guard] Allows working with functions like `_.map`
* without using their `index` arguments as `n`.
* @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
* @returns {*} Returns the random sample(s) of `collection`.
* @example
*
@@ -5235,8 +5229,7 @@
argsLength = args.length,
type = typeof guard;
// allows working with functions like `_.reduce` without using their
// `key` and `object` arguments as sources
// enables use as a callback for functions like `_.reduce`
if ((type == 'number' || type == 'string') && args[3] && args[3][guard] === source) {
argsLength = 2;
}
@@ -5317,8 +5310,7 @@
callback = isDeep;
isDeep = false;
// allows working with functions like `_.map` without using
// their `index` argument as a callback
// enables use as a callback for functions like `_.map`
if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === value) {
callback = null;
}
@@ -5418,8 +5410,7 @@
* @category Objects
* @param {Object} object The destination object.
* @param {...Object} [source] The source objects.
* @param- {Object} [guard] Allows working with functions like `_.reduce`
* without using their `key` and `object` arguments as sources.
* @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`.
* @returns {Object} Returns the destination object.
* @example
*
@@ -5432,8 +5423,7 @@
argsLength = args.length,
type = typeof guard;
// allows working with functions like `_.reduce` without using their
// `key` and `object` arguments as sources
// enables use as a callback for functions like `_.reduce`
if ((type == 'number' || type == 'string') && args[3] && args[3][guard] === source) {
argsLength = 2;
}
@@ -6335,8 +6325,7 @@
length = args.length,
type = typeof guard;
// allows working with functions like `_.reduce` without using their
// `key` and `object` arguments as sources
// enables use as a callback for functions like `_.reduce`
if ((type == 'number' || type == 'string') && args[3] && args[3][guard] === source) {
length = 2;
}
@@ -6371,8 +6360,9 @@
* @memberOf _
* @category Objects
* @param {Object} object The source object.
* @param {Function|...string|string[]} [callback] The properties to omit or the
* function called per iteration.
* @param {Function|...string|string[]} [callback] The function called per
* iteration or property names to omit, specified as individual property
* names or arrays of property names.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {Object} Returns an object without the omitted properties.
* @example
@@ -6386,16 +6376,31 @@
* // => { 'name': 'fred' }
*/
function omit(object, callback, thisArg) {
var result = {};
if (typeof callback != 'function') {
var result = {},
type = typeof callback;
if (type != 'function') {
// enables use as a callback for functions like `_.map`
// when combined with `_.partialRight`
var args = arguments;
if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === object) {
args = slice(args);
splice.call(args, 1, 2);
}
var omitProps = baseFlatten(args, true, false, 1),
length = omitProps.length;
while (length--) {
omitProps[length] = String(omitProps[length]);
}
var props = [];
baseForIn(object, function(value, key) {
props.push(key);
});
props = baseDifference(props, baseFlatten(arguments, true, false, 1));
var index = -1,
length = props.length;
var index = -1;
props = baseDifference(props, omitProps);
length = props.length;
while (++index < length) {
var key = props[index];
@@ -6467,10 +6472,19 @@
* // => { 'name': 'fred' }
*/
function pick(object, callback, thisArg) {
var result = {};
if (typeof callback != 'function') {
var result = {},
type = typeof callback;
if (type != 'function') {
// enables use as a callback for functions like `_.map`
// when combined with `_.partialRight`
var args = arguments;
if ((type == 'number' || type == 'string') && thisArg && thisArg[callback] === object) {
args = slice(args);
splice.call(args, 1, 2);
}
var index = -1,
props = baseFlatten(arguments, true, false, 1),
props = baseFlatten(args, true, false, 1),
length = isObject(object) ? props.length : 0;
while (++index < length) {