Ensure functions can be used as default values for _.result.

This commit is contained in:
John-David Dalton
2014-02-04 23:50:37 -08:00
parent 8498f12a9d
commit de7409fd07
2 changed files with 21 additions and 9 deletions

View File

@@ -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;
}

View File

@@ -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);
});
}());