A deep version of _.defaults(object, [sources])
, safe by default by deep cloning each source
. Arrays are not merged.
Install the package via npm
:
❯ npm install defaults-deep-safe
object
(Object): The destination object.[source]
(...Object): The source objects.
(Object): Returns the destination object.
const defaultsDeep = require('defaults-deep-safe');
const object = { foo: 'bar', bar: { biz: { net: 'qux' } }, qux: ['biz'] };
const source = { bar: { biz: { net: 'txi', qox: 'fuc' } }, qux: ['baz'] };
defaultsDeep(object, source);
// => { foo: 'bar', bar: { biz: { net: 'qux', qox: 'fuc' } }, qux: ['biz'] }
Or as a lodash mixin
:
const _ = require('lodash');
_.mixin({
defaultsDeep: require('defaults-deep-safe')
});
_.defaultsDeep(object, [sources]);
This module is perfect for merging config/settings files and to safely handle options by avoiding changing objects by reference.
Here's a quick example demonstrating why using _.defaults
may not be a safe operation:
const foo = { a: 1, c: 2 };
const bar = { b: new Date(), d: { e: 'f' } };
const result = require('lodash').defaults(foo, bar);
require('assert')(bar.b === result.b);
// => true
require('assert')(bar.d === result.d);
// => true
result.d.g = 'h';
console.log(bar.d);
// => { e: 'f', g: 'h' }
Using defaults-deep-safe
:
const foo = { a: 1, c: 2 };
const bar = { b: new Date(), d: { e: 'f' } };
const result = require('defaults-deep-safe')(foo, bar);
require('assert')(bar.b === result.b);
// => AssertionError: false == true
require('assert')(bar.d === result.d);
// => AssertionError: false == true
result.d.g = 'h';
console.log(bar.d);
// => { e: 'f' }
❯ npm test
MIT