UNPKG

assign-deep

Version:

Deeply assign the values of all enumerable-own-properties and symbols from one or more source objects to a target object. Returns the target object.

125 lines (86 loc) 6.13 kB
# assign-deep [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=W8YFZ425KND68) [![NPM version](https://img.shields.io/npm/v/assign-deep.svg?style=flat)](https://www.npmjs.com/package/assign-deep) [![NPM monthly downloads](https://img.shields.io/npm/dm/assign-deep.svg?style=flat)](https://npmjs.org/package/assign-deep) [![NPM total downloads](https://img.shields.io/npm/dt/assign-deep.svg?style=flat)](https://npmjs.org/package/assign-deep) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/assign-deep.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/assign-deep) > Deeply assign the values of all enumerable-own-properties and symbols from one or more source objects to a target object. Returns the target object. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. ## Install Install with [npm](https://www.npmjs.com/): ```sh $ npm install --save assign-deep ``` ## Heads up! [Please update](https://github.com/update/update) to version 1.0.1 or later, a critical bug was fixed in that version. ## Behavior * This follows the same behavior as [Object.assign()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign), and thus _does not_ deep clone values. * The first argument is the "target" object. * To shallow clone, pass an empty object as the first argument. * One or more additional ("source") objects may be passed. * When multiple objects are passed, properties in _later_ objects will overwrite same-named properties in _earlier_ objects. Thus, properties in the target object will be overwritten by same-named properties in other objects. * Only enumerable and own properties are copied. * String and Symbol properties are copied. * Sparse arguments are skipped, so this does not throw on `null` or `undefined` source values. * Like [Object.assign()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign), `[[Get]]` is used on source objects and `[[Set]]` is used on the target, so it will invoke getters and setters. Therefore it assigns properties versus just copying or defining new properties. _Note that this should not be used for merging new properties into a prototype if the merge sources contain getters and you do not want `[[Get]]` to be used on the getters. For copying property definitions and their enumerability into prototypes `Object.getOwnPropertyDescriptor()` and `Object.defineProperty()` should be used instead._ ## Usage ```js const assign = require('assign-deep'); const config = { admin: true, author: { name: { first: 'Joe' } } }; const locals = { admin: false, author: { name: { last: 'Smith' }, username: 'joesmith' } }; console.log(assign(config, locals)); // { // admin: false, // author: { // name: { first: 'Joe', last: 'Smith' }, // username: 'joesmith' // } // } ``` ## About <details> <summary><strong>Contributing</strong></summary> Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). </details> <details> <summary><strong>Running Tests</strong></summary> Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: ```sh $ npm install && npm test ``` </details> <details> <summary><strong>Building docs</strong></summary> _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ To generate the readme, run the following command: ```sh $ npm install -g verbose/verb#dev verb-generate-readme && verb ``` </details> ### Related projects You might also be interested in these projects: * [assign-symbols](https://www.npmjs.com/package/assign-symbols): Assign the enumerable es6 Symbol properties from one or more objects to the first object… [more](https://github.com/jonschlinkert/assign-symbols) | [homepage](https://github.com/jonschlinkert/assign-symbols "Assign the enumerable es6 Symbol properties from one or more objects to the first object passed on the arguments. Can be used as a supplement to other extend, assign or merge methods as a polyfill for the Symbols part of the es6 Object.assign method.") * [extend-shallow](https://www.npmjs.com/package/extend-shallow): Extend an object with the properties of additional objects. node.js/javascript util. | [homepage](https://github.com/jonschlinkert/extend-shallow "Extend an object with the properties of additional objects. node.js/javascript util.") * [merge-deep](https://www.npmjs.com/package/merge-deep): Recursively merge values in a javascript object. | [homepage](https://github.com/jonschlinkert/merge-deep "Recursively merge values in a javascript object.") * [mixin-deep](https://www.npmjs.com/package/mixin-deep): Deeply mix the properties of objects into the first object. Like merge-deep, but doesn't clone… [more](https://github.com/jonschlinkert/mixin-deep) | [homepage](https://github.com/jonschlinkert/mixin-deep "Deeply mix the properties of objects into the first object. Like merge-deep, but doesn't clone. No dependencies.") ### Contributors | **Commits** | **Contributor** | | --- | --- | | 31 | [jonschlinkert](https://github.com/jonschlinkert) | | 14 | [doowb](https://github.com/doowb) | ### Author **Jon Schlinkert** * [GitHub Profile](https://github.com/jonschlinkert) * [Twitter Profile](https://twitter.com/jonschlinkert) * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) ### License Copyright © 2019, [Jon Schlinkert](https://github.com/jonschlinkert). Released under the [MIT License](LICENSE). *** _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on June 19, 2019._