2.0.7 • Published 2 years ago

@evokegroup/merge v2.0.7

Weekly downloads
1
License
ISC
Repository
bitbucket
Last release
2 years ago

@evokegroup/merge

Merges multiple arrays or objects with a target using available merge options.

merge()

ParameterTypeDescription
targetobject ¦ 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()

ParameterTypeDescription
optionsMergeOptionsThe options to be used during the merge
targetobject ¦ 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

PropertyTypeDefaultDescription
allowUndefinedbooleanfalseAllow a value of undefined to override the target.
allowNullbooleanfalseAllow a value of null to override the target.
stringEmptybooleanfalseAllow an empty string to override the target.
arrayEmptybooleanfalseAllow an empty array to override the target.
arrayMergebooleantrueMerge a target array with a source array.
arrayDuplicatesbooleanfalseAllow duplicate values to be merged into the target array.
arrayUndefinedbooleantrueAllow a value of undefined to be merged into the target array.
arrayNullbooleantrueAllow a value of null to be merged into the target array.
arrayStringEmptybooleantrueAllow an empty string to be merged into the target array.
targetCleanbooleanfalseIf true the target array or object properties will be subject to the merge options.