Skip to content

Rec

const Rec: object

Defined in: Data/Rec.ts:704

compact: <A>(record) => Readonly<Record<string, A>>

Removes all None values from a Record<string, Maybe<A>>, returning a plain Record<string, A>. Useful when building records from fallible lookups.

A

Readonly<Record<string, Maybe<A>>>

Readonly<Record<string, A>>

Rec.compact({ a: Maybe.make.some(1), b: Maybe.make.none(), c: Maybe.make.some(3) });
// { a: 1, c: 3 }

entries: <T>(record) => readonly (readonly [keyof T, T[keyof T]])[]

Returns all key-value pairs of a record.

T extends Record<string, unknown>

T

readonly (readonly [keyof T, T[keyof T]])[]

  • keys for extracting record keys.
  • values for extracting record values.
Rec.entries({ a: 1, b: 2 }); // [["a", 1], ["b", 2]]

filter: <A>(predicate) => (record) => Readonly<Record<string, A>>

Filters values in a record by a predicate.

A

(value) => boolean

(record) => Readonly<Record<string, A>>

pipe({ a: 1, b: 2, c: 3 }, Rec.filter(n => n > 1)); // { b: 2, c: 3 }

filterMap: <A, B>(transform) => (record) => Readonly<Record<string, B>>

Maps each value in a record with a function returning a Maybe, keeping only Some values.

A

B

(value) => Maybe<B>

(record) => Readonly<Record<string, B>>

filter for filtering with a boolean predicate.

pipe(
  { a: 1, b: 2, c: 3 },
  Rec.filterMap((n) => (n % 2 === 0 ? Maybe.make.some(n * 10) : Maybe.make.none()))
); // { b: 20 }

filterWithKey: <A>(predicate) => (record) => Readonly<Record<string, A>>

Filters values in a record by a predicate that also receives the key.

A

(key, value) => boolean

(record) => Readonly<Record<string, A>>

filter for filtering values without key access.

pipe({ a: 1, b: 2, c: 3 }, Rec.filterWithKey((k, v) => k !== "a" && v > 0));
// { b: 2 }

from: object = RecFrom

entries: <A>(pairs) => Readonly<Record<string, A>>

Creates a record from key-value pairs.

A

readonly (readonly [string, A])[]

Readonly<Record<string, A>>

Rec.from.entries([["a", 1], ["b", 2]]); // { a: 1, b: 2 }

groupBy: <A>(keySelector) => (items) => Readonly<Record<string, readonly A[]>>

Groups elements of an array into a record keyed by the result of keySelector. Each key maps to the array of elements that produced it, in insertion order.

Unlike Dict.groupBy, keys are always strings. Use Dict.groupBy when you need non-string keys or want to avoid the plain-object prototype chain.

A

(item) => string

(items) => Readonly<Record<string, readonly A[]>>

pipe(
  ["apple", "avocado", "banana", "blueberry"],
  Rec.groupBy(s => s[0]),
); // { a: ["apple", "avocado"], b: ["banana", "blueberry"] }

is: object = RecIs

empty: <A>(record) => boolean

Returns true if the record has no keys.

A

Readonly<Record<string, A>>

boolean

nonEmpty for checking if a record has keys.

Rec.is.empty({});       // true
Rec.is.empty({ a: 1 }); // false

nonEmpty: <A, K>(record) => record is NonEmptyRecord<A, K> = _isNonEmpty

Type guard to check if a record is non-empty.

A

K extends string

Readonly<Record<K, A>>

record is NonEmptyRecord<A, K>

empty for checking if a record has no keys.

Rec.is.nonEmpty({ a: 1 }); // true
Rec.is.nonEmpty({});       // false

keys: <T>(record) => readonly (keyof T & string)[]

Returns all keys of a record.

T extends Record<string, unknown>

T

readonly (keyof T & string)[]

  • values for extracting record values.
  • entries for extracting key-value pairs.
Rec.keys({ a: 1, b: 2 }); // ["a", "b"]

lookup: <K>(key) => <V>(record) => Maybe<V>

Looks up a value by key, returning Maybe.

K extends string

K

<V>(record) => Maybe<V>

pipe({ a: 1, b: 2 }, Rec.lookup("a")); // Some(1)
pipe({ a: 1, b: 2 }, Rec.lookup("c")); // None

map: <A, B>(transform) => <K>(record) => Readonly<Record<K, B>>

Transforms each value in a record.

A

B

(value) => B

