I wrote a code snippet that extracts the source
object keys (which are supposed to be unknown so I've added a non-existing key baz
to be extracted.
Every element in the array that I extract - I want to add the key from which It was extracted and then flatten the result to get a single array.
I've encountered two major issues while writing the code:
- When trying to use
S.insert
, It always returned an error because the object values (numbers{r:**<<number>>**}
) were of different type than the string i've tried to add to the keySection
({'Section': **'foo'**}
for example). I ended up mutating the object just for the sake of conveying my intention. - I didn't manage to write a proper map function that would abstract over the
Maybe
and let me access the variables inside. So I had to useS.maybeToNullable
and then re-wrap the value intoMaybe
. - Is there a better way of expressing any logic written below with
sanctuary
?
The code snippet:
const S = require('sanctuary');
const source = {
foo: [{ r: 1 }, { r: 2 }, { r: 3 }],
bar: [{ r: 4 }, { r: 5 }, { r: 6 }],
}
const result =
S.pipe([
S.map(
prop => {
const nullable = S.maybeToNullable(S.value(prop)(source))
return nullable ? S.Just(nullable.map(val => {val['Section'] = prop; return val})) : S.Nothing
}
),
S.justs,
S.reduce(xs => x => [...xs, ...x])([])
])(['foo', 'bar', 'baz'])