Community, hello . I stuck with a small issue. I have such a data structure
{
"type": "Shoes",
"gender": "female",
"userInfo": {
"whois": {
"male": {
"htt": 20.2,
"referenceBrands": [{
"category": "Shoes",
"size": 40
}]
},
"female": {
"height": 191.0,
"age": 20,
"referenceBrands": [{
"category": "Shoes",
"size": 35.5
}, {
"category": "Apparel",
"size": 50
}]
}
},
}
}
I want to update a specific key in a reference brand based on gender
and a type
which are in a root.
Based on this answer (thanks to Scott Sauyet), I've created something like this. But as you see, I have hardcoded gender
and category
what the best way of using composition and point-free style to change this?
const lensMatch = (propName) => (key) => lens(
find(
propEq(propName, key)
),
(val, arr, idx = findIndex(propEq (propName, key), arr)) =>
update(idx > -1 ? idx : length (arr), val, arr)
)
const lensCategory = lensMatch('category')
const genderLens = lensProp('gender')
const lensReferenceBrand = lensProp('referenceBrands')
const updateSize = (prop, value) => over(
compose(
lensPath(['userInfo', 'whois']),
lensProp('female'),
lensReferenceBrand,
lensCategory('Shoes'),
lensProp(prop),
), always(value), data)
updateSize('size', 100)