0.2.5 • Published 5 years ago

sugarmerge v0.2.5

Weekly downloads
2
License
MIT
Repository
-
Last release
5 years ago

Sugar Merge

Deep merge utils with a little sugar on top

Merge

const { merge } = require('sugarmerge');

const a = {
    foo: {
        bar: [
            {
                baz: 'sis'
            },
            {
                qux: 'boom'
            }
        ]
    }
};

const b = {
    foo: {
        'bar[+]': { xyzzy: 'bah' }
    }
};

merge(a, b);

/**
 * returns:
 *    {
 *        foo: {
 *            bar: [
 *                {
 *                    baz: 'sis'
 *                },
 *                {
 *                    qux: 'boom'
 *                },
 *                {
 *                    xyzzy: 'bah'
 *                }
 *            ]
 *        }
 *    }
 */

const c = {
    foo: {
        'bar[-]': { xyzzy: 'bah' }
    }
};

merge(a, c);

/**
 * returns:
 *    {
 *        foo: {
 *            bar: [
 *                {
 *                    xyzzy: 'bah'
 *                },
 *                {
 *                    baz: 'sis'
 *                },
 *                {
 *                    qux: 'boom'
 *                }
 *            ]
 *        }
 *    }
 */

const d = {
    foo: {
        'bar[1,0]': { xyzzy: 'bah' }
    }
};

merge(a, d);

/**
 * returns:
 *    {
 *        foo: {
 *            bar: [
 *                {
 *                    baz: 'sis'
 *                },
 *                {
 *                    xyzzy: 'bah'
 *                },
 *                {
 *                    qux: 'boom'
 *                }
 *            ]
 *        }
 *    }
 */

Set

const { set } = require('sugarmerge');

const obj = {
    foo: {
        bar: [
            {
                baz: [ 1, 2, 3 ]
            }
        ]
    }
};

set(obj, 'foo.bar[0].baz[+].qux', 'xyzzy');

/**
 * returns:
 *    {
 *        foo: {
 *            bar: [
 *                {
 *                    baz: [
 *                        1,
 *                        2,
 *                        3,
 *                        {
 *                            qux: 'xyzzy'
 *                        }
 *                    ]
 *                }
 *            ]
 *        }
 *   }
 */

set(obj, 'foo.bar[0].baz[-].qux', 'xyzzy');

/**
 * returns:
 *    {
 *        foo: {
 *            bar: [
 *                {
 *                    baz: [
 *                        {
 *                            qux: 'xyzzy'
 *                        },
 *                        1,
 *                        2,
 *                        3
 *                    ]
 *                }
 *            ]
 *        }
 *   }
 */

set(obj, 'foo.bar[0].baz[1,0].qux', 'xyzzy');

/**
 * returns:
 *    {
 *        foo: {
 *            bar: [
 *                {
 *                    baz: [
 *                        1,
 *                        {
 *                            qux: 'xyzzy'
 *                        },
 *                        2,
 *                        3
 *                    ]
 *                }
 *            ]
 *        }
 *   }
 */

License

MIT

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago