Reputation: 101
How i write the code as functional if i has the value like this
const data = {
name: null,
email: null,
username: 'johndoo'
}
in this case if i write the function that return the display name i just wrote data.name || data.email || data.username
I try to use ramda like
const getName = R.prop('name')
const getEmail = R.prop('email')
const getUsername = R.prop('username')
const getDisplayName = getName || getEmail || getUsername
but doesn't work, how to write it in ramdajs
Upvotes: 2
Views: 10417
Reputation: 29167
You need to use a logical or R.either
:
const getDisplayName = R.either(getName, R.either(getEmail, getUsername))
Or more compact with R.reduce
:
const getDisplayName = R.reduce(R.either, R.isNil)([getName, getEmail, getUsername])
const data = {
name: null,
email: null,
username: 'johndoo'
}
const getName = R.prop('name')
const getEmail = R.prop('email')
const getUsername = R.prop('username')
const eitherList = R.reduce(R.either, R.isNil)
const getDisplayName = eitherList([getName, getEmail, getUsername])
console.log(getDisplayName(data))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
Upvotes: 7