mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Bump to v4.7.0.
This commit is contained in:
39
result.js
39
result.js
@@ -1,20 +1,19 @@
|
||||
var baseCastPath = require('./_baseCastPath'),
|
||||
get = require('./get'),
|
||||
isFunction = require('./isFunction'),
|
||||
isKey = require('./_isKey'),
|
||||
parent = require('./_parent');
|
||||
isKey = require('./_isKey');
|
||||
|
||||
/**
|
||||
* This method is like `_.get` except that if the resolved value is a function
|
||||
* it's invoked with the `this` binding of its parent object and its result
|
||||
* is returned.
|
||||
* This method is like `_.get` except that if the resolved value is a
|
||||
* function it's invoked with the `this` binding of its parent object and
|
||||
* its result is returned.
|
||||
*
|
||||
* @static
|
||||
* @since 0.1.0
|
||||
* @memberOf _
|
||||
* @category Object
|
||||
* @param {Object} object The object to query.
|
||||
* @param {Array|string} path The path of the property to resolve.
|
||||
* @param {*} [defaultValue] The value returned if the resolved value is `undefined`.
|
||||
* @param {*} [defaultValue] The value returned for `undefined` resolved values.
|
||||
* @returns {*} Returns the resolved value.
|
||||
* @example
|
||||
*
|
||||
@@ -33,17 +32,25 @@ var baseCastPath = require('./_baseCastPath'),
|
||||
* // => 'default'
|
||||
*/
|
||||
function result(object, path, defaultValue) {
|
||||
if (!isKey(path, object)) {
|
||||
path = baseCastPath(path);
|
||||
var result = get(object, path);
|
||||
object = parent(object, path);
|
||||
} else {
|
||||
result = object == null ? undefined : object[path];
|
||||
path = isKey(path, object) ? [path] : baseCastPath(path);
|
||||
|
||||
var index = -1,
|
||||
length = path.length;
|
||||
|
||||
// Ensure the loop is entered when path is empty.
|
||||
if (!length) {
|
||||
object = undefined;
|
||||
length = 1;
|
||||
}
|
||||
if (result === undefined) {
|
||||
result = defaultValue;
|
||||
while (++index < length) {
|
||||
var value = object == null ? undefined : object[path[index]];
|
||||
if (value === undefined) {
|
||||
index = length;
|
||||
value = defaultValue;
|
||||
}
|
||||
object = isFunction(value) ? value.call(object) : value;
|
||||
}
|
||||
return isFunction(result) ? result.call(object) : result;
|
||||
return object;
|
||||
}
|
||||
|
||||
module.exports = result;
|
||||
|
||||
Reference in New Issue
Block a user