A configurable alternative of
Object.assign
- Table of contents (You're reading it right now, so this link is useless)
- Installation
- Usage
- Examples
- All together...
- See also
- License
Run the following command to install:
npm i config-assign
Note!
config-assign
works on ES6 and above
Obtain the function like this:
const configAssign = require('config-assign')
Syntax:
configAssign(target, source[, options])
//or
configAssign(target, ...sources, options)
target <Object>
: The target objectsource <Object>
,...sources <Object>
: The source object(s)options <Object>
: A configuration object containing (some of) the following values:-
descriptors <boolean>
: Whether or not to copy accessor property descriptors.
Default istrue
. -
define <boolean>
: Whether or not to usedefineProperty
andgetOwnPropertyDescriptors
instead of[[Set]]
and[[Get]]
. Iffalse
,configAssign
will trigger getters and setters, while iftrue
, it will copy them.
Default istrue
. -
mutate <boolean>
: Whether or not to mutate thetarget
object.Warning! Disabling this option won't preserve object's method closures and constructors! Calling a closure-based method, or using
instanceof
on the newly created object, probably won't return the expected result, so it should be used only with plain objects.Note! Setting this option to
false
doesn't guarantee that the target object cloned deeply! Only the objects modified byconfigAssign
are cloned.Default is
true
. -
symbols <boolean>
: Whether or not to copy symbol properties.
Default istrue
. -
recursive <number>
: Maximum depth of recursion. UseInfinity
or-1
for infinite depth, and0
to disable recursion.
Default is0
. -
returnBool <boolean>
: Whether or not to return booleanfalse
on error, instead of throwing an exception.
Default isfalse
. -
stopOnError <boolean>
: Whether or not to abort the copying task when an error occurs.
Default istrue
. -
nonEnumerable <boolean>
: copy non-enumerable properties.
Default isfalse
. -
filter <Function> | <null>
: Function used to filter properties. The function should returntrue
orfalse
, indicating whether the property should or shouldn't be copied, respectively. Ifnull
, all properties being copied. The function gets called for each property of the source objects, and receives the following arguments:property <string> | <symbol>
: The property being copiedtarget <Object>
: Thetarget
objectsource <Object>
: The currentsource
object
Default is
null
. -
reverse <boolean>
: Whether or not to keep the leftmost value (instead of the rightmost) of the properties with the same key.Note! This option doesn't change the order of assigning properties.
Default is
false
.
-
- On success, the function returns the target object. It is the same object as the
target
given as argument, unless themutate
option set tofalse
. In that case, a cloned object get returned. - On failure, returns
false
if thereturnBool
option set totrue
Error
on failure, if thereturnBool
option isfalse
TypeError
if one of thesource
parameters is not an object andreturnBool
option isfalse
Works like Object.assign
:
const result = configAssign({foo:0},{bar:1},{baz:2},{/* options */})
console.log(result) //{foo:0, bar:1, baz:2}
Works with any JS object (incuding arrays):
const result = configAssign(['a','b'],[,,'c','d'],{4:'e',5:'f'},{/* options */})
console.log(result) //['a','b','c','d','e','f']
By default, it mutates the target
object:
const target = {foo:0}
const source = {bar:1}
configAssign(target,source,{/* options */})
console.log(target) //{foo:0, bar:1}
And returns the target
:
const target = {foo:0}
const source = {bar:1}
const result = configAssign(target,source,{/* options */})
console.log(target === result) //true
Easier error handling with returnBool: true
:
const target = Object.freeze({foo:0}) //Immutable target
const source = {bar:1}
//Original
{
let result;
try{
result = configAssign(target,source,{/* options */})
}catch(e){
//Handle error somehow
}
if(result){
//Do something
}
}
//With `returnBool:true`
{
if(configAssign(target,source,{returnBool:true})){
//Do something
} else {
//Handle error somehow
}
}
Supports recursion:
const target = {
foo:{
bar:{
baz:0,
x:0
},
x:0
},
x:0
}
const source = {foo:{bar:{baz:1}}}
//Reassign only baz
configAssign(target,source,{recursive:Infinity})
console.log(target)
/*
{
foo:{
bar:{
baz:1,
x:0
},
x:0
},
x:0
}
*/
You can even restrict recursion depth:
const target = {
foo:{
bar:{
baz:0,
x:0
},
x:0
},
x:0
}
const source = {foo:{bar:{baz:1}}}
//Reassign only bar
configAssign(target,source,{recursive:1})
console.log(target)
/*
{
foo:{
bar:{
baz:1
},
x:0
},
x:0
}
*/
Or you can disable mutations:
const target = {foo:0}
const source = {bar:1}
const result = configAssign(target,source,{mutate:false})
console.log(target) //{foo:0}
console.log(result) //{foo:0,bar:1}
console.log(result === target) //false
But be careful with more complex objects:
class Foo{}
const target = new Foo()
const source = {bar:1}
const result = configAssign(target,source,{mutate:false})
console.log(target) //Foo{}
console.log(result) //Foo{bar:1}
console.log(result instanceof Foo) //false ?!
Forwards or backwards? It relies on the reverse
option:
const target = {foo:0,bar:0}
const source = {bar:1,baz:1}
//With reverse: false (default), keeping the rightmost -->
{
const result = configAssign(target,source,{reverse:false,mutate:false})
console.log(target) //{foo:0,bar:1,baz:1}
}
//With reverse: true, keeping the leftmost <--
{
const result = configAssign(target,source,{reverse:true,mutate:false})
console.log(target) //{foo:0,bar:0,baz:1}
}
You got the power!
Combine all options together, and reach anything you want.
If you have a question, recommendation, bug or a feature request, open an issue in the Github repository.
Object.assign
- Similar, native JS function, without options
Licensed under: MIT © 2019 fzs111