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.
npm install @mzvonar/mergein
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'
}
}
}
npm test