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