mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 01:47:48 +00:00
Bump to v4.0.0.
This commit is contained in:
49
result.js
Normal file
49
result.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import baseToPath from './internal/baseToPath';
|
||||
import get from './get';
|
||||
import isFunction from './isFunction';
|
||||
import isKey from './internal/isKey';
|
||||
import parent from './internal/parent';
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @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`.
|
||||
* @returns {*} Returns the resolved value.
|
||||
* @example
|
||||
*
|
||||
* var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };
|
||||
*
|
||||
* _.result(object, 'a[0].b.c1');
|
||||
* // => 3
|
||||
*
|
||||
* _.result(object, 'a[0].b.c2');
|
||||
* // => 4
|
||||
*
|
||||
* _.result(object, 'a[0].b.c3', 'default');
|
||||
* // => 'default'
|
||||
*
|
||||
* _.result(object, 'a[0].b.c3', _.constant('default'));
|
||||
* // => 'default'
|
||||
*/
|
||||
function result(object, path, defaultValue) {
|
||||
if (!isKey(path, object)) {
|
||||
path = baseToPath(path);
|
||||
var result = get(object, path);
|
||||
object = parent(object, path);
|
||||
} else {
|
||||
result = object == null ? undefined : object[path];
|
||||
}
|
||||
if (result === undefined) {
|
||||
result = defaultValue;
|
||||
}
|
||||
return isFunction(result) ? result.call(object) : result;
|
||||
}
|
||||
|
||||
export default result;
|
||||
Reference in New Issue
Block a user