sugarmerge
deepmerge utils with a little sugar on top
Last updated a year ago by machellerogden .
MIT · Original npm · Tarball · package.json
$ cnpm install sugarmerge 
SYNC missed versions from official npm registry.

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

Current Tags

  • 0.2.5                                ...           latest (a year ago)

13 Versions

  • 0.2.5                                ...           a year ago
  • 0.2.4                                ...           a year ago
  • 0.2.3                                ...           a year ago
  • 0.2.2                                ...           a year ago
  • 0.2.1                                ...           a year ago
  • 0.2.0                                ...           a year ago
  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
  • 0.0.5                                ...           2 years ago
  • 0.0.4                                ...           2 years ago
  • 0.0.3                                ...           2 years ago
  • 0.0.2                                ...           2 years ago
  • 0.0.1                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 2
Last Month 5
Dependencies (1)
Dev Dependencies (4)
Dependents (4)

Copyright 2014 - 2016 © taobao.org |