Socket
Socket
Sign inDemoInstall

defaults-deep-safe

Package Overview
Dependencies
1
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    defaults-deep-safe

A deep version of _.defaults, safe by default.


Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Install size
1.36 MB
Created
Weekly downloads
 

Changelog

Source

1.0.0 (2018-09-23)

  • Remove support for node 4 #3 (ruimarinho)
  • Update dependencies #2 (rplopes)
  • Update package version #1 (fixe)

Readme

Source

defaults-deep-safe

A deep version of _.defaults(object, [sources]), safe by default by deep cloning each source. Arrays are not merged.

Status

npm version build status

Installation

Install the package via npm:

❯ npm install defaults-deep-safe

Usage

Arguments
  1. object (Object): The destination object.
  2. [source] (...Object): The source objects.
Returns

(Object): Returns the destination object.

Example
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]);

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: 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' }

Tests

❯ npm test

License

MIT

Keywords

FAQs

Last updated on 23 Sep 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc