In the following user
object, we need a new key called contactDetails
which is of the form "user.number
user.email
". This would seem like a good place to make use of the pipe function.
We select the required properties using props
, join them together and create an object from it with objOf
. objOf
is a Ramda function that takes in a key and a value and creates an object from it: objOf(key, value)
returns { key: value }
.
The next step is to merge it into user
. We can use mergeLeft
or mergeRight
as follows:
This does look good, but it works only for the above user
object. How would we reuse this for any input object? We would need to pass the object as a parameter to this function. We also cannot curry it since user
object is used twice. It can be written as follows:
Now, we can make it fully point-free and curry the data using more Ramda functions. But, let us stop this exercise here and look at how we can do it using vanilla JavaScript.
As you can see, vanilla JavaScript is perfectly readable. We did not have to go through the entire thinking process in Ramda. We saw a part of the requirement that would have been great for the pipe function and decided to use Ramda. This made it complicated for the complete requirement.
We should always try looking at vanilla JavaScript and how readable it would look before thinking of using Ramda in our code.