diff --git a/doc/README.md b/doc/README.md
index bcf25b4f3..dc88ec0d6 100644
--- a/doc/README.md
+++ b/doc/README.md
@@ -197,7 +197,7 @@
* `_.parseInt`
* `_.property`
* `_.random`
-* `_.result`
+* `_.result`
* `_.runInContext`
* `_.times`
* `_.uniqueId`
@@ -4637,14 +4637,15 @@ _.random(1.2, 5.2);
-### `_.result(object, key)`
-# [Ⓢ](https://github.com/lodash/lodash/blob/master/lodash.js#L7273 "View in source") [Ⓣ][1]
+### `_.result(object, key, [defaultValue])`
+# [Ⓢ](https://github.com/lodash/lodash/blob/master/lodash.js#L7279 "View in source") [Ⓣ][1]
-Resolves the value of property `key` on `object`. If `key` is a function it will be invoked with the `this` binding of `object` and its result returned, else the property value is returned. If `object` is falsey then `undefined` is returned.
+Resolves the value of property `key` on `object`. If `key` is a function it will be invoked with the `this` binding of `object` and its result returned, else the property value is returned. If `object` is `null` or `undefined` then `undefined` is returned. If a default value is provided it will be returned if the property value resolves to `undefined`.
#### Arguments
1. `object` *(Object)*: The object to inspect.
2. `key` *(string)*: The name of the property to resolve.
+3. `[defaultValue]` *(*)*: The value returned if the property value resolves to `undefined`.
#### Returns
*(*)*: Returns the resolved value.
@@ -4652,17 +4653,20 @@ Resolves the value of property `key` on `object`. If `key` is a function it will
#### Example
```js
var object = {
- 'cheese': 'crumpets',
- 'stuff': function() {
- return 'nonsense';
+ 'name': 'fred',
+ 'age': function() {
+ return 40;
}
};
-_.result(object, 'cheese');
-// => 'crumpets'
+_.result(object, 'name');
+// => 'fred'
-_.result(object, 'stuff');
-// => 'nonsense'
+_.result(object, 'age');
+// => 40
+
+_.result(object, 'employer', 'slate');
+// => 'slate'
```
* * *
@@ -4691,7 +4695,7 @@ Create a new `lodash` function using the given context object.
### `_.times(n, callback, [thisArg])`
-# [Ⓢ](https://github.com/lodash/lodash/blob/master/lodash.js#L7303 "View in source") [Ⓣ][1]
+# [Ⓢ](https://github.com/lodash/lodash/blob/master/lodash.js#L7310 "View in source") [Ⓣ][1]
Executes the callback `n` times, returning an array of the results of each callback execution. The callback is bound to `thisArg` and invoked with one argument; *(index)*.
@@ -4723,7 +4727,7 @@ _.times(3, function(n) { this.cast(n); }, mage);
### `_.uniqueId([prefix])`
-# [Ⓢ](https://github.com/lodash/lodash/blob/master/lodash.js#L7331 "View in source") [Ⓣ][1]
+# [Ⓢ](https://github.com/lodash/lodash/blob/master/lodash.js#L7338 "View in source") [Ⓣ][1]
Generates a unique ID. If `prefix` is provided the ID will be appended to it.
@@ -4776,7 +4780,7 @@ A reference to the `lodash` function.
### `_.VERSION`
-# [Ⓢ](https://github.com/lodash/lodash/blob/master/lodash.js#L7538 "View in source") [Ⓣ][1]
+# [Ⓢ](https://github.com/lodash/lodash/blob/master/lodash.js#L7545 "View in source") [Ⓣ][1]
*(string)*: The semantic version number.
diff --git a/lodash.js b/lodash.js
index 2f99ce63c..5bbf57135 100644
--- a/lodash.js
+++ b/lodash.js
@@ -7245,40 +7245,43 @@
/**
* Resolves the value of property `key` on `object`. If `key` is a function
- * it will be invoked with the `this` binding of `object` and its result returned,
- * else the property value is returned. If `object` is falsey then `undefined`
- * is returned. If property `key` is not set, return defaultValue if defined;
+ * it will be invoked with the `this` binding of `object` and its result
+ * returned, else the property value is returned. If `object` is `null` or
+ * `undefined` then `undefined` is returned. If a default value is provided
+ * it will be returned if the property value resolves to `undefined`.
*
* @static
* @memberOf _
* @category Utilities
* @param {Object} object The object to inspect.
* @param {string} key The name of the property to resolve.
- * @param {*} [defaultValue] The value to return if object doesn't have key.
+ * @param {*} [defaultValue] The value returned if the property value
+ * resolves to `undefined`.
* @returns {*} Returns the resolved value.
* @example
*
* var object = {
- * 'cheese': 'crumpets',
- * 'stuff': function() {
- * return 'nonsense';
+ * 'name': 'fred',
+ * 'age': function() {
+ * return 40;
* }
* };
*
- * _.result(object, 'cheese');
- * // => 'crumpets'
+ * _.result(object, 'name');
+ * // => 'fred'
*
- * _.result(object, 'stuff');
- * // => 'nonsense'
-
- * _.result(object, 'pizza', 'spaghetti');
- * // => 'spaghetti'
+ * _.result(object, 'age');
+ * // => 40
+ *
+ * _.result(object, 'employer', 'slate');
+ * // => 'slate'
*/
function result(object, key, defaultValue) {
- if (object) {
- var value = typeof object[key] !== 'undefined' ? object[key] : defaultValue;
- return isFunction(value) ? object[key]() : value;
+ if (object == null) {
+ return defaultValue;
}
+ var value = typeof object[key] != 'undefined' ? object[key] : defaultValue;
+ return isFunction(value) ? object[key]() : value;
}
/**
diff --git a/test/test.js b/test/test.js
index e7f675733..85cae1e03 100644
--- a/test/test.js
+++ b/test/test.js
@@ -6575,42 +6575,28 @@
QUnit.module('lodash.result');
(function() {
- test('should resolve property values', 4, function() {
- var object = {
- 'a': 1,
- 'b': 2,
- 'c': function(){ return this.b; }
- };
+ var object = {
+ 'a': 1,
+ 'b': 2,
+ 'c': function(){ return this.b; }
+ };
+ test('should resolve property values', 4, function() {
strictEqual(_.result(object, 'a'), 1);
strictEqual(_.result(object, 'b'), 2);
strictEqual(_.result(object, 'c'), 2);
strictEqual(_.result(object, 'd'), undefined);
});
- test('should return `undefined` when provided a falsey `object` argument', 1, function() {
- var actual = [],
- expected = _.map(falsey, function() { return undefined; });
-
- _.forEach(falsey, function(value, index) {
- try {
- actual.push(index ? _.result(value) : _.result());
- } catch(e) { }
- });
-
- deepEqual(actual, expected);
+ test('should return `undefined` when `object` is `null` or `undefined`', 2, function() {
+ strictEqual(_.result(null, 'a'), undefined);
+ strictEqual(_.result(undefined, 'a'), undefined);
});
- test('should return the default value if key is not found', 2, function() {
- var object = {
- 'a': 1,
- 'b': 2
- }
-
- strictEqual(_.result(object, 'c', 3), 3);
- strictEqual(_.result(object, 'c'), undefined);
+ test('should return the specified default value for undefined properties', 2, function() {
+ strictEqual(_.result(object, 'd', 3), 3);
+ strictEqual(_.result(null, 'd', 3), 3);
});
-
}());
/*--------------------------------------------------------------------------*/