@evokegroup/merge
Merges multiple arrays or objects with a target using available merge options.
merge()
Parameter | Type | Description |
---|
target | object ¦ Array<*> | The object or array that will be the target of the merged objects or arrays |
...source | ...object ¦ ...Array<*> | The source objects or arrays whose properties or values will be merged into the target |
// Merge 2 objects
import { merge } from '@evokegroup/merge';
const obj1 = {
a: null,
b: 'b',
c: [1, 2, 3],
d: {
e: 'e'
}
};
const obj2 = {
a: 'a',
b: null,
c: [2, 3, 4],
d: {
e: '',
f: 'f'
}
};
const objMerged = merge({}, obj1, obj2); // return a new merged object
merge(obj1, obj2); // merge obj2 into obj1
/* Expected result: {
a: 'a',
b: 'b',
c: [1, 2, 3, 4],
d: {
e: 'e',
f: 'f'
}
} */
// Merge arrays
import { mergeOpts } from '@evokegroup/merge';
const array1 = [1, 2, 3, 4];
const array2 = [1, 4, 5, 6];
merge(array1, array2);
// Expected result: [1, 2, 3, 4, 5, 6]
mergeOpts()
Parameter | Type | Description |
---|
options | MergeOptions | The options to be used during the merge |
target | object ¦ Array<*> | The object or array that will be the target of the merged objects or arrays |
...source | ...object ¦ ...Array<*> | The source objects or arrays whose properties or values will be merged into the target |
import { mergeOpts } from '@evokegroup/merge';
const obj1 = {
a: 'a',
b: 'b',
c: null
};
const obj2 = {
a: null,
b: ''
};
mergeOpts({
stringEmpty: true,
targetClean: true
}, obj1, obj2);
/* Expected result: {
a: 'a',
b: ''
} */
import { mergeOpts } from '@evokegroup/merge';
const array1 = [1, 2, 3, 4];
const array2 = [1, 4, 5, 6];
mergeOpts({
arrayDuplicates: true
}, array1, array2);
// Expected result: [1, 2, 3, 4, 1, 4, 5, 6]
MergeOptions
Property | Type | Default | Description |
---|
allowUndefined | boolean | false | Allow a value of undefined to override the target. |
allowNull | boolean | false | Allow a value of null to override the target. |
stringEmpty | boolean | false | Allow an empty string to override the target. |
arrayEmpty | boolean | false | Allow an empty array to override the target. |
arrayMerge | boolean | true | Merge a target array with a source array. |
arrayDuplicates | boolean | false | Allow duplicate values to be merged into the target array. |
arrayUndefined | boolean | true | Allow a value of undefined to be merged into the target array. |
arrayNull | boolean | true | Allow a value of null to be merged into the target array. |
arrayStringEmpty | boolean | true | Allow an empty string to be merged into the target array. |
targetClean | boolean | false | If true the target array or object properties will be subject to the merge options. |