Hyperlinkv0.8.0-beta.28

Record

Record.differenceconsteffect/Record.ts:1377
<K1 extends string, B>(that: ReadonlyRecord<K1, B>): <
  K0 extends string,
  A
>(
  self: ReadonlyRecord<K0, A>
) => Record<K0 | K1, A | B>
<K0 extends string, A, K1 extends string, B>(
  self: ReadonlyRecord<K0, A>,
  that: ReadonlyRecord<K1, B>
): Record<K0 | K1, A | B>

Merges two records, preserving only the entries that are unique to each record. Keys that exist in both records are excluded from the result.

Example (Keeping keys unique to each record)

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

assert.deepStrictEqual(
  Record.difference({ a: 1, b: 2 }, { b: 3, c: 4 }),
  { a: 1, c: 4 }
)
combining
Source effect/Record.ts:137731 lines
export const difference: {
  <K1 extends string, B>(
    that: ReadonlyRecord<K1, B>
  ): <K0 extends string, A>(self: ReadonlyRecord<K0, A>) => Record<K0 | K1, A | B>
  <K0 extends string, A, K1 extends string, B>(
    self: ReadonlyRecord<K0, A>,
    that: ReadonlyRecord<K1, B>
  ): Record<K0 | K1, A | B>
} = dual(2, <K0 extends string, A, K1 extends string, B>(
  self: ReadonlyRecord<K0, A>,
  that: ReadonlyRecord<K1, B>
): Record<K0 | K1, A | B> => {
  if (isEmptyRecord(self)) {
    return { ...that } as any
  }
  if (isEmptyRecord(that)) {
    return { ...self } as any
  }
  const out = {} as Record<K0 | K1, A | B>
  for (const key of keys(self)) {
    if (!has(that, key as any)) {
      out[key] = self[key]
    }
  }
  for (const key of keys(that)) {
    if (!has(self, key as any)) {
      out[key] = that[key]
    }
  }
  return out
})