Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notUnion property for arrays #10

Open
ale-otero opened this issue Jun 18, 2019 · 0 comments
Open

notUnion property for arrays #10

ale-otero opened this issue Jun 18, 2019 · 0 comments

Comments

@ale-otero
Copy link

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 }] });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant