Hyperlinkv0.8.0-beta.28

Record

Record.intersectionconsteffect/Record.ts:1328
<K1 extends string, A, B, C>(
  that: ReadonlyRecord<K1, B>,
  combine: (selfValue: A, thatValue: B) => C
): <K0 extends string>(
  self: ReadonlyRecord<K0, A>
) => Record<ReadonlyRecord.IntersectKeys<K0, K1>, 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<ReadonlyRecord.IntersectKeys<K0, K1>, C>

Merges two records, retaining only the entries that exist in both records. For intersecting keys, the provided combine function is used to merge the values.

Example (Merging intersecting keys)

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

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