Say I have a series of calculations that each use the …spread operator to add a new property to an object
Note: Just an example the actual calculations don’t matter here
const calc1 = (obj) => ({
...obj,
x: obj.a + obj.b
})
const calc2 = (obj) => ({
...obj,
y: obj.x + obj.c
})
const calc3 = (obj) => ({
...obj,
z: obj.x + obj.y
})
const total = (obj) => ({
...obj,
total: obj.x + obj.y + obj.z
})
const calculations = [calc1, calc2, calc3]
I then create a calculation function using pipe
const calculate = pipe(...calculations)
const result = calculate({a: 1, b: 2, c: 3})
// resulting in something like this
{
a: 1,
b: 2,
...
...
z: 9,
total: 24
}
To give you a picture, I am using the above procedure on form inputs to generate a hash-map of form outputs.
It’s workable, but to refactor further what if I took out the spread operators from each function e.g.
const calc1 = (obj) => ({
x: obj.a + obj.b
})
const calc2 = (obj) => ({
y: obj.x + obj.c
})
const calc3 = (obj) => ({
z: obj.x + obj.y
})
What if I then make a function that wraps each of those functions to do the merging for me.
const mergeWith = fn => obj => ({ ...obj, ...fn(obj) })
I then create the pipe function like so
const calculate = pipe(...calculations.map(mergeWith))
The reason I am asking this, is that I have done quite a bit of searching through the likes of ramda, underscore etc. and I can’t find anything like this mergeWith function. This makes me think my approach is unnecessarily complicating things or is flawed.