-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetOr.js
33 lines (32 loc) · 972 Bytes
/
getOr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
var curry3 = require('./internal/curry3')
var get = require('./get')
/**
* Gets value from (nested) path in a collection, falls back on default value.
*
* @param {*} defaultValue - value to return if nothing found at `path`
* @param {(string|string[])} path - dot-string or string-array denoting path
* @param {(Object|Array)} collection - collection to get value from
* @returns {*} Returns value if found, `defaultValue` otherwise
*
* @example
* getOr('foo', 'a.b', { a: { b: 42 } }) // => 42
*
* @example
* getOr('foo', 'a.z', { a: { b: 42 } }) // => foo
*
* @example
* getOr('foo', 'a.b', { a: { b: undefined } }) // => foo
*
* @example
* getOr('foo', ['a', 'b'], { a: { b: 42 } }) // => 42
*
* @example
* getOr('foo', 'a.1', { a: [1, 2] }) // => 2
*
* @since 0.6.0
*/
function getOr (defaultValue, path, collection) {
var result = get(path, collection)
return result === undefined ? defaultValue : result
}
module.exports = curry3(getOr)