jonschlinkert / merge-deep

Recursively merge values in a JavaScript object.

Home Page:https://github.com/jonschlinkert

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

notUnion property for arrays

ale-otero opened this issue · comments

Can specify which array fields will be replaced (instead of concatenated with union). Few lines in code and all tests working.

module.exports = function mergeDeep(orig, objects, notUnionParams) {

if (isObject(val) || Array.isArray(val)) {
     merge(target, val, notUnionParams);
}
function merge(target, obj, notUnionParams) {
var notUnion = false;
if((notUnionParams !== undefined) && (notUnionParams.length > 0) && ((notUnionParams.indexOf(key) > -1) || (notUnionParams == key))){
     notUnion = true;
}
if (isObject(newVal) && isObject(oldVal)) {
     target[key] = merge(newVal, oldVal, notUnionParams);
} else if (Array.isArray(newVal)) {
     if(notUnion){
          target[key] = union([], newVal);  
     } else {
          target[key] = union([], newVal, oldVal);
     }
} else {
     target[key] = clone(oldVal);
}

And the test

it('should correctly use not union property', function() {
    var obj1 = {a: {
      "a": [{
        1: 1,2: 2
      }]
    }};
    var obj2 = {a: {
      "a": [{
        1: 2,2: 3
      }]
    }};

    var actual = merge(obj1, obj2, ["a"]);
    console.log(actual.a);
    assert.deepEqual(actual.a, { a: [{ 1:1,2:2 }] });
});