diff --git a/lodash.js b/lodash.js index 94854d52e..47f537608 100644 --- a/lodash.js +++ b/lodash.js @@ -2185,6 +2185,35 @@ return result; } + /** + * The base implementation of `_.conforms` which doesn't clone `source`. + * + * @private + * @param {Object} source The object of property predicates to conform to. + * @returns {Function} Returns the new function. + */ + function baseConforms(source) { + var props = keysIn(source), + length = props.length; + + return function(object) { + if (object == null) { + return false; + } + var index = length; + while (index--) { + var key = props[index], + predicate = source[key], + value = object[key]; + + if ((value === undefined && !(key in Object(object))) || !predicate(value)) { + return false; + } + } + return true; + }; + } + /** * The base implementation of `_.create` without support for assigning * properties to the created object. @@ -12150,6 +12179,30 @@ } }); + /** + * Creates a function that invokes the predicate properties of `source` with + * the corresponding property values of a given object, returning `true` if + * all predicates return truthy, else `false`. + * + * @static + * @memberOf _ + * @category Utility + * @param {Object} source The object of property predicates to conform to. + * @returns {Function} Returns the new function. + * @example + * + * var users = [ + * { 'user': 'barney', 'age': 36 }, + * { 'user': 'fred', 'age': 40 } + * ]; + * + * _.filter(users, _.conforms({ 'age': _.partial(_.gt, _, 38) })); + * // => [{ 'user': 'fred', 'age': 40 }] + */ + function conforms(source) { + return baseConforms(baseClone(source, true)); + } + /** * Creates a function that returns `value`. * @@ -12942,6 +12995,7 @@ lodash.chain = chain; lodash.chunk = chunk; lodash.compact = compact; + lodash.conforms = conforms; lodash.conj = conj; lodash.constant = constant; lodash.countBy = countBy; diff --git a/test/test.js b/test/test.js index aca3909a6..7faf0552d 100644 --- a/test/test.js +++ b/test/test.js @@ -21768,7 +21768,7 @@ var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); QUnit.test('should accept falsey arguments', function(assert) { - assert.expect(261); + assert.expect(262); var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));