UNPKG

1.6 kBJavaScriptView Raw
1/**
2 * lodash 3.0.0 (Custom Build) <https://lodash.com/>
3 * Build: `lodash modern modularize exports="npm" -o ./`
4 * Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
5 * Based on Underscore.js 1.7.0 <http://underscorejs.org/LICENSE>
6 * Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
7 * Available under MIT license <https://lodash.com/license>
8 */
9var isFunction = require('lodash.isfunction');
10
11/**
12 * Resolves the value of property `key` on `object`. If the value of `key` is
13 * a function it is invoked with the `this` binding of `object` and its result
14 * is returned, else the property value is returned. If the property value is
15 * `undefined` the `defaultValue` is used in its place.
16 *
17 * @static
18 * @memberOf _
19 * @category Object
20 * @param {Object} object The object to query.
21 * @param {string} key The key of the property to resolve.
22 * @param {*} [defaultValue] The value returned if the property value
23 * resolves to `undefined`.
24 * @returns {*} Returns the resolved value.
25 * @example
26 *
27 * var object = { 'user': 'fred', 'age': _.constant(40) };
28 *
29 * _.result(object, 'user');
30 * // => 'fred'
31 *
32 * _.result(object, 'age');
33 * // => 40
34 *
35 * _.result(object, 'status', 'busy');
36 * // => 'busy'
37 *
38 * _.result(object, 'status', _.constant('busy'));
39 * // => 'busy'
40 */
41function result(object, key, defaultValue) {
42 var value = object == null ? undefined : object[key];
43 if (typeof value == 'undefined') {
44 value = defaultValue;
45 }
46 return isFunction(value) ? value.call(object) : value;
47}
48
49module.exports = result;