Link Search Menu Expand Document

ReadonlyRecord overview

Added in v2.5.0


Table of contents


Compactable

compact

Signature

export declare const compact: <A>(r: Readonly<Record<string, O.Option<A>>>) => Readonly<Record<string, A>>

Added in v2.5.0

separate

Signature

export declare const separate: <A, B>(
  r: Readonly<Record<string, Either<A, B>>>
) => Separated<Readonly<Record<string, A>>, Readonly<Record<string, B>>>

Added in v2.5.0

Filterable

filter

Signature

export declare const filter: {
  <A, B extends A>(refinement: Refinement<A, B>): (fa: Readonly<Record<string, A>>) => Readonly<Record<string, B>>
  <A>(predicate: Predicate<A>): (fa: Readonly<Record<string, A>>) => Readonly<Record<string, A>>
}

Added in v2.5.0

filterMap

Signature

export declare const filterMap: <A, B>(
  f: (a: A) => O.Option<B>
) => (fa: Readonly<Record<string, A>>) => Readonly<Record<string, B>>

Added in v2.5.0

partition

Signature

export declare const partition: {
  <A, B extends A>(refinement: Refinement<A, B>): (
    fa: Readonly<Record<string, A>>
  ) => Separated<Readonly<Record<string, A>>, Readonly<Record<string, B>>>
  <A>(predicate: Predicate<A>): (
    fa: Readonly<Record<string, A>>
  ) => Separated<Readonly<Record<string, A>>, Readonly<Record<string, A>>>
}

Added in v2.5.0

partitionMap

Signature

export declare const partitionMap: <A, B, C>(
  f: (a: A) => Either<B, C>
) => (fa: Readonly<Record<string, A>>) => Separated<Readonly<Record<string, B>>, Readonly<Record<string, C>>>

Added in v2.5.0

Foldable

foldMap

Signature

export declare const foldMap: <M>(M: Monoid<M>) => <A>(f: (a: A) => M) => (fa: Readonly<Record<string, A>>) => M

Added in v2.5.0

reduce

Signature

export declare const reduce: <A, B>(b: B, f: (b: B, a: A) => B) => (fa: Readonly<Record<string, A>>) => B

Added in v2.5.0

reduceRight

Signature

export declare const reduceRight: <A, B>(b: B, f: (a: A, b: B) => B) => (fa: Readonly<Record<string, A>>) => B

Added in v2.5.0

Witherable

wilt

Signature

export declare const wilt: PipeableWilt1<'ReadonlyRecord'>

Added in v2.6.5

wither

Signature

export declare const wither: PipeableWither1<'ReadonlyRecord'>

Added in v2.6.5

combinators

deleteAt

Delete a key and value from a ReadonlyRecord.

Signature

export declare function deleteAt<K extends string>(
  k: K
): <KS extends string, A>(r: ReadonlyRecord<KS, A>) => ReadonlyRecord<string extends K ? string : Exclude<KS, K>, A>

Added in v2.5.0

filterMapWithIndex

Signature

export declare function filterMapWithIndex<K extends string, A, B>(
  f: (key: K, a: A) => Option<B>
): (fa: ReadonlyRecord<K, A>) => ReadonlyRecord<string, B>

Added in v2.5.0

flap

Derivable from Functor.

Signature

export declare const flap: <A>(a: A) => <B>(fab: Readonly<Record<string, (a: A) => B>>) => Readonly<Record<string, B>>

Added in v2.10.0

map

Map a ReadonlyRecord passing the values to the iterating function.

Signature

export declare function map<A, B>(f: (a: A) => B): <K extends string>(fa: ReadonlyRecord<K, A>) => ReadonlyRecord<K, B>

Added in v2.5.0

mapWithIndex

Map a ReadonlyRecord passing the keys to the iterating function.

Signature

export declare function mapWithIndex<K extends string, A, B>(
  f: (k: K, a: A) => B
): (fa: ReadonlyRecord<K, A>) => ReadonlyRecord<K, B>

Added in v2.5.0

upsertAt

Insert or replace a key/value pair in a ReadonlyRecord.

Signature

export declare const upsertAt: <A>(k: string, a: A) => (r: Readonly<Record<string, A>>) => Readonly<Record<string, A>>

Added in v2.10.0

insertAt

Use upsertAt instead.

Signature

export declare const insertAt: <A>(k: string, a: A) => (r: Readonly<Record<string, A>>) => Readonly<Record<string, A>>

Added in v2.5.0

constructors

singleton

Create a ReadonlyRecord with one key/value pair.

Signature

export declare const singleton: <A>(k: string, a: A) => Readonly<Record<string, A>>

