Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lens): add utils for further lens utilization #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/lens.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
var set = require('ramda/src/set')
var lens = require('ramda/src/lens')
var curry = require('ramda/src/curry')
var view = require('ramda/src/view')
var pipe = require('ramda/src/pipe')
var equals = require('ramda/src/equals')
var defaultTo = require('ramda/src/defaultTo')

var lensEq = curry(function(_lens, val, obj) {
return pipe(view(_lens), equals(val))(obj)
});

var lensSatisfies = curry(function(pred, _lens, obj) {
return pipe(view(_lens), pred, equals(true))(obj)
});

var viewOr = curry(function (defaultValue, _lens, obj) {
return pipe(view(_lens), defaultTo(defaultValue))(obj)
});

module.exports = {
lens: lens,
set: set,
view: require('./view'),
over: require('./over')
viewOr: viewOr,
over: require('./over'),
lensEq: lensEq,
lensSatisfies: lensSatisfies
}
37 changes: 34 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const assert = require("assert")
const assert = require('assert')
const L = require('../index')
const R = require("ramda")
const R = require('ramda')
const compose = R.compose
const lensProp = R.lensProp
const lensIndex = R.lensIndex
const equals = R.equals
const set = L.set
const view = L.view
const viewOr = L.viewOr
const over = L.over
const lensEq = L.lensEq
const lensSatisfies = L.lensSatisfies
const mapped = L.mapped
const traversed = L.traversed
const traverseOf = L.traverseOf
Expand Down Expand Up @@ -35,7 +39,7 @@ describe("Lenses", function() {
const zip = lensProp('zip')


describe("Set/View/Over", function() {
describe("Set/View/ViewOr/Over/LensEq/LensSatisfies", function() {
const firstStreet = compose(_0, addresses, _0, street)

it('gets the value', function() {
Expand All @@ -56,6 +60,33 @@ describe("Lenses", function() {
assert.equal('393 Post Ave.', res[1].addresses[0].street)
assert.equal('92 Oak St.', users[0].addresses[0].street)
})

it('test if object has a specific value on provided lens', function() {
const res = lensEq(firstStreet, '92 Oak St.', users)
assert.ok(res)

const resCurried = lensEq(firstStreet)('92 Oak St.')(users)
assert.ok(resCurried)
})

it('test if specified object property at lens satisfies the given predicate', function() {
const res = lensSatisfies(equals('92 Oak St.'), firstStreet, users)
assert.ok(res)

const resCurried = lensSatisfies(equals('92 Oak St.'))(firstStreet)(users)
assert.ok(resCurried)
})

it('test "view" of the given data structure, determined by the given lens with default value', function() {
const res = viewOr('default street', firstStreet, users)
assert.equal('92 Oak St.', res)

const resDefault = viewOr('default street', lensProp('non-existing-prop'), users)
assert.equal('default street', resDefault)

const resCurried = viewOr('default street')(firstStreet)(users)
assert.equal('92 Oak St.', resCurried)
})
})

describe("Mapping", function() {
Expand Down