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

84
dist/lodash.js vendored
View File

@@ -2114,8 +2114,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;
}
@@ -2796,8 +2795,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;
}
@@ -3079,8 +3077,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;
}
@@ -3718,8 +3715,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;
}
@@ -3794,8 +3790,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;
}
@@ -3985,8 +3980,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
*
@@ -4943,8 +4937,7 @@
* return typeof a == 'undefined' ? b : a;
* });
*
* var object = { 'name': 'barney' };
* defaults(object, { 'name': 'fred', 'employer': 'slate' });
* defaults({ 'name': 'barney' }, { 'name': 'fred', 'employer': 'slate' });
* // => { 'name': 'barney', 'employer': 'slate' }
*/
function assign(object, source, guard) {
@@ -4953,8 +4946,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;
}
@@ -5035,8 +5027,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;
}
@@ -5136,13 +5127,11 @@
* @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
*
* var object = { 'name': 'barney' };
* _.defaults(object, { 'name': 'fred', 'employer': 'slate' });
* _.defaults({ 'name': 'barney' }, { 'name': 'fred', 'employer': 'slate' });
* // => { 'name': 'barney', 'employer': 'slate' }
*/
function defaults(object, source, guard) {
@@ -5151,8 +5140,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;
}
@@ -6041,8 +6029,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;
}
@@ -6077,8 +6064,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
@@ -6092,16 +6080,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];
@@ -6173,10 +6176,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) {