1 var baseToPath = require('./_baseToPath'),
2 get = require('./get'),
3 isFunction = require('./isFunction'),
4 isKey = require('./_isKey'),
5 parent = require('./_parent');
8 * This method is like `_.get` except that if the resolved value is a function
9 * it's invoked with the `this` binding of its parent object and its result
15 * @param {Object} object The object to query.
16 * @param {Array|string} path The path of the property to resolve.
17 * @param {*} [defaultValue] The value returned if the resolved value is `undefined`.
18 * @returns {*} Returns the resolved value.
21 * var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };
23 * _.result(object, 'a[0].b.c1');
26 * _.result(object, 'a[0].b.c2');
29 * _.result(object, 'a[0].b.c3', 'default');
32 * _.result(object, 'a[0].b.c3', _.constant('default'));
35 function result(object, path, defaultValue) {
36 if (!isKey(path, object)) {
37 path = baseToPath(path);
38 var result = get(object, path);
39 object = parent(object, path);
41 result = object == null ? undefined : object[path];
43 if (result === undefined) {
44 result = defaultValue;
46 return isFunction(result) ? result.call(object) : result;
49 module.exports = result;