Ramda js: lens for deeply nested objects with nested arrays of objects

試著忘記壹切 提交于 2019-12-02 18:14:50

This should be possible by creating a lens that matches an object by ID which can then be composed with other lenses to drill down to the image field.

To start with, we can create a lens that will focus on an element of an array that matches some predicate (note: this will only be a valid lens if it is guaranteed to match at least one element of the list)

//:: (a -> Boolean) -> Lens [a] a
const lensMatching = pred => (toF => entities => {
    const index = R.findIndex(pred, entities);
    return R.map(entity => R.update(index, entity, entities),
                 toF(entities[index]));
});

Note that we're manually constructing the lens here rather than using R.lens to save duplication of finding the index of the item that matches the predicate.

Once we have this function we can construct a lens that matches a given ID.

//:: String -> Lens [{ id: String }] { id: String }
const lensById = R.compose(lensMatching, R.propEq('id'))

And then we can compose all the lenses together to target the image field

const imageLens = R.compose(
  R.lensProp('groups'),
  lensById('/1/B'),
  R.lensProp('apps'),
  lensById('/1/B/i'),
  R.lensPath(['container', 'docker', 'image'])
)

Which can be used to update the data object like so:

set(imageLens, 'NAME:VERSION2', data)

You could then take this a step further if you wanted to and declare a lens that focuses on the version of the image string.

const vLens = R.lens(
  R.compose(R.nth(1), R.split(':')),
  (version, str) => R.replace(/:.*/, ':' + version, str)
)

set(vLens, 'v2', 'NAME:v1') // 'NAME:v2'

This could then be appended to the composition of imageLens to target the version within the entire object.

const verLens = compose(imageLens, vLens);
set(verLens, 'VERSION2', data);

Here's one solution:

const updateDockerImageName =
R.over(R.lensProp('groups'),
       R.map(R.over(R.lensProp('apps'),
                    R.map(R.when(R.propEq('id', '/1/B/i'),
                                 R.over(R.lensPath(['container', 'docker', 'image']),
                                        R.replace(/^NAME:VERSION1$/, 'NAME:VERSION2')))))));

This could be decomposed into smaller functions, of course. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!