Skip to content

Commit

Permalink
feat(lens): viewOr implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkuk committed Aug 14, 2017
1 parent 1c351c2 commit f494dc1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,14 @@ declare namespace RamdaAdjunct {
lensNotSatisfies(predicate: Function, lens: Function): (data: any) => Boolean
lensNotSatisfies(predicate: Function): (lens: Function) => (data: any) => Boolean

/**
* Applies lens to given data structure and returns view value if present.
* Otherwise returns `defaultValue`.
*/
viewOr(defaultValue: any, lens: Function, data: any): any
viewOr(defaultValue: any, lens: Function): (data: any) => any
viewOr(defaultValue: any): (lens: Function) => (data: any) => any

/**
* Identity type.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ import lensEq from './lensEq';
import lensNotEq from './lensNotEq';
import lensSatisfies from './lensSatisfies';
import lensNotSatisfies from './lensNotSatisfies';
// Lens
import viewOr from './viewOr';

// Type
export { default as isNotUndefined } from './isNotUndefined';
Expand Down Expand Up @@ -145,6 +147,8 @@ export { default as lensEq } from './lensEq';
export { default as lensNotEq } from './lensNotEq';
export { default as lensSatisfies } from './lensSatisfies';
export { default as lensNotSatisfies } from './lensNotSatisfies';
// Lens
export { default as viewOr } from './viewOr';

/**
* @namespace RA
Expand Down Expand Up @@ -228,6 +232,8 @@ const RA = {
lensNotEq,
lensSatisfies,
lensNotSatisfies,
// Lens
viewOr,
};

export default RA;
28 changes: 28 additions & 0 deletions src/viewOr.js
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;
17 changes: 17 additions & 0 deletions test/viewOr.js
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');
});
});

0 comments on commit f494dc1

Please sign in to comment.