From 762748684e75078110c261235ae7b9b87518e021 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 5 Jun 2016 15:08:07 -0700 Subject: [PATCH] Add `_.conformsTo`. --- lodash.js | 74 +++++++++++++++++++++++++++++++++++++++------------- test/test.js | 2 +- 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/lodash.js b/lodash.js index 80d5add89..329f2791e 100644 --- a/lodash.js +++ b/lodash.js @@ -2413,28 +2413,39 @@ * @returns {Function} Returns the new spec function. */ function baseConforms(source) { - var props = keys(source), - length = props.length; - + var props = keys(source); return function(object) { - if (object == null) { - return !length; - } - 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; + return baseConformsTo(object, source, props); }; } + /** + * The base implementation of `_.conformsTo` which accepts `props` to check. + * + * @private + * @param {Object} object The object to inspect. + * @param {Object} source The object of property predicates to conform to. + * @returns {boolean} Returns `true` if `object` conforms, else `false`. + */ + function baseConformsTo(object, source, props) { + var length = props.length; + if (object == null) { + return !length; + } + 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. @@ -10470,6 +10481,32 @@ return baseClone(value, true, true, customizer); } + /** + * Checks if `object` conforms to `source` by invoking the predicate properties + * of `source` with the corresponding property values of `object`. This method + * is equivalent to a `_.conforms` function when `source` is partially applied. + * + * @static + * @memberOf _ + * @since 4.14.0 + * @category Lang + * @param {Object} object The object to inspect. + * @param {Object} source The object of property predicates to conform to. + * @returns {boolean} Returns `true` if `object` conforms, else `false`. + * @example + * + * var object = { 'user': 'fred', 'age': 40 }; + * + * _.conformsTo(object, { 'age': function(n) { return n > 38; } }); + * // => true + * + * _.conformsTo(object, { 'age': function(n) { return n < 38; } }); + * // => false + */ + function conformsTo(object, source) { + return source == null || baseConformsTo(object, source, keys(source)); + } + /** * Performs a * [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero) @@ -16003,6 +16040,7 @@ lodash.cloneDeep = cloneDeep; lodash.cloneDeepWith = cloneDeepWith; lodash.cloneWith = cloneWith; + lodash.conformsTo = conformsTo; lodash.deburr = deburr; lodash.defaultTo = defaultTo; lodash.divide = divide; diff --git a/test/test.js b/test/test.js index b557a215e..8a138fb4b 100644 --- a/test/test.js +++ b/test/test.js @@ -26597,7 +26597,7 @@ var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); QUnit.test('should accept falsey arguments', function(assert) { - assert.expect(315); + assert.expect(316); var arrays = lodashStable.map(falsey, stubArray);