defaults-deep-safe
A deep version of _.defaults(object, [sources])
, safe by default by deep cloning each source
. Arrays are not merged.
Status
Installation
Install the package via npm
:
❯ npm install defaults-deep-safe
Usage
Arguments
object
(Object): The destination object.[source]
(...Object): The source objects.
Returns
(Object): Returns the destination object.
Example
const defaultsDeep = ; const object = foo: 'bar' bar: biz: net: 'qux' qux: 'biz' ;const source = bar: biz: net: 'txi' qox: 'fuc' qux: 'baz' ; ;// => { foo: 'bar', bar: { biz: { net: 'qux', qox: 'fuc' } }, qux: ['biz'] }
Or as a lodash mixin
:
const _ = ; _; _;
Motivation
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: d: e: 'f' ; const result = ; barb === resultb;// => true bard === resultd;// => true resultdg = 'h'; console;// => { e: 'f', g: 'h' }
Using defaults-deep-safe
:
const foo = a: 1 c: 2 ;const bar = b: d: e: 'f' ; const result = foo bar; barb === resultb;// => AssertionError: false == true bard === resultd;// => AssertionError: false == true resultdg = 'h'; console;// => { e: 'f' }
Tests
❯ npm test
License
MIT