mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-02 16:17:50 +00:00
50 lines
1.6 KiB
JavaScript
50 lines
1.6 KiB
JavaScript
/**
|
|
* lodash 3.0.0 (Custom Build) <https://lodash.com/>
|
|
* Build: `lodash modern modularize exports="npm" -o ./`
|
|
* Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
|
|
* Based on Underscore.js 1.7.0 <http://underscorejs.org/LICENSE>
|
|
* Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
|
* Available under MIT license <https://lodash.com/license>
|
|
*/
|
|
var isFunction = require('lodash.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;
|
|
}
|
|
|
|
module.exports = result;
|