Add _.matchesProperty.

This commit is contained in:
jdalton
2015-02-11 22:50:39 -08:00
parent 182cb8ab25
commit f2d4f6ccf6
2 changed files with 57 additions and 8 deletions

View File

@@ -1802,10 +1802,12 @@
if (func == null) {
return identity;
}
// Handle "_.property" and "_.matches" style callback shorthands.
return type == 'object'
? baseMatches(func)
: baseProperty(func + '');
if (type == 'object') {
return baseMatches(func);
}
return typeof thisArg == 'undefined'
? baseProperty(func + '')
: baseMatchesProperty(func + '', thisArg);
}
/**
@@ -2453,8 +2455,7 @@
}
/**
* The base implementation of `_.matches` which supports specifying whether
* `source` should be cloned.
* The base implementation of `_.matches` which does not clone `source`.
*
* @private
* @param {Object} source The object of property values to match.
@@ -2487,6 +2488,26 @@
};
}
/**
* The base implementation of `_.matchesProperty` which does not coerce `key`
* to a string.
*
* @private
* @param {string} key The key of the property to get.
* @param {*} value The value to compare.
* @returns {Function} Returns the new function.
*/
function baseMatchesProperty(key, value) {
if (isStrictComparable(value)) {
return function(object) {
return object != null && object[key] === value;
};
}
return function(object) {
return object != null && baseIsEqual(value, object[key], null, true);
};
}
/**
* The base implementation of `_.merge` without support for argument juggling,
* multiple sources, and `this` binding `customizer` functions.
@@ -6375,7 +6396,7 @@
* // => [36, 40] (iteration order is not guaranteed)
*/
function pluck(collection, key) {
return map(collection, baseProperty(key + ''));
return map(collection, baseProperty(key));
}
/**
@@ -10464,6 +10485,33 @@
return baseMatches(baseClone(source, true));
}
/**
* Creates a function which compares the property value of `key` on a given
* object to `value`.
*
* @static
* @memberOf _
* @category Utility
* @param {string} key The key of the property to get.
* @param {*} value The value to compare.
* @returns {Function} Returns the new function.
* @example
*
* var users = [
* { 'user': 'barney', 'age': 36 },
* { 'user': 'fred', 'age': 40 },
* { 'user': 'pebbles', 'age': 1 }
* ];
*
* var matchFred = _.matchesProperty('user', 'fred');
*
* _.find(users, matchFred);
* // => { 'user': 'fred', 'age': 40 }
*/
function matchesProperty(key, value) {
return baseMatchesProperty(key + '', baseClone(value, true));
}
/**
* Adds all own enumerable function properties of a source object to the
* destination object. If `object` is a function then methods are added to
@@ -10832,6 +10880,7 @@
lodash.map = map;
lodash.mapValues = mapValues;
lodash.matches = matches;
lodash.matchesProperty = matchesProperty;
lodash.memoize = memoize;
lodash.merge = merge;
lodash.mixin = mixin;

View File

@@ -15285,7 +15285,7 @@
var acceptFalsey = _.difference(allMethods, rejectFalsey);
test('should accept falsey arguments', 207, function() {
test('should accept falsey arguments', 208, function() {
var emptyArrays = _.map(falsey, _.constant([])),
isExposed = '_' in root,
oldDash = root._;