Skip to content

Merges object with value at provided path in context object

License

Notifications You must be signed in to change notification settings

mzvonar/mergeIn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mergeIn Build Status Coverage Status npm version

Merges value in object by path with provided object. Path can be string or array (e.g. ['user', 'profile', 'gender']).
If any part of path doesn't exist it is created. Always returns new copy of object.

Installation

npm install @mzvonar/mergein

Usage

const mergeIn = require('@mzvonar/mergein');
  
const context = {
    user: {
        profile: {
            gender: 'female'
        }
    }
};
  
const newContext = mergeIn(context, ['user', 'profile'], {
    gender: 'male',
    address: {
        country: 'slovakia'
    }
});

returns:

    {
        user: {
            profile: {
                gender: 'male',
                address: {
                        country: 'slovakia'
                    }
            }
        }
    }
const newContext = mergeIn(context, 'user', {
    address: {
      country: 'slovakia'
    }
);

returns:

    {
        user: {
            profile: {
                gender: 'female',
            },
            address: {
                country: 'slovakia'
            }
        }
    }

Tests

npm test

About

Merges object with value at provided path in context object

Resources

License

Stars

Watchers

Forks

Packages

No packages published