<K>(record) => Readonly<Record<K, B>>

  • mapWithKey for transforming values with key access.
  • mapKeys for transforming keys while preserving values.
  • mapEntries for transforming keys and values simultaneously.
pipe({ a: 1, b: 2 }, Rec.map(n => n * 2)); // { a: 2, b: 4 }

mapEntries: <A, K2, B>(transform) => (record) => Readonly<Record<K2, B>>

Transforms key and value pairs simultaneously.

A

K2 extends string

B

(key, value) => readonly [K2, B]

(record) => Readonly<Record<K2, B>>

  • map for transforming values.
  • mapKeys for transforming keys.
pipe(
  { a: 1, b: 2 },
  Rec.mapEntries((k, v) => [k.toUpperCase(), v * 10])
); // { A: 10, B: 20 }

mapKeys: (transform) => <A>(record) => Readonly<Record<string, A>>

Transforms each key while preserving values. If two keys map to the same new key, the last one wins.

(key) => string

<A>(record) => Readonly<Record<string, A>>

  • map for transforming values.
  • mapEntries for transforming keys and values simultaneously.
pipe({ firstName: "Alice", lastName: "Smith" }, Rec.mapKeys(k => k.toUpperCase()));
// { FIRSTNAME: "Alice", LASTNAME: "Smith" }

mapWithKey: <A, B>(transform) => <K>(record) => Readonly<Record<K, B>>

Transforms each value in a record, also receiving the key.

A

B

(key, value) => B

<K>(record) => Readonly<Record<K, B>>

map for transforming values without key access.

pipe({ a: 1, b: 2 }, Rec.mapWithKey((k, v) => `${k}:${v}`));
// { a: "a:1", b: "b:2" }

merge: <A>(other) => (record) => Readonly<Record<string, A>>

Merges two records. Values from the second record take precedence.

A

Readonly<Record<string, A>>

(record) => Readonly<Record<string, A>>

mergeWith for merging records with custom collision resolution.

pipe({ a: 1, b: 2 }, Rec.merge({ b: 3, c: 4 })); // { a: 1, b: 3, c: 4 }

mergeWith: <A>(combine) => {(second): (first) => Readonly<Record<string, A>>; (first, second): Readonly<Record<string, A>>; }

Merges two records using a custom combination function on key collisions. Supports both uncurried Rec.mergeWith(combine)(first, second) and curried pipe(first, Rec.mergeWith(combine)(second)).

A

(first, second) => A

{(second): (first) => Readonly<Record<string, A>>; (first, second): Readonly<Record<string, A>>; }

merge for shallow merging with right-precedence.

const combineStats = Rec.mergeWith((a: number, b: number) => a + b);
combineStats({ a: 1, b: 2 }, { b: 3, c: 4 }); // { a: 1, b: 5, c: 4 }
pipe({ a: 1, b: 2 }, combineStats({ b: 3, c: 4 })); // { a: 1, b: 5, c: 4 }

NonEmpty: object = RecNonEmpty

Utilities for working with guaranteed non-empty records (NonEmptyRecord).

entries: <K, A>(record) => NonEmptyArr<readonly [K, A]>

Returns all entries of a non-empty record as a NonEmptyArr.

K extends string

A

NonEmptyRecord<A, K>

NonEmptyArr<readonly [K, A]>

Rec.NonEmpty.entries({ a: 1, b: 2 } as Rec.NonEmpty<number>); // [["a", 1], ["b", 2]]

from: object

record: <K, A>(record) => Maybe<NonEmptyRecord<A, K>>

Refines a standard record to a NonEmptyRecord, returning Some if it has keys, None otherwise.

K extends string

A

Readonly<Record<K, A>>

Maybe<NonEmptyRecord<A, K>>

Rec.NonEmpty.from.record({ a: 1 }); // Some({ a: 1 })
Rec.NonEmpty.from.record({});       // None

keys: <K, A>(record) => NonEmptyArr<K>

Returns all keys of a non-empty record as a NonEmptyArr.

K extends string

A

NonEmptyRecord<A, K>

NonEmptyArr<K>

Rec.NonEmpty.keys({ a: 1, b: 2 } as Rec.NonEmpty<number>); // ["a", "b"]

map: <A, B>(transform) => <K>(record) => NonEmptyRecord<B, K>

Transforms each value of a non-empty record, preserving non-emptiness.

A

B

(value) => B

<K>(record) => NonEmptyRecord<B, K>

