In my country (Brazil) we have a saying, who doesn't have a dog hunting with a cat.
I've been working on a project where we could not update the latest version of the node and there was no possibility to put the babel
or even the experimental flag --harmony
, because we had some enterprise restrictions to do something like that.
So it's time to use creativity ✨
const optionalChainingByPath = (object, path) => {
const pathSplitted = path.split('.')
const [firstKey] = pathSplitted
if (object[firstKey] == null || object[firstKey] ==='' ) { return null }
if (typeof object[firstKey] === 'object') {
pathSplitted.shift()
return optionalChainningByPath(object[firstKey], pathSplitted.join('.'))
}
return object[firstKey]
}
Usage:
const makeResponse = patient => ({
name: optionalChainingByPath(patient, 'personalInformation.name'),
gender: optionalChainingByPath(patient, 'personalInformation.gender'),
cardNumber: optionalChainingByPath(patient, 'personalInformation.cardNumber')
})
It's ok but unamused 😒
Let's make this cool enough 🥳
We'll use partial functions to transform this boring function into a fancy function ✨
const optionalChainingByPath = object => path => {
const pathSplitted = path.split('.')
const [firstKey] = pathSplitted
if (object[firstKey] == null || object[firstKey] === '') {
return null
}
if (typeof object[firstKey] === 'object') {
pathSplitted.shift()
return optionalChainingByPath(object[firstKey], pathSplitted.join('.'))
}
return object[firstKey]
}
Usage:
const makeResponse = patient => {
return {
name: optionalChaining('personalInformation.name'),
gender: optionalChaining('personalInformation.gender'),
cardNumber: optionalChaining('personalInformation.cardNumber')
}
}
Does sounds like a charm or doesn't?
Top comments (7)
Always fun to work through these problems as a learning experience, but here's another implementation, based on the lodash get method.
github.com/cedmax/youmightnotneed/...
The same GitHub repo has examples of
set
andhas
implementations.Awesome tip fude, thank u for sharing 🌞🤘
Really cool solution, it does look like a charm : )
You can also extract the object value in a functional style with:
and use like that:
We can also extend the Object prototype to avoid some problems (dont know about how good or bad it could be in terms of design, but its possible):
Clean and beauty, thanks bro
If Node's new stuff doesn't come to you, then move YOUR VERY SELF to Node's new stuff haha
Suhsiajaidjdi it's ALL folks xD
Here is another way based on proxy that that feels more native