Ensure methods like _.get, _.has, _.matchesProperty, _.property, & _.propertyOf can get characters of string indexes.

This commit is contained in:
jdalton
2015-04-07 00:16:33 -07:00
parent af8fcf234f
commit cc0b8e9d10

View File

@@ -2261,7 +2261,11 @@
* @returns {*} Returns the resolved value. * @returns {*} Returns the resolved value.
*/ */
function baseGet(object, path, pathKey) { function baseGet(object, path, pathKey) {
if (typeof pathKey != 'undefined' && pathKey in toObject(object)) { if (object == null) {
return;
}
object = toObject(object);
if (typeof pathKey != 'undefined' && pathKey in object) {
path = [pathKey]; path = [pathKey];
} }
var index = -1, var index = -1,
@@ -2465,8 +2469,11 @@
if (isStrictComparable(value)) { if (isStrictComparable(value)) {
return function(object) { return function(object) {
return object != null && object[key] === value && if (object == null) {
(typeof value != 'undefined' || (key in toObject(object))); return false;
}
object = toObject(object);
return object[key] === value && (typeof value != 'undefined' || (key in object));
}; };
} }
} }
@@ -2493,22 +2500,18 @@
* @returns {Function} Returns the new function. * @returns {Function} Returns the new function.
*/ */
function baseMatchesProperty(path, value) { function baseMatchesProperty(path, value) {
if (isKey(path) && isStrictComparable(value)) { var isArr = isArray(path),
return function(object) { isCommon = isKey(path) && isStrictComparable(value),
return object != null && object[path] === value && pathKey = isArr ? undefined : (path + '');
(typeof value != 'undefined' || (path in toObject(object)));
};
}
var pathKey = isArray(path) ? undefined : path;
path = toPath(path); path = toPath(path);
return function(object) { return function(object) {
if (object == null) { if (object == null) {
return false; return false;
} }
var key = pathKey; var key = pathKey;
object = toObject(object); if ((isArr || !isCommon) && !(key in object)) {
if (typeof key == 'undefined' || !(key in object)) { object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
object = baseGet(object, baseSlice(path, 0, -1));
if (object == null) { if (object == null) {
return false; return false;
} }
@@ -8834,12 +8837,13 @@
return false; return false;
} }
customizer = typeof customizer == 'function' && bindCallback(customizer, thisArg, 3); customizer = typeof customizer == 'function' && bindCallback(customizer, thisArg, 3);
object = toObject(object);
if (!customizer && length == 1) { if (!customizer && length == 1) {
var key = props[0], var key = props[0],
value = source[key]; value = source[key];
if (isStrictComparable(value)) { if (isStrictComparable(value)) {
return value === object[key] && (typeof value != 'undefined' || (key in toObject(object))); return value === object[key] && (typeof value != 'undefined' || (key in object));
} }
} }
var values = Array(length), var values = Array(length),
@@ -8849,7 +8853,7 @@
value = values[length] = source[props[length]]; value = values[length] = source[props[length]];
strictCompareFlags[length] = isStrictComparable(value); strictCompareFlags[length] = isStrictComparable(value);
} }
return baseIsMatch(toObject(object), props, values, strictCompareFlags, customizer); return baseIsMatch(object, props, values, strictCompareFlags, customizer);
} }
/** /**
@@ -9519,7 +9523,7 @@
if (!result && !isKey(path)) { if (!result && !isKey(path)) {
path = toPath(path); path = toPath(path);
object = baseGet(object, baseSlice(path, 0, -1)); object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
result = object != null && hasOwnProperty.call(object, last(path)); result = object != null && hasOwnProperty.call(object, last(path));
} }
return result; return result;
@@ -9930,7 +9934,7 @@
function result(object, path, defaultValue) { function result(object, path, defaultValue) {
if (!isKey(path, object)) { if (!isKey(path, object)) {
path = toPath(path); path = toPath(path);
object = baseGet(object, baseSlice(path, 0, -1)); object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
path = last(path); path = last(path);
} }
var result = object == null ? undefined : object[path]; var result = object == null ? undefined : object[path];