pipe({ a: 1, b: 2 } as Rec.NonEmpty<number>, Rec.NonEmpty.map(n => n * 2)); // { a: 2, b: 4 }

mapWithKey: <A, B>(transform) => <K>(record) => NonEmptyRecord<B, K>

Transforms each value of a non-empty record with key access, preserving non-emptiness.

A

B

(key, value) => B

<K>(record) => NonEmptyRecord<B, K>

pipe({ a: 1, b: 2 } as Rec.NonEmpty<number>, Rec.NonEmpty.mapWithKey((k, v) => `${k}:${v}`));
// { a: "a:1", b: "b:2" }

reduce: <A>(reducer) => <K>(record) => A

Reduces the values of a non-empty record using the first value as the initial accumulator.

A

(accumulator, value) => A

<K>(record) => A

pipe({ a: 1, b: 2 } as Rec.NonEmpty<number>, Rec.NonEmpty.reduce((x, y) => x + y)); // 3

singleton: <K, A>(key, value) => NonEmptyRecord<A, K>

Creates a NonEmptyRecord containing a single key-value entry.

K extends string

A

K

A

NonEmptyRecord<A, K>

Rec.NonEmpty.singleton("key", 42); // { key: 42 }

values: <K, A>(record) => NonEmptyArr<A>

Returns all values of a non-empty record as a NonEmptyArr.

K extends string

A

NonEmptyRecord<A, K>

NonEmptyArr<A>

Rec.NonEmpty.values({ a: 1, b: 2 } as Rec.NonEmpty<number>); // [1, 2]

omit: <K>(…omittedKeys) => <A>(record) => Omit<A, K>

Omits specific keys from a record.

K extends string

…readonly K[]

<A>(record) => Omit<A, K>

pick for retaining only specific keys from a record.

pipe({ a: 1, b: 2, c: 3 }, Rec.omit("b")); // { a: 1, c: 3 }

pick: <K>(…pickedKeys) => <A>(record) => Pick<A, K & keyof A>

Picks specific keys from a record.

K extends string

…readonly K[]

<A>(record) => Pick<A, K & keyof A>

omit for removing specific keys from a record.

pipe({ a: 1, b: 2, c: 3 }, Rec.pick("a", "c")); // { a: 1, c: 3 }

sequence: object

Sequences a record of effectful containers into a container of record.

Maybe: <A>(record) => Maybe<Readonly<Record<string, A>>> = sequenceMaybe

A

Readonly<Record<string, Maybe<A>>>

Maybe<Readonly<Record<string, A>>>

Result: <E, A>(record) => Result<E, Readonly<Record<string, A>>> = sequenceResult

E

A

Readonly<Record<string, Result<E, A>>>

Result<E, Readonly<Record<string, A>>>

size: <A>(record) => number

Returns the number of keys in a record.

A

Readonly<Record<string, A>>

number

Rec.size({ a: 1, b: 2 }); // 2

to: object = RecTo

Dict: <A, K>(record) => ReadonlyMap<K, A>

Converts a record to a ReadonlyMap.

A

K extends string = string

Readonly<Record<K, A>>

ReadonlyMap<K, A>

Rec.to.Dict({ a: 1, b: 2 }); // ReadonlyMap { "a" => 1, "b" => 2 }

traverse: object

Maps record values to an effectful container and aggregates the results into a record.

Maybe: <A, B>(transform) => (record) => Maybe<Readonly<Record<string, B>>> = traverseMaybe

A

B

(value) => Maybe<B>

(record) => Maybe<Readonly<Record<string, B>>>

Result: <E, A, B>(transform) => (record) => Result<E, Readonly<Record<string, B>>> = traverseResult

E

A

B

(value) => Result<E, B>

(record) => Result<E, Readonly<Record<string, B>>>

updateIn: <T>(path, transform) => (record) => Readonly<Record<string, unknown>>

Immutably updates a value at a deep nested path inside a record.

T

readonly [string, string]

(value) => T

(record) => Readonly<Record<string, unknown>>

pipe(
  { user: { profile: { age: 30 } } },
  Rec.updateIn(["user", "profile", "age"], (n: number) => n + 1)
); // { user: { profile: { age: 31 } } }

values: <T>(record) => readonly T[keyof T & string][]

Returns all values of a record.

T extends Record<string, unknown>

T

readonly T[keyof T & string][]

  • keys for extracting record keys.
  • entries for extracting key-value pairs.
Rec.values({ a: 1, b: 2 }); // [1, 2]