Hyperlinkv0.8.0-beta.28

Optic

Optic.entriesfunctioneffect/Optic.ts:1509
<A>(): Iso<Record<string, A>, ReadonlyArray<readonly [string, A]>>

Iso that converts a Record<string, A> to an array of [key, value] entries and back.

When to use

Use when you want to traverse or manipulate record entries as an array (e.g. with .forEach()).

Details

  • get uses Object.entries.
  • set uses Object.fromEntries.
  • Round-trip is lossless for Record<string, A>.

Example (Traversing record values)

import { Optic, Schema } from "effect"

const _positiveValues = Optic.entries<number>()
  .forEach((entry) => entry.key(1).check(Schema.isGreaterThan(0)))

const inc = _positiveValues.modifyAll((n) => n + 1)

console.log(inc({ a: 0, b: 3, c: -1 }))
// Output: { a: 0, b: 4, c: -1 }
Source effect/Optic.ts:15093 lines
export function entries<A>(): Iso<Record<string, A>, ReadonlyArray<readonly [string, A]>> {
  return make(new IsoNode(Object.entries, Object.fromEntries))
}