Move the isKey check into castPath.

This commit is contained in:
John-David Dalton
2016-11-14 15:37:46 -08:00
parent e344b66af9
commit 058361e222

View File

@@ -3056,7 +3056,7 @@
* @returns {*} Returns the resolved value. * @returns {*} Returns the resolved value.
*/ */
function baseGet(object, path) { function baseGet(object, path) {
path = isKey(path, object) ? [path] : castPath(path); path = castPath(path, object);
var index = 0, var index = 0,
length = path.length; length = path.length;
@@ -3242,12 +3242,9 @@
* @returns {*} Returns the result of the invoked method. * @returns {*} Returns the result of the invoked method.
*/ */
function baseInvoke(object, path, args) { function baseInvoke(object, path, args) {
if (!isKey(path, object)) { path = castPath(path, object);
path = castPath(path); object = parent(object, path);
object = parent(object, path); var func = object == null ? object : object[toKey(last(path))];
path = last(path);
}
var func = object == null ? object : object[toKey(path)];
return func == null ? undefined : apply(func, object, args); return func == null ? undefined : apply(func, object, args);
} }
@@ -3885,17 +3882,14 @@
if (isIndex(index)) { if (isIndex(index)) {
splice.call(array, index, 1); splice.call(array, index, 1);
} }
else if (!isKey(index, array)) { else {
var path = castPath(index), var path = castPath(index, array),
object = parent(array, path); object = parent(array, path);
if (object != null) { if (object != null) {
delete object[toKey(last(path))]; delete object[toKey(last(path))];
} }
} }
else {
delete array[toKey(index)];
}
} }
} }
return array; return array;
@@ -4015,7 +4009,7 @@
if (!isObject(object)) { if (!isObject(object)) {
return object; return object;
} }
path = isKey(path, object) ? [path] : castPath(path); path = castPath(path, object);
var index = -1, var index = -1,
length = path.length, length = path.length,
@@ -4356,9 +4350,8 @@
* @returns {boolean} Returns `true` if the property is deleted, else `false`. * @returns {boolean} Returns `true` if the property is deleted, else `false`.
*/ */
function baseUnset(object, path) { function baseUnset(object, path) {
path = isKey(path, object) ? [path] : castPath(path); path = castPath(path, object);
object = parent(object, path); object = parent(object, path);
var key = toKey(last(path)); var key = toKey(last(path));
return !(object != null && hasOwnProperty.call(object, key)) || delete object[key]; return !(object != null && hasOwnProperty.call(object, key)) || delete object[key];
} }
@@ -4500,10 +4493,14 @@
* *
* @private * @private
* @param {*} value The value to inspect. * @param {*} value The value to inspect.
* @param {Object} [object] The object to query keys on.
* @returns {Array} Returns the cast property path array. * @returns {Array} Returns the cast property path array.
*/ */
function castPath(value) { function castPath(value, object) {
return isArray(value) ? value : stringToPath(toString(value)); if (isArray(value)) {
return value;
}
return isKey(value, object) ? [value] : stringToPath(toString(value));
} }
/** /**
@@ -6128,7 +6125,7 @@
* @returns {boolean} Returns `true` if `path` exists, else `false`. * @returns {boolean} Returns `true` if `path` exists, else `false`.
*/ */
function hasPath(object, path, hasFunc) { function hasPath(object, path, hasFunc) {
path = isKey(path, object) ? [path] : castPath(path); path = castPath(path, object);
var index = -1, var index = -1,
length = path.length, length = path.length,
@@ -6605,7 +6602,7 @@
* @returns {*} Returns the parent value. * @returns {*} Returns the parent value.
*/ */
function parent(object, path) { function parent(object, path) {
return path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1)); return path.length < 2 ? object : baseGet(object, baseSlice(path, 0, -1));
} }
/** /**
@@ -13477,7 +13474,7 @@
} }
var bitmask = CLONE_FLAT_FLAG | CLONE_SYMBOLS_FLAG; var bitmask = CLONE_FLAT_FLAG | CLONE_SYMBOLS_FLAG;
paths = arrayMap(paths, function(path) { paths = arrayMap(paths, function(path) {
path = toPath(path); path = castPath(path, object);
bitmask |= (path.length > 1 ? CLONE_DEEP_FLAG : 0); bitmask |= (path.length > 1 ? CLONE_DEEP_FLAG : 0);
return path; return path;
}); });
@@ -13598,7 +13595,7 @@
* // => 'default' * // => 'default'
*/ */
function result(object, path, defaultValue) { function result(object, path, defaultValue) {
path = isKey(path, object) ? [path] : castPath(path); path = castPath(path, object);
var index = -1, var index = -1,
length = path.length; length = path.length;