Adjust baseGet and isKey to reduce code in other methods.

This commit is contained in:
jdalton
2015-04-05 21:22:11 -05:00
parent d7a4524e70
commit d70bc74dc8

View File

@@ -2257,15 +2257,18 @@
* @private * @private
* @param {Object} object The object to query. * @param {Object} object The object to query.
* @param {Array} path The path of the property to get. * @param {Array} path The path of the property to get.
* @param {string} [pathKey] The key representation of path.
* @returns {*} Returns the resolved value. * @returns {*} Returns the resolved value.
*/ */
function baseGet(object, path) { function baseGet(object, path, pathKey) {
var result, if (typeof pathKey != 'undefined' && pathKey in toObject(object)) {
index = -1, path = [pathKey];
}
var index = -1,
length = path.length; length = path.length;
while (object != null && ++index < length) { while (object != null && ++index < length) {
result = object = object[path[index]]; var result = object = object[path[index]];
} }
return result; return result;
} }
@@ -2490,13 +2493,13 @@
* @returns {Function} Returns the new function. * @returns {Function} Returns the new function.
*/ */
function baseMatchesProperty(path, value) { function baseMatchesProperty(path, value) {
var pathKey = path + '';
if (isKey(path) && isStrictComparable(value)) { if (isKey(path) && isStrictComparable(value)) {
return function(object) { return function(object) {
return object != null && object[pathKey] === value && return object != null && object[path] === value &&
(typeof value != 'undefined' || (pathKey in toObject(object))); (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) {
@@ -2504,7 +2507,7 @@
} }
var key = pathKey; var key = pathKey;
object = toObject(object); object = toObject(object);
if (!(key in object)) { if (typeof key == 'undefined' || !(key in object)) {
object = baseGet(object, baseSlice(path, 0, -1)); object = baseGet(object, baseSlice(path, 0, -1));
if (object == null) { if (object == null) {
return false; return false;
@@ -2648,15 +2651,10 @@
* @returns {Function} Returns the new function. * @returns {Function} Returns the new function.
*/ */
function basePropertyDeep(path) { function basePropertyDeep(path) {
var pathKey = path + ''; var pathKey = isArray(path) ? undefined : (path + '');
path = toPath(path); path = toPath(path);
return function(object) { return function(object) {
if (object == null) { return baseGet(object, path, pathKey);
return undefined;
}
return pathKey in toObject(object)
? object[pathKey]
: baseGet(object, path);
}; };
} }
@@ -4265,11 +4263,15 @@
* *
* @private * @private
* @param {*} value The value to check. * @param {*} value The value to check.
* @param {Object} [object] The object to query keys on.
* @returns {boolean} Returns `true` if `value` is a property name, else `false`. * @returns {boolean} Returns `true` if `value` is a property name, else `false`.
*/ */
function isKey(value) { function isKey(value, object) {
var type = typeof value; if (isArray(value)) {
return type == 'number' || (type == 'string' && !reIsDeepProp.test(value)); return false;
}
var result = !reIsDeepProp.test(value);
return result || (object != null && value in toObject(object));
} }
/** /**
@@ -9486,12 +9488,9 @@
* // => 'default' * // => 'default'
*/ */
function get(object, path, defaultValue) { function get(object, path, defaultValue) {
var result; var pathKey = isArray(path) ? undefined : (path + ''),
if (object != null) { result = baseGet(object, toPath(path), pathKey);
result = (isKey(path) || (path in toObject(object)))
? object[path]
: baseGet(object, toPath(path));
}
return typeof result == 'undefined' ? defaultValue : result; return typeof result == 'undefined' ? defaultValue : result;
} }
@@ -9512,7 +9511,12 @@
* // => true * // => true
*/ */
function has(object, path) { function has(object, path) {
var result = object != null && hasOwnProperty.call(object, path); if (object == null) {
return false;
}
var pathKey = isArray(path) ? undefined : (path + ''),
result = typeof pathKey != 'undefined' && hasOwnProperty.call(object, path);
if (!result && !isKey(path)) { if (!result && !isKey(path)) {
path = toPath(path); path = toPath(path);
object = baseGet(object, baseSlice(path, 0, -1)); object = baseGet(object, baseSlice(path, 0, -1));
@@ -9924,7 +9928,7 @@
* // => 'busy' * // => 'busy'
*/ */
function result(object, path, defaultValue) { function result(object, path, defaultValue) {
if (object != null && !(isKey(path) || (path in toObject(object)))) { if (!isKey(path, object)) {
path = toPath(path); path = toPath(path);
object = baseGet(object, baseSlice(path, 0, -1)); object = baseGet(object, baseSlice(path, 0, -1));
path = last(path); path = last(path);
@@ -9963,7 +9967,7 @@
if (object == null) { if (object == null) {
return object; return object;
} }
if (isKey(path) || (path in toObject(object))) { if (isKey(path, object)) {
object[path] = value; object[path] = value;
return object; return object;
} }
@@ -11247,11 +11251,8 @@
}); });
var methodOf = restParam(function(object, args) { var methodOf = restParam(function(object, args) {
if (object == null) {
return constant(undefined);
}
return function(path) { return function(path) {
return baseMethod(object, path, args); return object == null ? undefined : baseMethod(object, path, args);
}; };
}); });
@@ -11430,13 +11431,9 @@
* // => ['b', 'c', 'a'] * // => ['b', 'c', 'a']
*/ */
function propertyOf(object) { function propertyOf(object) {
if (object == null) {
return constant(undefined);
}
return function(path) { return function(path) {
return (isKey(path) || (path in toObject(object))) var pathKey = isArray(path) ? undefined : (path + '');
? object[path] return baseGet(object, toPath(path), pathKey);
: baseGet(object, toPath(path));
}; };
} }