1.0.2 • Published 5 years ago

strict-merge v1.0.2

Weekly downloads
117
License
Apache-2.0
Repository
github
Last release
5 years ago

Strict Merge

Deep merge two objects with strict type checking.

import { strictMerge } from 'strict-merge';

let first = {
    foo: {
        bar: {
            baz: 1,
            biz: 2
        }
    }
}

let second = {
    foo: {
        bar: {
            bas: 3
        }
    }
}

let result = strictMerge(first, second);

/*
{
    foo: {
        bar: {
            baz: 3,
            biz: 2
        }
    }
}
*/

Or provide a merger function to resolve conflicts:

import { strictMerge, TYPE } from 'strict-merge';

let first = {
    foo: {
        bar: {
            baz: 1,
            biz: 2
        }
    }
}

let second = {
    foo: {
        bar: {
            bas: 3
        }
    }
}

let result = strictMerge(first, second, (a, b, type) => {
    if (type === TYPE.NUMBER) {
        return Math.min(a, b);
    }
    return b;
});

/*
{
    foo: {
        bar: {
            baz: 1,
            biz: 2
        }
    }
}
*/

Notes:

  • Leaf nodes from second argument will take precedence
  • Any conflicts between types for leaf nodes will result in an error (e.g. if you try to merge a string with an int)