GitHub

daffMerge

Helper

import { daffMerge } from '@daffodil/core'

Merges dictionaries with a specific strategy for handling collisions.

function daffMerge<T extends Record<string, unknown> = Record<string, unknown>>(
  dicts: T[]
  strategy: DaffMergeStrategy<T> = {}
): T

Parameters

Parameterdicts: T[]
Description
Parameterstrategy: DaffMergeStrategy<T>
Default{}
Description

Examples

Merging two dictionaries with predefined mergers

const a = {
     ary: [1, 2],
  obj: {foo: 5, bar: 10}
}
const a = {
     ary: [3, 4],
  obj: {foo: 6},
  fish: 'tacos'
}
const result = daffMerge(
  [a, b],
  {
    ary: daffArrayConcatMerger,
    obj: daffDictAssignMerger
  }
)

the value of result would be:

{
  ary: [1, 2, 3, 4],
  obj: {foo: 6, bar: 10},
  fish: 'tacos'
}

Merging two dictionaries with predefined mergers

const a = {
     ary: [1, 2],
  obj: {foo: 5, bar: 10}
}
const a = {
     ary: [3, 4],
  obj: {foo: 6},
  fish: 'tacos'
}
const result = daffMerge(
  [a, b],
  {
    ary: daffArrayConcatMerger,
    obj: daffDictAssignMerger
  }
)

the value of result would be:

{
  ary: [1, 2, 3, 4],
  obj: {foo: 6, bar: 10},
  fish: 'tacos'
}

Merging two dictionaries with predefined mergers

const a = {
     ary: [1, 2],
  obj: {foo: 5, bar: 10}
}
const a = {
     ary: [3, 4],
  obj: {foo: 6},
  fish: 'tacos'
}
const result = daffMerge(
  [a, b],
  {
    ary: daffArrayConcatMerger,
    obj: daffDictAssignMerger
  }
)

the value of result would be:

{
  ary: [1, 2, 3, 4],
  obj: {foo: 6, bar: 10},
  fish: 'tacos'
}

Merging two dictionaries with predefined mergers

const a = {
     ary: [1, 2],
  obj: {foo: 5, bar: 10}
}
const a = {
     ary: [3, 4],
  obj: {foo: 6},
  fish: 'tacos'
}
const result = daffMerge(
  [a, b],
  {
    ary: daffArrayConcatMerger,
    obj: daffDictAssignMerger
  }
)

the value of result would be:

{
  ary: [1, 2, 3, 4],
  obj: {foo: 6, bar: 10},
  fish: 'tacos'
}