strict-merge

1.0.2 • Public • Published

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)

Readme

Keywords

Package Sidebar

Install

npm i strict-merge

Weekly Downloads

7

Version

1.0.2

License

Apache-2.0

Unpacked Size

40.9 kB

Total Files

15

Last publish

Collaborators

  • bluepnume