@trenskow/merge
v0.1.30
Published
A small library for doing recursive `Object.assign`.
Downloads
1,269
Readme
@trenskow/merge
A small library for doing recursive Object.assign
.
Usage
Below is an example of how to use the package.
import merge from '@trenskow/merge';
const myFirstValue = { first: 1, shared: { value: 1 } };
const mySecondValue = { second: 2, shared: { value: 2 } };
const myThirdValue = { third: 3, shared: { value: 3 } };
const myValue = merge(myFirstValue, mySecondValue, myThirdValue);
/*
myValue is {
first: 1,
second: 2,
third: 3,
shared: {
value: 3
}
}
*/
How it works
Input values are compared in pairs, so if more than two values are provided, the first two are compared – then the result is compared to the third – and that result is compared to the fourth, etc...
Rules
The rules for merging the input values is as below.
- If one of the values is not an object?
- The second value survives.
- If both values are an array?
- An new array is created with the items from the first array and then the values of the second array.
- The new array survives.
- An new array is created with the items from the first array and then the values of the second array.
- Both values are now regarded as objects.
- An new object with the following keys are created.
- The properties unique of the first object.
- The properties which is set on both objects are merged.
- The keys unique of the second object.
- The new object survives.
- An new object with the following keys are created.
License
See license in LICENSE.