55a2ddb369436b2410f92a0581ed99c570ddab87
[yaffs-website] / node_modules / uncss / node_modules / lodash / result.js
1 var baseToPath = require('./internal/baseToPath'),
2     get = require('./get'),
3     isFunction = require('./isFunction'),
4     isKey = require('./internal/isKey'),
5     parent = require('./internal/parent');
6
7 /**
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
10  * is returned.
11  *
12  * @static
13  * @memberOf _
14  * @category Object
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.
19  * @example
20  *
21  * var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };
22  *
23  * _.result(object, 'a[0].b.c1');
24  * // => 3
25  *
26  * _.result(object, 'a[0].b.c2');
27  * // => 4
28  *
29  * _.result(object, 'a[0].b.c3', 'default');
30  * // => 'default'
31  *
32  * _.result(object, 'a[0].b.c3', _.constant('default'));
33  * // => 'default'
34  */
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);
40   } else {
41     result = object == null ? undefined : object[path];
42   }
43   if (result === undefined) {
44     result = defaultValue;
45   }
46   return isFunction(result) ? result.call(object) : result;
47 }
48
49 module.exports = result;