Add _.conformsTo.

This commit is contained in:
John-David Dalton
2016-06-05 15:08:07 -07:00
parent bdfd5880e9
commit 762748684e
2 changed files with 57 additions and 19 deletions

View File

@@ -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;

View File

@@ -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);