react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / node_modules / argparse / node_modules / lodash / object / result.js
80742 viewsvar baseGet = require('../internal/baseGet'),1baseSlice = require('../internal/baseSlice'),2isFunction = require('../lang/isFunction'),3isKey = require('../internal/isKey'),4last = require('../array/last'),5toPath = require('../internal/toPath');67/**8* This method is like `_.get` except that if the resolved value is a function9* it is invoked with the `this` binding of its parent object and its result10* is returned.11*12* @static13* @memberOf _14* @category Object15* @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* @example20*21* var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };22*23* _.result(object, 'a[0].b.c1');24* // => 325*26* _.result(object, 'a[0].b.c2');27* // => 428*29* _.result(object, 'a.b.c', 'default');30* // => 'default'31*32* _.result(object, 'a.b.c', _.constant('default'));33* // => 'default'34*/35function result(object, path, defaultValue) {36var result = object == null ? undefined : object[path];37if (result === undefined) {38if (object != null && !isKey(path, object)) {39path = toPath(path);40object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));41result = object == null ? undefined : object[last(path)];42}43result = result === undefined ? defaultValue : result;44}45return isFunction(result) ? result.call(object) : result;46}4748module.exports = result;495051