From f2d4f6ccf69649804a8cd489cf80d9bf518d2c90 Mon Sep 17 00:00:00 2001 From: jdalton Date: Wed, 11 Feb 2015 22:50:39 -0800 Subject: [PATCH] Add `_.matchesProperty`. --- lodash.src.js | 63 +++++++++++++++++++++++++++++++++++++++++++++------ test/test.js | 2 +- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/lodash.src.js b/lodash.src.js index 84834c952..a90ab94fd 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -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; diff --git a/test/test.js b/test/test.js index 4cd605ae3..55002fe11 100644 --- a/test/test.js +++ b/test/test.js @@ -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._;