-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c351c2
commit f494dc1
Showing
4 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { view, defaultTo, curry } from 'ramda'; | ||
|
||
/** | ||
* If the data structure "viewed" through `lens` contains value, the value is returned. | ||
* Otherwise returns the provided `defaultValue`. | ||
* | ||
* @func viewOr | ||
* @memberOf RA | ||
* @since {@link https://char0n.github.io/ramda-adjunct/1.13.0|1.13.0} | ||
* @category Lens | ||
* @sig * -> lens -> data -> * | ||
* @param {*} defaultValue Defauly value | ||
* @param {Function} lens Van Laarhoven lens | ||
* @param {*} data The data structure to apply the lens to | ||
* | ||
* @example | ||
* | ||
* RA.viewOr('N/A', R.lensProp('x'), {}); // => 'N/A' | ||
* RA.viewOr('N/A', R.lensProp('x'), { x: 1 }); // => 1 | ||
* RA.viewOr('some', R.lensProp('y'), { y: null }); // => 'some' | ||
* RA.viewOr('some', R.lensProp('y'), { y: false }); // => false | ||
*/ | ||
|
||
const viewOr = curry( | ||
(defaultValue, lens, data) => defaultTo(defaultValue, view(lens, data)) | ||
); | ||
|
||
export default viewOr; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { lensIndex, lensProp } from 'ramda'; | ||
import RA from '../src/index'; | ||
import eq from './shared/eq'; | ||
|
||
describe('viewOr', function () { | ||
it('tests "view" through lens with fallback to default value', function () { | ||
eq(RA.viewOr('foo', lensProp('bar'), {}), 'foo'); | ||
eq(RA.viewOr('foo', lensProp('bar'), { bar: 'foobar' }), 'foobar'); | ||
|
||
eq(RA.viewOr('foo', lensProp('bar'), { bar: undefined }), 'foo'); | ||
eq(RA.viewOr('foo', lensProp('bar'), { bar: null }), 'foo'); | ||
eq(RA.viewOr('foo', lensProp('bar'), { bar: false }), false); | ||
|
||
eq(RA.viewOr('foo', lensIndex(11), []), 'foo'); | ||
eq(RA.viewOr('foo', lensIndex(11), {}), 'foo'); | ||
}); | ||
}); |