Files
lodash/internal/baseMatchesProperty.js
2015-04-15 20:56:31 -07:00

47 lines
1.3 KiB
JavaScript

import baseGet from './baseGet';
import baseIsEqual from './baseIsEqual';
import baseSlice from './baseSlice';
import isArray from '../lang/isArray';
import isKey from './isKey';
import isStrictComparable from './isStrictComparable';
import last from '../array/last';
import toObject from './toObject';
import toPath from './toPath';
/**
* The base implementation of `_.matchesProperty` which does not which does
* not clone `value`.
*
* @private
* @param {string} path The path of the property to get.
* @param {*} value The value to compare.
* @returns {Function} Returns the new function.
*/
function baseMatchesProperty(path, value) {
var isArr = isArray(path),
isCommon = isKey(path) && isStrictComparable(value),
pathKey = (path + '');
path = toPath(path);
return function(object) {
if (object == null) {
return false;
}
var key = pathKey;
object = toObject(object);
if ((isArr || !isCommon) && !(key in object)) {
object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
if (object == null) {
return false;
}
key = last(path);
object = toObject(object);
}
return object[key] === value
? (value !== undefined || (key in object))
: baseIsEqual(value, object[key], null, true);
};
}
export default baseMatchesProperty;