Added in v2.5.0

destructors

toUnfoldable

Unfolds a ReadonlyRecord into a list of key/value pairs.

Signature

export declare function toUnfoldable<F extends URIS>(
  U: Unfoldable1<F>
): <K extends string, A>(r: ReadonlyRecord<K, A>) => Kind<F, readonly [K, A]>
export declare function toUnfoldable<F>(
  U: Unfoldable<F>
): <K extends string, A>(r: ReadonlyRecord<K, A>) => HKT<F, readonly [K, A]>

Added in v2.5.0

instances

Compactable

Signature

export declare const Compactable: Compactable1<'ReadonlyRecord'>

Added in v2.7.0

Filterable

Signature

export declare const Filterable: Filterable1<'ReadonlyRecord'>

Added in v2.7.0

FilterableWithIndex

Signature

export declare const FilterableWithIndex: FilterableWithIndex1<'ReadonlyRecord', string>

Added in v2.7.0

Foldable

Signature

export declare const Foldable: Foldable1<'ReadonlyRecord'>

Added in v2.7.0

FoldableWithIndex

Signature

export declare const FoldableWithIndex: FoldableWithIndex1<'ReadonlyRecord', string>

Added in v2.7.0

Functor

Signature

export declare const Functor: Functor1<'ReadonlyRecord'>

Added in v2.7.0

FunctorWithIndex

Signature

export declare const FunctorWithIndex: FunctorWithIndex1<'ReadonlyRecord', string>

Added in v2.7.0

Traversable

Signature

export declare const Traversable: Traversable1<'ReadonlyRecord'>

Added in v2.7.0

TraversableWithIndex

Signature

export declare const TraversableWithIndex: TraversableWithIndex1<'ReadonlyRecord', string>

Added in v2.7.0

URI

Signature

export declare const URI: 'ReadonlyRecord'

Added in v2.5.0

URI (type alias)

Signature

export type URI = typeof URI

Added in v2.5.0

Witherable

Signature

export declare const Witherable: Witherable1<'ReadonlyRecord'>

Added in v2.7.0

getEq

Signature

export declare function getEq<K extends string, A>(E: Eq<A>): Eq<ReadonlyRecord<K, A>>

Added in v2.5.0

getMonoid

Returns a Monoid instance for ReadonlyRecords given a Semigroup instance for their values.

Signature

export declare function getMonoid<K extends string, A>(S: Semigroup<A>): Monoid<ReadonlyRecord<K, A>>

Example

import { SemigroupSum } from 'fp-ts/number'
import { getMonoid } from 'fp-ts/ReadonlyRecord'

const M = getMonoid(SemigroupSum)
assert.deepStrictEqual(M.concat({ foo: 123 }, { foo: 456 }), { foo: 579 })

Added in v2.5.0

getShow

Signature

export declare function getShow<A>(S: Show<A>): Show<ReadonlyRecord<string, A>>

Added in v2.5.0

readonlyRecord

Use small, specific instances instead.

Signature

export declare const readonlyRecord: FunctorWithIndex1<'ReadonlyRecord', string> &
  FoldableWithIndex1<'ReadonlyRecord', string> &
  FilterableWithIndex1<'ReadonlyRecord', string> &
  TraversableWithIndex1<'ReadonlyRecord', string> &
  Witherable1<'ReadonlyRecord'>

Added in v2.5.0

interop

fromRecord

Signature

export declare function fromRecord<K extends string, A>(r: Record<K, A>): ReadonlyRecord<K, A>

Added in v2.5.0

toRecord

Signature

export declare function toRecord<K extends string, A>(r: ReadonlyRecord<K, A>): Record<K, A>

Added in v2.5.0

model

ReadonlyRecord (type alias)

Signature

export type ReadonlyRecord<K extends string, T> = Readonly<Record<K, T>>

Added in v2.5.0

utils

collect

Map a ReadonlyRecord into an ReadonlyArray.

Signature

export declare const collect: <K extends string, A, B>(
  f: (k: K, a: A) => B
) => (r: Readonly<Record<K, A>>) => readonly B[]

Example

import { collect } from 'fp-ts/ReadonlyRecord'

const x: { readonly a: string; readonly b: boolean } = { a: 'c', b: false }
assert.deepStrictEqual(collect((key, val) => ({ key: key, value: val }))(x), [
  { key: 'a', value: 'c' },
  { key: 'b', value: false },
])

Added in v2.5.0

elem

Signature

export declare function elem<A>(
  E: Eq<A>
): {
  (a: A): (fa: ReadonlyRecord<string, A>) => boolean
  (a: A, fa: ReadonlyRecord<string, A>): boolean
}

