Reduce baseIsMatch and isMatch by adding getMatchData.

This commit is contained in:
jdalton
2015-05-16 13:56:49 -07:00
parent c42fea0d3d
commit b652fc7e1a

View File

@@ -2382,31 +2382,34 @@
* *
* @private * @private
* @param {Object} object The object to inspect. * @param {Object} object The object to inspect.
* @param {Array} props The source property names to match. * @param {Array} matchData The propery names, values, and compare flags to match.
* @param {Array} values The source values to match.
* @param {Array} strictCompareFlags Strict comparison flags for source values.
* @param {Function} [customizer] The function to customize comparing objects. * @param {Function} [customizer] The function to customize comparing objects.
* @returns {boolean} Returns `true` if `object` is a match, else `false`. * @returns {boolean} Returns `true` if `object` is a match, else `false`.
*/ */
function baseIsMatch(object, props, values, strictCompareFlags, customizer) { function baseIsMatch(object, matchData, customizer) {
var length = props.length, var index = matchData.length,
index = length, length = index,
noCustomizer = !customizer; noCustomizer = !customizer;
if (object == null) {
return !length;
}
while (index--) { while (index--) {
if ((noCustomizer && strictCompareFlags[index]) var data = matchData[index];
? values[index] !== object[props[index]] if ((noCustomizer && data[2])
: !(props[index] in object) ? data[1] !== object[data[0]]
: !(data[0] in object)
) { ) {
return false; return false;
} }
} }
while (++index < length) { while (++index < length) {
var key = props[index], data = matchData[index];
var key = data[0],
objValue = object[key], objValue = object[key],
srcValue = values[index]; srcValue = data[1];
if (noCustomizer && strictCompareFlags[index]) { if (noCustomizer && data[2]) {
if (objValue === undefined && !(key in object)) { if (objValue === undefined && !(key in object)) {
return false; return false;
} }
@@ -2447,36 +2450,21 @@
* @returns {Function} Returns the new function. * @returns {Function} Returns the new function.
*/ */
function baseMatches(source) { function baseMatches(source) {
var props = keys(source), var matchData = getMatchData(source);
length = props.length; if (matchData.length == 1 && matchData[0][2]) {
var key = matchData[0][0],
value = matchData[0][1];
if (!length) { return function(object) {
return constant(true); if (object == null) {
} return false;
if (length == 1) { }
var key = props[0], object = toObject(object);
value = source[key]; return object[key] === value && (value !== undefined || (key in object));
};
if (isStrictComparable(value)) {
return function(object) {
if (object == null) {
return false;
}
object = toObject(object);
return object[key] === value && (value !== undefined || (key in object));
};
}
}
var values = Array(length),
strictCompareFlags = Array(length);
while (length--) {
value = source[props[length]];
values[length] = value;
strictCompareFlags[length] = isStrictComparable(value);
} }
return function(object) { return function(object) {
return object != null && baseIsMatch(toObject(object), props, values, strictCompareFlags); return baseIsMatch(object, matchData);
}; };
} }
@@ -4077,6 +4065,23 @@
*/ */
var getLength = baseProperty('length'); var getLength = baseProperty('length');
/**
* Gets the propery names, values, and compare flags of `object`.
*
* @private
* @param {Object} object The object to query.
* @returns {Array} Returns the match data of `object`.
*/
function getMatchData(object) {
var result = pairs(object),
length = result.length;
while (length--) {
result[length][2] = isStrictComparable(result[length][1]);
}
return result;
}
/** /**
* Gets the native function at `key` of `object`. * Gets the native function at `key` of `object`.
* *
@@ -8915,24 +8920,8 @@
* // => true * // => true
*/ */
function isMatch(object, source, customizer, thisArg) { function isMatch(object, source, customizer, thisArg) {
var props = keys(source),
length = props.length;
if (!length) {
return true;
}
if (object == null) {
return false;
}
var values = Array(length),
strictCompareFlags = Array(length);
while (length--) {
var value = values[length] = source[props[length]];
strictCompareFlags[length] = isStrictComparable(value);
}
customizer = typeof customizer == 'function' ? bindCallback(customizer, thisArg, 3) : undefined; customizer = typeof customizer == 'function' ? bindCallback(customizer, thisArg, 3) : undefined;
return baseIsMatch(toObject(object), props, values, strictCompareFlags, customizer); return baseIsMatch(object, getMatchData(source), customizer);
} }
/** /**