Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

config-assign

fzs1116MIT1.1.1

A configurable alternative of Object.assign

Object.assign, object, assign, config, configurable, option, extend, merge, ES6, properties

readme

config-assign

A configurable alternative of Object.assign

0. Table of contents

  1. Table of contents (You're reading it right now, so this link is useless)
  2. Installation
  3. Usage
    1. Require
    2. Syntax
    3. Return value
    4. Throws
  4. Examples
  5. All together...
  6. See also
  7. License

1. Installation

Run the following command to install:

npm i config-assign

Note! config-assign works on ES6 and above

2. Usage

2.1 Require

Obtain the function like this:

const configAssign = require('config-assign')

2.2 Syntax

Syntax:

configAssign(target, source[, options])
//or
configAssign(target, ...sources, options)

console.log(result) //{foo:0, bar:1, baz:2}

Works with any JS object (incuding arrays):
```js
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}
}

4. All together...

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.

5. See also

Object.assign - Similar, native JS function, without options

6. License

Licensed under: MIT © 2019 fzs111