Implement defaultToAny method (#3615)

This commit is contained in:
Eran Nussbaum
2018-03-08 18:05:12 +02:00
committed by John-David Dalton
parent c1f805f497
commit f73b35bb49

32
defaultToAny.js Normal file
View File

@@ -0,0 +1,32 @@
import arrayReduce from './.internal/arrayReduce'
import defaultTo from './defaultTo.js'
/**
* This method is like `defaultTo` except that it accepts multiple default values and returns the first one that is not
* `NaN`, `null`, or `undefined`.
*
* @since 5.0.0
* @category Util
* @param {*} value The value to check.
* @param {...*} defaultValues The default values.
* @returns {*} Returns the resolved value.
* @see _.defaultTo
* @example
*
* defaultToAny(1, 10, 20)
* // => 1
*
* defaultToAny(undefined, 10, 20)
* // => 10
*
* defaultToAny(undefined, null, 20)
* // => 20
*
* defaultToAny(undefined, null, NaN)
* // => NaN
*/
function defaultToAny(value, ...defaultValues) {
return arrayReduce(defaultValues, defaultTo, value)
}
export default defaultToAny