result.js
1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
define(['../internal/baseGet', '../internal/baseSlice', '../lang/isFunction', '../internal/isKey', '../array/last', '../internal/toObject', '../internal/toPath'], function(baseGet, baseSlice, isFunction, isKey, last, toObject, toPath) {
/** Used as a safe reference for `undefined` in pre-ES5 environments. */
var undefined;
/**
* This method is like `_.get` except that if the resolved value is a function
* it's invoked with the `this` binding of its parent object and its result
* is returned.
*
* @static
* @memberOf _
* @category Object
* @param {Object} object The object to query.
* @param {Array|string} path The path of the property to resolve.
* @param {*} [defaultValue] The value returned if the resolved value is `undefined`.
* @returns {*} Returns the resolved value.
* @example
*
* var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };
*
* _.result(object, 'a[0].b.c1');
* // => 3
*
* _.result(object, 'a[0].b.c2');
* // => 4
*
* _.result(object, 'a.b.c', 'default');
* // => 'default'
*
* _.result(object, 'a.b.c', _.constant('default'));
* // => 'default'
*/
function result(object, path, defaultValue) {
var result = object == null ? undefined : toObject(object)[path];
if (result === undefined) {
if (object != null && !isKey(path, object)) {
path = toPath(path);
object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
result = object == null ? undefined : toObject(object)[last(path)];
}
result = result === undefined ? defaultValue : result;
}
return isFunction(result) ? result.call(object) : result;
}
return result;
});