0
var _ = require('ramda');

var obj1 = {
    innerNum: 1,
    innerObj: {
        innerStr: 'a',
        innerStrB: 'Z'
    }
};

var obj2 = {
    innerNum: 2,
    innerObj: {
        innerStr: 'b'
    }
};

var mixedObj = _.mixin(obj1, obj2);

mixedIObj does not include the inner object's innerStrB. Any ramda solution?

Daniel
  • 1,562
  • 3
  • 22
  • 34

1 Answers1

2

It's not clear what you would want here. Most mixin/extend implementations I've seen are shallow, adding the values of the keys in the second object to those of the first, overriding where the keys are duplicated.

Ramda does have functions to clone an object with an updated value at a particular path: assocPath. I'm guessing that this wouldn't do everything you seem to want, though:

R.assocPath('innerObj.innerStr', 'b', obj1); //=> 
// { 
//    innerNum: 1,
//    innerObj: {
//      innerStr: 'b',
//      innerStrB: 'Z'
//    }
// }

The question is what a deep mixin would really mean. If the objects have the same structure, it's pretty clear, but if they don't, it might be a little hairy:

mixinDeep(
    {a: 1, b: {c: 2, d: 3}},
    {b: {d: {e: 4, f: 5}, g: 6}}
);
// ?=>? {a: 1, b: {c: 2, d: {e: 4, f: 5}, g: 6}}

If that's the sort of thing you're looking for, then the Ramda team would love to have an issue raised or even a pull request.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • The mixinDeep example you gave seems reasonable to me, but it does deserve more thinking before implementation... I would be glad to contribute to Ramda, great and helpful library! Thanks. – Daniel Feb 01 '15 at 15:57