node-assign v1.1.1
node-assign
Recursive Object.assign as a node module.
Usage
npm install node-assignThere are two functions included. The assign function (default) and the explicitly called merge function.
require('node-assign')(target, ...sources)
//-> return target
require('node-assign').merge(target, ...sources)
//-> return targetThe only difference between the two is that assign will overwrite sub objects with supplied non-objects, whereas merge will preserve object depth in the target (or add to it). merge will never remove a target key hierarchy, whereas assign might drop entire sub objects if given non-object keys. See below for example.
Use assign
const assign = require('node-assign')
var objA = {foo: 1, bar: {a: 1, b: 2}, baz: {goo: 99}}
var objB = {bar: {c: 3}, baz: 3}
assign(objA, objB)
console.log(objA)
//-> {foo: 1, bar: {a: 1, b: 2, c: 3}, baz: 3}Use merge
const merge = require('node-assign').merge
var objA = {foo: 1, bar: {a: 1, b: 2}, baz: {goo: 99}}
var objB = {bar: {c: 3}, baz: 3}
merge(objA, objB)
console.log(objA)
//-> {foo: 1, bar: {a: 1, b: 2, c: 3}, baz: {goo: 99}}Globalize
You can also globalize it into Object, but I suggest you use a different name than assign, since the built-in function may be used in various libraries which require its specific functionality.
Object.rassign = require('node-assign') // recursive assign
Object.merge = Object.rassign.merge // deep mergeCaveats
Important version update 1.1
The current version of the function ALTERS the target object, similar to Object.assign. If you need the function to return a brand new object, use an empty object literal {} as the first argument.
Only objects
The original Object.assign can also accept primitives (which are then converted to objects). Our function does not accept primitives as arguments. It will throw a fit and report you to the authorities.