mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 18:37:50 +00:00
Make baseGet align with baseSet.
This commit is contained in:
21
lodash.js
21
lodash.js
@@ -1932,18 +1932,15 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `_.get` without support for default values or
|
* The base implementation of `_.get` without support for default values.
|
||||||
* nullish `object` values.
|
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {Object} object The object to query.
|
* @param {Object} object The object to query.
|
||||||
* @param {Array|string} path The path of the property to get.
|
* @param {Array|string} 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, pathKey) {
|
function baseGet(object, path) {
|
||||||
pathKey = pathKey == null ? (path + '') : pathKey;
|
path = isKey(path, object) ? [path + ''] : toPath(path);
|
||||||
path = isKey(pathKey, object) ? [pathKey] : toPath(path);
|
|
||||||
|
|
||||||
var index = 0,
|
var index = 0,
|
||||||
length = path.length;
|
length = path.length;
|
||||||
@@ -2393,10 +2390,8 @@
|
|||||||
* @returns {Function} Returns the new function.
|
* @returns {Function} Returns the new function.
|
||||||
*/
|
*/
|
||||||
function basePropertyDeep(path) {
|
function basePropertyDeep(path) {
|
||||||
var pathKey = (path + '');
|
|
||||||
path = toPath(path);
|
|
||||||
return function(object) {
|
return function(object) {
|
||||||
return baseGet(object, path, pathKey);
|
return baseGet(object, path);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2472,7 +2467,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `_.set` without support for nullish `object` values.
|
* The base implementation of `_.set`.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {Object} object The object to query.
|
* @param {Object} object The object to query.
|
||||||
@@ -9380,9 +9375,11 @@
|
|||||||
* // => 'default'
|
* // => 'default'
|
||||||
*/
|
*/
|
||||||
function result(object, path, defaultValue) {
|
function result(object, path, defaultValue) {
|
||||||
var result = object == null ? undefined : object[path];
|
var isPath = !isKey(path, object),
|
||||||
|
result = (isPath || object == null) ? undefined : object[path];
|
||||||
|
|
||||||
if (result === undefined) {
|
if (result === undefined) {
|
||||||
if (object != null && !isKey(path, object)) {
|
if (object != null && isPath) {
|
||||||
path = toPath(path);
|
path = toPath(path);
|
||||||
object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
|
object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
|
||||||
result = object == null ? undefined : object[last(path)];
|
result = object == null ? undefined : object[last(path)];
|
||||||
|
|||||||
@@ -12934,9 +12934,9 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('`_.' + methodName + '` should coerce array paths to strings', 1, function() {
|
test('`_.' + methodName + '` should not coerce array paths to strings', 1, function() {
|
||||||
var object = { 'a,b,c': 3, 'a': { 'b': { 'c': 4 } } };
|
var object = { 'a,b,c': 3, 'a': { 'b': { 'c': 4 } } };
|
||||||
strictEqual(func(object, ['a', 'b', 'c']), 3);
|
strictEqual(func(object, ['a', 'b', 'c']), 4);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('`_.' + methodName + '` should ignore empty brackets', 1, function() {
|
test('`_.' + methodName + '` should ignore empty brackets', 1, function() {
|
||||||
@@ -13436,10 +13436,10 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should coerce array paths to strings', 1, function() {
|
test('should not coerce array paths to strings', 1, function() {
|
||||||
var object = { 'a,b,c': 3, 'a': { 'b': { 'c': 3 } } };
|
var object = { 'a,b,c': 3, 'a': { 'b': { 'c': 3 } } };
|
||||||
_.set(object, ['a', 'b', 'c'], 4);
|
_.set(object, ['a', 'b', 'c'], 4);
|
||||||
deepEqual(object, { 'a,b,c': 4, 'a': { 'b': { 'c': 3 } } });
|
strictEqual(object.a.b.c, 4);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should ignore empty brackets', 1, function() {
|
test('should ignore empty brackets', 1, function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user