mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 03:17:49 +00:00
Cleanup _.result default value patch. [closes #455]
This commit is contained in:
@@ -197,7 +197,7 @@
|
|||||||
* <a href="#_parseintvalue-radix">`_.parseInt`</a>
|
* <a href="#_parseintvalue-radix">`_.parseInt`</a>
|
||||||
* <a href="#_propertykey">`_.property`</a>
|
* <a href="#_propertykey">`_.property`</a>
|
||||||
* <a href="#_randommin0-max1-floatingfalse">`_.random`</a>
|
* <a href="#_randommin0-max1-floatingfalse">`_.random`</a>
|
||||||
* <a href="#_resultobject-key">`_.result`</a>
|
* <a href="#_resultobject-key-defaultvalue">`_.result`</a>
|
||||||
* <a href="#_runincontextcontextroot">`_.runInContext`</a>
|
* <a href="#_runincontextcontextroot">`_.runInContext`</a>
|
||||||
* <a href="#_timesn-callback-thisarg">`_.times`</a>
|
* <a href="#_timesn-callback-thisarg">`_.times`</a>
|
||||||
* <a href="#_uniqueidprefix">`_.uniqueId`</a>
|
* <a href="#_uniqueidprefix">`_.uniqueId`</a>
|
||||||
@@ -4637,14 +4637,15 @@ _.random(1.2, 5.2);
|
|||||||
|
|
||||||
<!-- div -->
|
<!-- div -->
|
||||||
|
|
||||||
### <a id="_resultobject-key"></a>`_.result(object, key)`
|
### <a id="_resultobject-key-defaultvalue"></a>`_.result(object, key, [defaultValue])`
|
||||||
<a href="#_resultobject-key">#</a> [Ⓢ](https://github.com/lodash/lodash/blob/master/lodash.js#L7273 "View in source") [Ⓣ][1]
|
<a href="#_resultobject-key-defaultvalue">#</a> [Ⓢ](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
|
#### Arguments
|
||||||
1. `object` *(Object)*: The object to inspect.
|
1. `object` *(Object)*: The object to inspect.
|
||||||
2. `key` *(string)*: The name of the property to resolve.
|
2. `key` *(string)*: The name of the property to resolve.
|
||||||
|
3. `[defaultValue]` *(*)*: The value returned if the property value resolves to `undefined`.
|
||||||
|
|
||||||
#### Returns
|
#### Returns
|
||||||
*(*)*: Returns the resolved value.
|
*(*)*: Returns the resolved value.
|
||||||
@@ -4652,17 +4653,20 @@ Resolves the value of property `key` on `object`. If `key` is a function it will
|
|||||||
#### Example
|
#### Example
|
||||||
```js
|
```js
|
||||||
var object = {
|
var object = {
|
||||||
'cheese': 'crumpets',
|
'name': 'fred',
|
||||||
'stuff': function() {
|
'age': function() {
|
||||||
return 'nonsense';
|
return 40;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
_.result(object, 'cheese');
|
_.result(object, 'name');
|
||||||
// => 'crumpets'
|
// => 'fred'
|
||||||
|
|
||||||
_.result(object, 'stuff');
|
_.result(object, 'age');
|
||||||
// => 'nonsense'
|
// => 40
|
||||||
|
|
||||||
|
_.result(object, 'employer', 'slate');
|
||||||
|
// => 'slate'
|
||||||
```
|
```
|
||||||
|
|
||||||
* * *
|
* * *
|
||||||
@@ -4691,7 +4695,7 @@ Create a new `lodash` function using the given context object.
|
|||||||
<!-- div -->
|
<!-- div -->
|
||||||
|
|
||||||
### <a id="_timesn-callback-thisarg"></a>`_.times(n, callback, [thisArg])`
|
### <a id="_timesn-callback-thisarg"></a>`_.times(n, callback, [thisArg])`
|
||||||
<a href="#_timesn-callback-thisarg">#</a> [Ⓢ](https://github.com/lodash/lodash/blob/master/lodash.js#L7303 "View in source") [Ⓣ][1]
|
<a href="#_timesn-callback-thisarg">#</a> [Ⓢ](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)*.
|
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);
|
|||||||
<!-- div -->
|
<!-- div -->
|
||||||
|
|
||||||
### <a id="_uniqueidprefix"></a>`_.uniqueId([prefix])`
|
### <a id="_uniqueidprefix"></a>`_.uniqueId([prefix])`
|
||||||
<a href="#_uniqueidprefix">#</a> [Ⓢ](https://github.com/lodash/lodash/blob/master/lodash.js#L7331 "View in source") [Ⓣ][1]
|
<a href="#_uniqueidprefix">#</a> [Ⓢ](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.
|
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.
|
|||||||
<!-- div -->
|
<!-- div -->
|
||||||
|
|
||||||
### <a id="_version"></a>`_.VERSION`
|
### <a id="_version"></a>`_.VERSION`
|
||||||
<a href="#_version">#</a> [Ⓢ](https://github.com/lodash/lodash/blob/master/lodash.js#L7538 "View in source") [Ⓣ][1]
|
<a href="#_version">#</a> [Ⓢ](https://github.com/lodash/lodash/blob/master/lodash.js#L7545 "View in source") [Ⓣ][1]
|
||||||
|
|
||||||
*(string)*: The semantic version number.
|
*(string)*: The semantic version number.
|
||||||
|
|
||||||
|
|||||||
37
lodash.js
37
lodash.js
@@ -7245,40 +7245,43 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolves the value of property `key` on `object`. If `key` is a function
|
* 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,
|
* it will be invoked with the `this` binding of `object` and its result
|
||||||
* else the property value is returned. If `object` is falsey then `undefined`
|
* returned, else the property value is returned. If `object` is `null` or
|
||||||
* is returned. If property `key` is not set, return defaultValue if defined;
|
* `undefined` then `undefined` is returned. If a default value is provided
|
||||||
|
* it will be returned if the property value resolves to `undefined`.
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @category Utilities
|
* @category Utilities
|
||||||
* @param {Object} object The object to inspect.
|
* @param {Object} object The object to inspect.
|
||||||
* @param {string} key The name of the property to resolve.
|
* @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.
|
* @returns {*} Returns the resolved value.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* var object = {
|
* var object = {
|
||||||
* 'cheese': 'crumpets',
|
* 'name': 'fred',
|
||||||
* 'stuff': function() {
|
* 'age': function() {
|
||||||
* return 'nonsense';
|
* return 40;
|
||||||
* }
|
* }
|
||||||
* };
|
* };
|
||||||
*
|
*
|
||||||
* _.result(object, 'cheese');
|
* _.result(object, 'name');
|
||||||
* // => 'crumpets'
|
* // => 'fred'
|
||||||
*
|
*
|
||||||
* _.result(object, 'stuff');
|
* _.result(object, 'age');
|
||||||
* // => 'nonsense'
|
* // => 40
|
||||||
|
*
|
||||||
* _.result(object, 'pizza', 'spaghetti');
|
* _.result(object, 'employer', 'slate');
|
||||||
* // => 'spaghetti'
|
* // => 'slate'
|
||||||
*/
|
*/
|
||||||
function result(object, key, defaultValue) {
|
function result(object, key, defaultValue) {
|
||||||
if (object) {
|
if (object == null) {
|
||||||
var value = typeof object[key] !== 'undefined' ? object[key] : defaultValue;
|
return defaultValue;
|
||||||
return isFunction(value) ? object[key]() : value;
|
|
||||||
}
|
}
|
||||||
|
var value = typeof object[key] != 'undefined' ? object[key] : defaultValue;
|
||||||
|
return isFunction(value) ? object[key]() : value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
38
test/test.js
38
test/test.js
@@ -6575,42 +6575,28 @@
|
|||||||
QUnit.module('lodash.result');
|
QUnit.module('lodash.result');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
test('should resolve property values', 4, function() {
|
var object = {
|
||||||
var object = {
|
'a': 1,
|
||||||
'a': 1,
|
'b': 2,
|
||||||
'b': 2,
|
'c': function(){ return this.b; }
|
||||||
'c': function(){ return this.b; }
|
};
|
||||||
};
|
|
||||||
|
|
||||||
|
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'), 2);
|
||||||
strictEqual(_.result(object, 'c'), 2);
|
strictEqual(_.result(object, 'c'), 2);
|
||||||
strictEqual(_.result(object, 'd'), undefined);
|
strictEqual(_.result(object, 'd'), undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return `undefined` when provided a falsey `object` argument', 1, function() {
|
test('should return `undefined` when `object` is `null` or `undefined`', 2, function() {
|
||||||
var actual = [],
|
strictEqual(_.result(null, 'a'), undefined);
|
||||||
expected = _.map(falsey, function() { return undefined; });
|
strictEqual(_.result(undefined, 'a'), undefined);
|
||||||
|
|
||||||
_.forEach(falsey, function(value, index) {
|
|
||||||
try {
|
|
||||||
actual.push(index ? _.result(value) : _.result());
|
|
||||||
} catch(e) { }
|
|
||||||
});
|
|
||||||
|
|
||||||
deepEqual(actual, expected);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return the default value if key is not found', 2, function() {
|
test('should return the specified default value for undefined properties', 2, function() {
|
||||||
var object = {
|
strictEqual(_.result(object, 'd', 3), 3);
|
||||||
'a': 1,
|
strictEqual(_.result(null, 'd', 3), 3);
|
||||||
'b': 2
|
|
||||||
}
|
|
||||||
|
|
||||||
strictEqual(_.result(object, 'c', 3), 3);
|
|
||||||
strictEqual(_.result(object, 'c'), undefined);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|||||||
Reference in New Issue
Block a user