Added in v2.5.0

empty

Signature

export declare const empty: Readonly<Record<string, never>>

Added in v2.5.0

every

Signature

export declare function every<A>(predicate: Predicate<A>): (r: ReadonlyRecord<string, A>) => boolean

Added in v2.5.0

filterWithIndex

Signature

export declare function filterWithIndex<K extends string, A, B extends A>(
  refinementWithIndex: RefinementWithIndex<K, A, B>
): (fa: ReadonlyRecord<K, A>) => ReadonlyRecord<string, B>
export declare function filterWithIndex<K extends string, A>(
  predicateWithIndex: PredicateWithIndex<K, A>
): (fa: ReadonlyRecord<K, A>) => ReadonlyRecord<string, A>

Added in v2.5.0

foldMapWithIndex

Signature

export declare function foldMapWithIndex<M>(
  M: Monoid<M>
): <K extends string, A>(f: (k: K, a: A) => M) => (fa: ReadonlyRecord<K, A>) => M

Added in v2.5.0

fromFoldable

Create a ReadonlyRecord from a foldable collection of key/value pairs, using the specified Magma to combine values for duplicate keys.

Signature

export declare function fromFoldable<F extends URIS3, A>(
  M: Magma<A>,
  F: Foldable3<F>
): <R, E>(fka: Kind3<F, R, E, readonly [string, A]>) => ReadonlyRecord<string, A>
export declare function fromFoldable<F extends URIS2, A>(
  M: Magma<A>,
  F: Foldable2<F>
): <E>(fka: Kind2<F, E, readonly [string, A]>) => ReadonlyRecord<string, A>
export declare function fromFoldable<F extends URIS, A>(
  M: Magma<A>,
  F: Foldable1<F>
): (fka: Kind<F, readonly [string, A]>) => ReadonlyRecord<string, A>
export declare function fromFoldable<F, A>(
  M: Magma<A>,
  F: FoldableHKT<F>
): (fka: HKT<F, readonly [string, A]>) => ReadonlyRecord<string, A>

Added in v2.5.0

fromFoldableMap

Create a ReadonlyRecord from a foldable collection using the specified functions to:

  • map to key/value pairs
  • combine values for duplicate keys.

Signature

export declare function fromFoldableMap<F extends URIS3, B>(
  M: Magma<B>,
  F: Foldable3<F>
): <R, E, A>(fa: Kind3<F, R, E, A>, f: (a: A) => readonly [string, B]) => ReadonlyRecord<string, B>
export declare function fromFoldableMap<F extends URIS2, B>(
  M: Magma<B>,
  F: Foldable2<F>
): <E, A>(fa: Kind2<F, E, A>, f: (a: A) => readonly [string, B]) => ReadonlyRecord<string, B>
export declare function fromFoldableMap<F extends URIS, B>(
  M: Magma<B>,
  F: Foldable1<F>
): <A>(fa: Kind<F, A>, f: (a: A) => readonly [string, B]) => ReadonlyRecord<string, B>
export declare function fromFoldableMap<F, B>(
  M: Magma<B>,
  F: FoldableHKT<F>
): <A>(fa: HKT<F, A>, f: (a: A) => readonly [string, B]) => ReadonlyRecord<string, B>

Example

import { last } from 'fp-ts/Semigroup'
import { Foldable, zip } from 'fp-ts/ReadonlyArray'
import { identity } from 'fp-ts/function'
import { ReadonlyRecord, fromFoldableMap } from 'fp-ts/ReadonlyRecord'

export const zipObject = <K extends string, A>(
  keys: ReadonlyArray<K>,
  values: ReadonlyArray<A>
): ReadonlyRecord<K, A> => fromFoldableMap(last<A>(), Foldable)(zip(keys, values), identity)

assert.deepStrictEqual(zipObject(['a', 'b'], [1, 2, 3]), { a: 1, b: 2 })

interface User {
  readonly id: string
  readonly name: string
}

const users: ReadonlyArray<User> = [
  { id: 'id1', name: 'name1' },
  { id: 'id2', name: 'name2' },
  { id: 'id1', name: 'name3' },
]

assert.deepStrictEqual(
  fromFoldableMap(last<User>(), Foldable)(users, (user) => [user.id, user]),
  {
    id1: { id: 'id1', name: 'name3' },
    id2: { id: 'id2', name: 'name2' },
  }
)

Added in v2.5.0

has

Test whether or not a key exists in a ReadonlyRecord.

Note. This function is not pipeable because is a custom type guard.

Signature

export declare const has: <K extends string>(k: string, r: Readonly<Record<K, unknown>>) => k is K

