Hyperlinkv0.8.0-beta.28

Record

Record.unionconsteffect/Record.ts:1269
<K1 extends string, A, B, C>(
  that: ReadonlyRecord<K1, B>,
  combine: (selfValue: A, thatValue: B) => C
): <K0 extends string>(
  self: ReadonlyRecord<K0, A>
) => Record<K0 | K1, A | B | C>
<K0 extends string, A, K1 extends string, B, C>(
  self: ReadonlyRecord<K0, A>,
  that: ReadonlyRecord<K1, B>,
  combine: (selfValue: A, thatValue: B) => C
): Record<K0 | K1, A | B | C>

Merges two records, preserving entries that exist in either of the records. For keys that exist in both records, the provided combine function is used to merge the values.

Example (Merging records with union)

import { Record } from "effect"
import * as assert from "node:assert"

assert.deepStrictEqual(
  Record.union({ a: 1, b: 2 }, { b: 3, c: 4 }, (a, b) => a + b),
  { a: 1, b: 5, c: 4 }
)
combining
Source effect/Record.ts:126939 lines
export const union: {
  <K1 extends string, A, B, C>(
    that: ReadonlyRecord<K1, B>,
    combine: (selfValue: A, thatValue: B) => C
  ): <K0 extends string>(self: ReadonlyRecord<K0, A>) => Record<K0 | K1, A | B | C>
  <K0 extends string, A, K1 extends string, B, C>(
    self: ReadonlyRecord<K0, A>,
    that: ReadonlyRecord<K1, B>,
    combine: (selfValue: A, thatValue: B) => C
  ): Record<K0 | K1, A | B | C>
} = dual(
  3,
  <K0 extends string, A, K1 extends string, B, C>(
    self: ReadonlyRecord<K0, A>,
    that: ReadonlyRecord<K1, B>,
    combine: (selfValue: A, thatValue: B) => C
  ): Record<K0 | K1, A | B | C> => {
    if (isEmptyRecord(self)) {
      return { ...that } as any
    }
    if (isEmptyRecord(that)) {
      return { ...self } as any
    }
    const out: Record<string, A | B | C> = empty()
    for (const key of keys(self)) {
      if (has(that, key as any)) {
        out[key] = combine(self[key], that[key as unknown as K1])
      } else {
        out[key] = self[key]
      }
    }
    for (const key of keys(that)) {
      if (!has(out, key)) {
        out[key] = that[key]
      }
    }
    return out
  }
)
Referenced by 1 symbols