ReadonlyStruct overview
This module targets readonly objects in the sense of product types. For readonly objects in the sense of maps see the Record
module.
Added in v0.14.0
Table of contents
3 Functions
get
Get the value for a key in a struct.
Signature
export declare const get: <K extends string>(k: K) => <A>(x: Readonly<Record<K, A>>) => A
get :: k extends string => k -> Readonly (Record k a) -> a
Example
import { get } from 'fp-ts-std/Struct'
type Person = { name: string; age: number }
const person: Person = { name: 'Albert', age: 76 }
const getName = get('name')
assert.strictEqual(getName(person), 'Albert')
Added in v0.17.0
merge
Merge two readonly structs together. For merging many identical structs, instead consider defining a semigroup.
Signature
export declare const merge: <A, B>(x: A) => <C extends B>(y: C) => A & C
merge :: c extends b => a -> c -> a & c
Example
import { merge } from 'fp-ts-std/ReadonlyStruct'
assert.deepStrictEqual(merge({ a: 1, b: 2 })({ b: 'two', c: true }), { a: 1, b: 'two', c: true })
Added in v0.14.0
omit
Omit a set of keys from a readonly struct. The value-level equivalent of the Omit
type.
Signature
export declare const omit: <K extends string>(
ks: readonly K[]
) => <V, A extends Readonly<Record<K, V>>>(x: A) => Pick<A, Exclude<keyof A, K>>
omit :: k extends string, a extends (Readonly (Record k v)) => Array k -> a -> Pick a (Exclude (keyof a) k)
Example
import { omit } from 'fp-ts-std/ReadonlyStruct'
const sansB = omit(['b'])
assert.deepStrictEqual(sansB({ a: 1, b: 'two', c: [true] }), { a: 1, c: [true] })
Added in v0.14.0
omitFrom
Like omit
, but allows you to specify the input struct upfront.
Signature
export declare const omitFrom: <A>() => <K extends keyof A & string>(
ks: readonly K[]
) => (x: A) => Pick<A, Exclude<keyof A, K>>
omitFrom :: k extends ((keyof a) & string) => () -> Array k -> a -> Pick a (Exclude (keyof a) k)
Example
import { omitFrom } from 'fp-ts-std/ReadonlyStruct'
type MyType = { a: number; b: string; c: ReadonlyArray<boolean> }
const sansB = omitFrom<MyType>()(['b'])
assert.deepStrictEqual(sansB({ a: 1, b: 'two', c: [true] }), { a: 1, c: [true] })
Added in v0.15.0
pick
Pick a set of keys from a readonly struct. The value-level equivalent of the Pick
type.
Signature
export declare const pick: <A extends object, K extends keyof A>(ks: readonly K[]) => (x: A) => Pick<A, K>
pick :: a extends object, k extends (keyof a) => Array k -> a -> Pick a k
Example
import { pick } from 'fp-ts-std/ReadonlyStruct'
import { pipe } from 'fp-ts/function'
const picked = pipe({ a: 1, b: 'two', c: [true] }, pick(['a', 'c']))
assert.deepStrictEqual(picked, { a: 1, c: [true] })
Added in v0.14.0
pickFrom
Like pick
, but allows you to specify the input struct upfront.
Signature
export declare const pickFrom: <A extends object>() => <K extends keyof A>(ks: readonly K[]) => (x: A) => Pick<A, K>
pickFrom :: a extends object, k extends (keyof a) => () -> Array k -> a -> Pick a k
Example
import { pickFrom } from 'fp-ts-std/ReadonlyStruct'
type MyType = { a: number; b: string; c: ReadonlyArray<boolean> }
const picked = pickFrom<MyType>()(['a', 'c'])
assert.deepStrictEqual(picked({ a: 1, b: 'two', c: [true] }), { a: 1, c: [true] })
Added in v0.14.0
renameKey
Rename a key in a struct, preserving the value. If the new key already exists, the old key will be overwritten. Optionality is preserved.
Signature
export declare const renameKey: <I extends string>(
oldK: I
) => <J extends string>(
newK: J
) => <A extends MaybePartial<Readonly<Record<I, unknown>>>>(
x: A
) => Readonly<{ [K in keyof A as K extends I ? J : K]: A[K] }>
renameKey :: i extends string, j extends string, a extends (MaybePartial (Readonly (Record i unknown))) => i -> j -> a -> Readonly { [k in (keyof a) as k extends i ? j : k]: a[k] }
Example
import { renameKey } from 'fp-ts-std/Struct'
type Foo = { a: string; b: number }
type Bar = { a: string; c: number }
const fooBar: (x: Foo) => Bar = renameKey('b')('c')
Added in v0.15.0
withDefaults
Provide default values for an object with optional properties.
Signature
export declare const withDefaults: <
T extends Readonly<Record<string, unknown>>,
PT extends Exact<{ [K in OptionalKeys<T>]-?: Exclude<T[K], undefined> }, PT>
>(
defaults: PT
) => (t: T) => Readonly<PT & T>
Example
import { withDefaults } from 'fp-ts-std/ReadonlyStruct'
import { pipe } from 'fp-ts/function'
const aOptB: { a: number; b?: string } = { a: 1 }
assert.deepStrictEqual(pipe(aOptB, withDefaults({ b: 'foo' })), { a: 1, b: 'foo' })
Added in v0.15.0