[d24f17c] | 1 | import { view, defaultTo, curryN } from 'ramda';
|
---|
| 2 |
|
---|
| 3 | /**
|
---|
| 4 | * Returns a "view" of the given data structure, determined by the given lens.
|
---|
| 5 | * The lens's focus determines which portion of the data structure is visible.
|
---|
| 6 | * Returns the defaultValue if "view" is null, undefined or NaN; otherwise the "view" is returned.
|
---|
| 7 | *
|
---|
| 8 | * @func viewOr
|
---|
| 9 | * @memberOf RA
|
---|
| 10 | * @since {@link https://char0n.github.io/ramda-adjunct/1.13.0|1.13.0}
|
---|
| 11 | * @category Object
|
---|
| 12 | * @typedef Lens s b = Functor f => (b -> f b) -> s -> f s
|
---|
| 13 | * @sig a -> Lens s b -> s -> b | a
|
---|
| 14 | * @see {@link http://ramdajs.com/docs/#view|R.view}
|
---|
| 15 | * @param {*} defaultValue The default value
|
---|
| 16 | * @param {Function} lens Van Laarhoven lens
|
---|
| 17 | * @param {*} data The data structure
|
---|
| 18 | * @returns {*} "view" or defaultValue
|
---|
| 19 | *
|
---|
| 20 | * @example
|
---|
| 21 | *
|
---|
| 22 | * RA.viewOr('N/A', R.lensProp('x'), {}); // => 'N/A'
|
---|
| 23 | * RA.viewOr('N/A', R.lensProp('x'), { x: 1 }); // => 1
|
---|
| 24 | * RA.viewOr('some', R.lensProp('y'), { y: null }); // => 'some'
|
---|
| 25 | * RA.viewOr('some', R.lensProp('y'), { y: false }); // => false
|
---|
| 26 | */
|
---|
| 27 |
|
---|
| 28 | const viewOr = curryN(3, (defaultValue, lens, data) =>
|
---|
| 29 | defaultTo(defaultValue, view(lens, data))
|
---|
| 30 | );
|
---|
| 31 |
|
---|
| 32 | export default viewOr;
|
---|