<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
getusesObject.entries.setusesObject.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 }export function function entries<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 }
entries<function (type parameter) A in entries<A>(): Iso<Record<string, A>, ReadonlyArray<readonly [string, A]>>A>(): interface Iso<in out S, in out A>A lossless, reversible conversion between types S and A.
When to use
Use when you have a pair of functions that convert back and forth without losing
information (e.g. Record ↔ entries, Celsius ↔ Fahrenheit).
- You want the strongest optic that can be composed with any other.
Details
get(s) always succeeds and returns an A.
set(a) always succeeds and returns an S.
get(set(a)) === a and set(get(s)) equals s (round-trip laws).
- Extends both
Lens
and
Prism
.
Example (Converting between Celsius and Fahrenheit)
import { Optic } from "effect"
const fahrenheit = Optic.makeIso<number, number>(
(c) => c * 9 / 5 + 32,
(f) => (f - 32) * 5 / 9
)
console.log(fahrenheit.get(100))
// Output: 212
console.log(fahrenheit.set(32))
// Output: 0
Iso<type Record<K extends keyof any, T> = {
[P in K]: T
}
Construct a type with a set of properties K of type T
Record<string, function (type parameter) A in entries<A>(): Iso<Record<string, A>, ReadonlyArray<readonly [string, A]>>A>, interface ReadonlyArray<T>ReadonlyArray<readonly [string, function (type parameter) A in entries<A>(): Iso<Record<string, A>, ReadonlyArray<readonly [string, A]>>A]>> {
return function make(node: Node): anymake(new constructor IsoNode<{}, [string, any][]>(get: (s: {}) => [string, any][], set: (a: [string, any][]) => {}): IsoNode<{}, [string, any][]>IsoNode(var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.entries<T>(o: {
[s: string]: T;
} | ArrayLike<T>): [string, T][] (+1 overload)
Returns an array of key/values of the enumerable own properties of an object
entries, var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.fromEntries<T = any>(entries: Iterable<readonly [PropertyKey, T]>): {
[k: string]: T;
} (+1 overload)
Returns an object created by key-value entries for properties and methods
fromEntries))
}