Added in v2.10.0

isEmpty

Test whether a ReadonlyRecord is empty.

Signature

export declare const isEmpty: (r: ReadonlyRecord<string, unknown>) => boolean

Added in v2.5.0

isSubrecord

Test whether one ReadonlyRecord contains all of the keys and values contained in another ReadonlyRecord.

Signature

export declare function isSubrecord<A>(
  E: Eq<A>
): {
  (that: ReadonlyRecord<string, A>): (me: ReadonlyRecord<string, A>) => boolean
  (me: ReadonlyRecord<string, A>, that: ReadonlyRecord<string, A>): boolean
}

Added in v2.5.0

keys

Signature

export declare const keys: <K extends string>(r: Readonly<Record<K, unknown>>) => readonly K[]

Added in v2.5.0

lookup

Lookup the value for a key in a ReadonlyRecord.

Signature

export declare function lookup(k: string): <A>(r: ReadonlyRecord<string, A>) => Option<A>
export declare function lookup<A>(k: string, r: ReadonlyRecord<string, A>): Option<A>

Added in v2.5.0

modifyAt

Signature

export declare const modifyAt: <A>(
  k: string,
  f: (a: A) => A
) => <K extends string>(r: Readonly<Record<K, A>>) => O.Option<Readonly<Record<K, A>>>

Added in v2.5.0

partitionMapWithIndex

Signature

export declare function partitionMapWithIndex<K extends string, A, B, C>(
  f: (key: K, a: A) => Either<B, C>
): (fa: ReadonlyRecord<K, A>) => Separated<ReadonlyRecord<string, B>, ReadonlyRecord<string, C>>

Added in v2.5.0

partitionWithIndex

Signature

export declare function partitionWithIndex<K extends string, A, B extends A>(
  refinementWithIndex: RefinementWithIndex<K, A, B>
): (fa: ReadonlyRecord<K, A>) => Separated<ReadonlyRecord<string, A>, ReadonlyRecord<string, B>>
export declare function partitionWithIndex<K extends string, A>(
  predicateWithIndex: PredicateWithIndex<K, A>
): (fa: ReadonlyRecord<K, A>) => Separated<ReadonlyRecord<string, A>, ReadonlyRecord<string, A>>

Added in v2.5.0

pop

Delete a key and value from a ReadonlyRecord, returning the value as well as the subsequent ReadonlyRecord.

Signature

export declare function pop<K extends string>(
  k: K
): <KS extends string, A>(
  r: ReadonlyRecord<KS, A>
) => Option<readonly [A, ReadonlyRecord<string extends K ? string : Exclude<KS, K>, A>]>

Added in v2.5.0

reduceRightWithIndex

Signature

export declare function reduceRightWithIndex<K extends string, A, B>(
  b: B,
  f: (k: K, a: A, b: B) => B
): (fa: ReadonlyRecord<K, A>) => B

Added in v2.5.0

reduceWithIndex

Signature

export declare function reduceWithIndex<K extends string, A, B>(
  b: B,
  f: (k: K, b: B, a: A) => B
): (fa: ReadonlyRecord<K, A>) => B

Added in v2.5.0

sequence

Signature

export declare function sequence<F extends URIS3>(
  F: Applicative3<F>
): <K extends string, R, E, A>(ta: ReadonlyRecord<K, Kind3<F, R, E, A>>) => Kind3<F, R, E, ReadonlyRecord<K, A>>
export declare function sequence<F extends URIS3, E>(
  F: Applicative3C<F, E>
): <K extends string, R, A>(ta: ReadonlyRecord<K, Kind3<F, R, E, A>>) => Kind3<F, R, E, ReadonlyRecord<K, A>>
export declare function sequence<F extends URIS2>(
  F: Applicative2<F>
): <K extends string, E, A>(ta: ReadonlyRecord<K, Kind2<F, E, A>>) => Kind2<F, E, ReadonlyRecord<K, A>>
export declare function sequence<F extends URIS2, E>(
  F: Applicative2C<F, E>
): <K extends string, A>(ta: ReadonlyRecord<K, Kind2<F, E, A>>) => Kind2<F, E, ReadonlyRecord<K, A>>
export declare function sequence<F extends URIS>(
  F: Applicative1<F>
): <K extends string, A>(ta: ReadonlyRecord<K, Kind<F, A>>) => Kind<F, ReadonlyRecord<K, A>>
export declare function sequence<F>(
  F: Applicative<F>
): <K extends string, A>(ta: ReadonlyRecord<K, HKT<F, A>>) => HKT<F, ReadonlyRecord<K, A>>

Added in v2.5.0

size

