mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 09:47:48 +00:00
Bump to v3.0.0.
This commit is contained in:
41
object/result.js
Normal file
41
object/result.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import isFunction from '../lang/isFunction';
|
||||
|
||||
/**
|
||||
* Resolves the value of property `key` on `object`. If the value of `key` is
|
||||
* a function it is invoked with the `this` binding of `object` and its result
|
||||
* is returned, else the property value is returned. If the property value is
|
||||
* `undefined` the `defaultValue` is used in its place.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Object
|
||||
* @param {Object} object The object to query.
|
||||
* @param {string} key The key of the property to resolve.
|
||||
* @param {*} [defaultValue] The value returned if the property value
|
||||
* resolves to `undefined`.
|
||||
* @returns {*} Returns the resolved value.
|
||||
* @example
|
||||
*
|
||||
* var object = { 'user': 'fred', 'age': _.constant(40) };
|
||||
*
|
||||
* _.result(object, 'user');
|
||||
* // => 'fred'
|
||||
*
|
||||
* _.result(object, 'age');
|
||||
* // => 40
|
||||
*
|
||||
* _.result(object, 'status', 'busy');
|
||||
* // => 'busy'
|
||||
*
|
||||
* _.result(object, 'status', _.constant('busy'));
|
||||
* // => 'busy'
|
||||
*/
|
||||
function result(object, key, defaultValue) {
|
||||
var value = object == null ? undefined : object[key];
|
||||
if (typeof value == 'undefined') {
|
||||
value = defaultValue;
|
||||
}
|
||||
return isFunction(value) ? value.call(object) : value;
|
||||
}
|
||||
|
||||
export default result;
|
||||
Reference in New Issue
Block a user