Hyperlinkv0.8.0-beta.28

Record

Record.modifyconsteffect/Record.ts:464
<K extends string | symbol, A, B>(key: NoInfer<K>, f: (a: A) => B): (
  self: ReadonlyRecord<K, A>
) => Option.Option<Record<K, A | B>>
<K extends string | symbol, A, B>(
  self: ReadonlyRecord<K, A>,
  key: NoInfer<K>,
  f: (a: A) => B
): Option.Option<Record<K, A | B>>

Applies a function to the element at the specified key safely, creating a new record, or return Option.none() if the key doesn't exist.

Example (Modifying a value at a key)

import { Record } from "effect"

const f = (x: number) => x * 2

const input: Record<string, number> = { a: 3 }

Record.modify(input, "a", f) // Option.some({ a: 6 })
Record.modify(input, "b", f) // Option.none()
mutations
Source effect/Record.ts:46421 lines
export const modify: {
  <K extends string | symbol, A, B>(
    key: NoInfer<K>,
    f: (a: A) => B
  ): (self: ReadonlyRecord<K, A>) => Option.Option<Record<K, A | B>>
  <K extends string | symbol, A, B>(
    self: ReadonlyRecord<K, A>,
    key: NoInfer<K>,
    f: (a: A) => B
  ): Option.Option<Record<K, A | B>>
} = dual(
  3,
  <K extends string | symbol, A, B>(
    self: ReadonlyRecord<K, A>,
    key: NoInfer<K>,
    f: (a: A) => B
  ): Option.Option<Record<K, A | B>> => {
    if (!has(self, key)) return Option.none()
    return Option.some({ ...self, [key]: f(self[key]) })
  }
)
Referenced by 1 symbols