Calculate the number of key/value pairs in a ReadonlyRecord,

Signature

export declare const size: (r: ReadonlyRecord<string, unknown>) => number

Added in v2.5.0

some

Signature

export declare function some<A>(predicate: (a: A) => boolean): (r: ReadonlyRecord<string, A>) => boolean

Added in v2.5.0

toReadonlyArray

Get a sorted ReadonlyArray of the key/value pairs contained in a ReadonlyRecord.

Signature

export declare const toReadonlyArray: <K extends string, A>(r: Readonly<Record<K, A>>) => readonly (readonly [K, A])[]

Added in v2.5.0

traverse

Signature

export declare function traverse<F extends URIS3>(
  F: Applicative3<F>
): <R, E, A, B>(
  f: (a: A) => Kind3<F, R, E, B>
) => <K extends string>(ta: ReadonlyRecord<K, A>) => Kind3<F, R, E, ReadonlyRecord<K, B>>
export declare function traverse<F extends URIS3, E>(
  F: Applicative3C<F, E>
): <R, A, B>(
  f: (a: A) => Kind3<F, R, E, B>
) => <K extends string>(ta: ReadonlyRecord<K, A>) => Kind3<F, R, E, ReadonlyRecord<K, B>>
export declare function traverse<F extends URIS2>(
  F: Applicative2<F>
): <E, A, B>(
  f: (a: A) => Kind2<F, E, B>
) => <K extends string>(ta: ReadonlyRecord<K, A>) => Kind2<F, E, ReadonlyRecord<K, B>>
export declare function traverse<F extends URIS2, E>(
  F: Applicative2C<F, E>
): <A, B>(
  f: (a: A) => Kind2<F, E, B>
) => <K extends string>(ta: ReadonlyRecord<K, A>) => Kind2<F, E, ReadonlyRecord<K, B>>
export declare function traverse<F extends URIS>(
  F: Applicative1<F>
): <A, B>(f: (a: A) => Kind<F, B>) => <K extends string>(ta: ReadonlyRecord<K, A>) => Kind<F, ReadonlyRecord<K, B>>
export declare function traverse<F>(
  F: Applicative<F>
): <A, B>(f: (a: A) => HKT<F, B>) => <K extends string>(ta: ReadonlyRecord<K, A>) => HKT<F, ReadonlyRecord<K, B>>

Added in v2.5.0

traverseWithIndex

Signature

export declare function traverseWithIndex<F extends URIS3>(
  F: Applicative3<F>
): <K extends string, R, E, A, B>(
  f: (k: K, a: A) => Kind3<F, R, E, B>
) => (ta: ReadonlyRecord<K, A>) => Kind3<F, R, E, ReadonlyRecord<K, B>>
export declare function traverseWithIndex<F extends URIS3, E>(
  F: Applicative3C<F, E>
): <K extends string, R, A, B>(
  f: (k: K, a: A) => Kind3<F, R, E, B>
) => (ta: ReadonlyRecord<K, A>) => Kind3<F, R, E, ReadonlyRecord<K, B>>
export declare function traverseWithIndex<F extends URIS2>(
  F: Applicative2<F>
): <K extends string, E, A, B>(
  f: (k: K, a: A) => Kind2<F, E, B>
) => (ta: ReadonlyRecord<K, A>) => Kind2<F, E, ReadonlyRecord<K, B>>
export declare function traverseWithIndex<F extends URIS2, E>(
  F: Applicative2C<F, E>
): <K extends string, A, B>(
  f: (k: K, a: A) => Kind2<F, E, B>
) => (ta: ReadonlyRecord<K, A>) => Kind2<F, E, ReadonlyRecord<K, B>>
export declare function traverseWithIndex<F extends URIS>(
  F: Applicative1<F>
): <K extends string, A, B>(
  f: (k: K, a: A) => Kind<F, B>
) => (ta: ReadonlyRecord<K, A>) => Kind<F, ReadonlyRecord<K, B>>
export declare function traverseWithIndex<F>(
  F: Applicative<F>
): <K extends string, A, B>(f: (k: K, a: A) => HKT<F, B>) => (ta: ReadonlyRecord<K, A>) => HKT<F, ReadonlyRecord<K, B>>

Added in v2.5.0

updateAt

Signature

export declare const updateAt: <A>(
  k: string,
  a: A
) => <K extends string>(r: Readonly<Record<K, A>>) => O.Option<Readonly<Record<K, A>>>

Added in v2.5.0

hasOwnProperty (function)

Use has instead.

Signature

export declare function hasOwnProperty<K extends string>(k: string, r: ReadonlyRecord<K, unknown>): k is K

Added in v2.5.0