mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 07:47:49 +00:00
Ensure functions can be used as default values for _.result.
This commit is contained in:
@@ -7338,10 +7338,10 @@
|
||||
* // => 'slate'
|
||||
*/
|
||||
function result(object, key, defaultValue) {
|
||||
if (object == null) {
|
||||
if (object == null || typeof object[key] == 'undefined') {
|
||||
return defaultValue;
|
||||
}
|
||||
var value = typeof object[key] != 'undefined' ? object[key] : defaultValue;
|
||||
var value = object[key];
|
||||
return isFunction(value) ? object[key]() : value;
|
||||
}
|
||||
|
||||
|
||||
26
test/test.js
26
test/test.js
@@ -6702,14 +6702,14 @@
|
||||
(function() {
|
||||
var object = {
|
||||
'a': 1,
|
||||
'b': 2,
|
||||
'c': function(){ return this.b; }
|
||||
'b': null,
|
||||
'c': function() { return this.a; }
|
||||
};
|
||||
|
||||
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, 'b'), null);
|
||||
strictEqual(_.result(object, 'c'), 1);
|
||||
strictEqual(_.result(object, 'd'), undefined);
|
||||
});
|
||||
|
||||
@@ -6718,9 +6718,21 @@
|
||||
strictEqual(_.result(undefined, 'a'), 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);
|
||||
test('should return the specified default value for undefined properties', 1, function() {
|
||||
var values = falsey.concat(1, function() { return 1; });
|
||||
|
||||
var expected = _.transform(values, function(result, value) {
|
||||
result.push(value, value);
|
||||
});
|
||||
|
||||
var actual = _.transform(values, function(result, value) {
|
||||
result.push(
|
||||
_.result(object, 'd', value),
|
||||
_.result(null, 'd', value)
|
||||
);
|
||||
});
|
||||
|
||||
deepEqual(actual, expected);
|
||||
});
|
||||
}());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user