/** * This module provides utility functions for working with records in TypeScript. * * @since 1.0.0 */ import type { Either } from "@fp-ts/core/Either"; import type { Kind, TypeLambda } from "@fp-ts/core/HKT"; import type { Option } from "@fp-ts/core/Option"; import type * as applicative from "@fp-ts/core/typeclass/Applicative"; import * as covariant from "@fp-ts/core/typeclass/Covariant"; import type * as filterable from "@fp-ts/core/typeclass/Filterable"; import * as invariant from "@fp-ts/core/typeclass/Invariant"; import * as traversable from "@fp-ts/core/typeclass/Traversable"; import * as traversableFilterable from "@fp-ts/core/typeclass/TraversableFilterable"; /** * @category models * @since 1.0.0 */ export interface ReadonlyRecord { readonly [x: string]: A; } /** * @category type lambdas * @since 1.0.0 */ export interface ReadonlyRecordTypeLambda extends TypeLambda { readonly type: ReadonlyRecord; } /** * Creates a new, empty record. * * @category constructors * @since 1.0.0 */ export declare const empty: () => Record; /** * Takes an iterable and a projection function and returns a record. * The projection function maps each value of the iterable to a tuple of a key and a value, which is then added to the resulting record. * * @param self - An iterable of values to be mapped to a record. * @param f - A projection function that maps values of the iterable to a tuple of a key and a value. * * @example * import { fromIterable } from '@fp-ts/core/ReadonlyRecord' * * const input = [1, 2, 3, 4] * * assert.deepStrictEqual( * fromIterable(input, a => [String(a), a * 2]), * { '1': 2, '2': 4, '3': 6, '4': 8 } * ) * * @category constructors * @since 1.0.0 */ export declare const fromIterable: { (f: (a: A) => readonly [string, B]): (self: Iterable) => Record; (self: Iterable, f: (a: A) => readonly [string, B]): Record; }; /** * Determine if a `ReadonlyRecord` is empty. * * @param self - `ReadonlyRecord` to test for emptiness. * * @example * import { isEmpty } from "@fp-ts/core/ReadonlyRecord" * * assert.deepStrictEqual(isEmpty({}), true); * assert.deepStrictEqual(isEmpty({ a: 3 }), false); * * @category guards * @since 1.0.0 */ export declare const isEmpty: (self: ReadonlyRecord) => self is Record; /** * Transforms the values of a `ReadonlyRecord` into an `Array` with a custom mapping function. * * @param self - The `ReadonlyRecord` to transform. * @param f - The custom mapping function to apply to each key/value of the `ReadonlyRecord`. * * @example * import { collect } from '@fp-ts/core/ReadonlyRecord' * * const x = { a: 1, b: 2, c: 3 } * assert.deepStrictEqual(collect(x, (key, n) => [key, n]), [["a", 1], ["b", 2], ["c", 3]]) * * @category conversions * @since 1.0.0 */ export declare const collect: { (f: (key: string, a: A) => B): (self: ReadonlyRecord) => Array; (self: ReadonlyRecord, f: (key: string, a: A) => B): Array; }; /** * Converts a `ReadonlyRecord` to an `Array` of key-value pairs. * * @param self - A `ReadonlyRecord` to convert to an `Array`. * * @example * import { toArray } from '@fp-ts/core/ReadonlyRecord' * * const x = { a: 1, b: 2 } * assert.deepStrictEqual(toArray(x), [["a", 1], ["b", 2]]) * * @category conversions * @since 1.0.0 */ export declare const toArray: (self: ReadonlyRecord) => Array<[string, A]>; /** * Returns the number of key/value pairs in a `ReadonlyRecord`. * * @param self - A `ReadonlyRecord` to calculate the number of key/value pairs in. * * @example * import { size } from "@fp-ts/core/ReadonlyRecord"; * * assert.deepStrictEqual(size({ a: "a", b: 1, c: true }), 3); * * @since 1.0.0 */ export declare const size: (self: ReadonlyRecord) => number; /** * Check if a given `key` exists in a `ReadonlyRecord`. * * @param self - the `ReadonlyRecord` to look in. * @param key - the key to look for in the `ReadonlyRecord`. * * @example * import { has } from '@fp-ts/core/ReadonlyRecord' * * assert.deepStrictEqual(has({ a: 1, b: 2 }, "a"), true); * assert.deepStrictEqual(has({ a: 1, b: 2 }, "c"), false); * * @since 1.0.0 */ export declare const has: { (key: string): (self: ReadonlyRecord) => boolean; (self: ReadonlyRecord, key: string): boolean; }; /** * Retrieve a value at a particular key from a `ReadonlyRecord`, returning it wrapped in an `Option`. * * @param self - The `ReadonlyRecord` to retrieve value from. * @param key - Key to retrieve from `ReadonlyRecord`. * * @example * import { get } from "@fp-ts/core/ReadonlyRecord" * import { some, none } from "@fp-ts/core/Option" * * const person = { name: "John Doe", age: 35 } * * assert.deepStrictEqual(get(person, "name"), some("John Doe")) * assert.deepStrictEqual(get(person, "email"), none()) * * @since 1.0.0 */ export declare const get: { (key: string): (self: ReadonlyRecord) => Option; (self: ReadonlyRecord, key: string): Option; }; /** * Apply a function to the element at the specified key, creating a new record, * or return `None` if the key doesn't exist. * * @param self - The `ReadonlyRecord` to be updated. * @param key - The key of the element to modify. * @param f - The function to apply to the element. * * @example * import { modifyOption } from "@fp-ts/core/ReadonlyRecord" * import { some, none } from "@fp-ts/core/Option" * * const f = (x: number) => x * 2 * * assert.deepStrictEqual( * modifyOption({ a: 3 }, 'a', f), * some({ a: 6 }) * ) * assert.deepStrictEqual( * modifyOption({ a: 3 }, 'b', f), * none() * ) * * @since 1.0.0 */ export declare const modifyOption: { (key: string, f: (a: A) => B): (self: ReadonlyRecord) => Option>; (self: ReadonlyRecord, key: string, f: (a: A) => B): Option>; }; /** * Replaces a value in the record with the new value passed as parameter. * * @param self - The `ReadonlyRecord` to be updated. * @param key - The key to search for in the record. * @param b - The new value to replace the existing value with. * * @example * import { replaceOption } from "@fp-ts/core/ReadonlyRecord" * import { some, none } from "@fp-ts/core/Option" * * assert.deepStrictEqual( * replaceOption({ a: 1, b: 2, c: 3 }, 'a', 10), * some({ a: 10, b: 2, c: 3 }) * ) * assert.deepStrictEqual(replaceOption({}, 'a', 10), none()) * * @since 1.0.0 */ export declare const replaceOption: { (key: string, b: B): (self: ReadonlyRecord) => Option>; (self: ReadonlyRecord, key: string, b: B): Option>; }; /** * Removes a key from a `ReadonlyRecord` and returns a new `Record` * * @param self - the `ReadonlyRecord` to remove the key from. * @param key - the key to remove from the `ReadonlyRecord`. * * @example * import { remove } from '@fp-ts/core/ReadonlyRecord' * * assert.deepStrictEqual(remove({ a: 1, b: 2 }, "a"), { b: 2 }) * * @since 1.0.0 */ export declare const remove: { (key: string): (self: ReadonlyRecord) => Record; (self: ReadonlyRecord, key: string): Record; }; /** * Retrieves the value of the property with the given `key` from a `ReadonlyRecord` and returns an `Option` * of a tuple with the value and the `ReadonlyRecord` with the removed property. * If the key is not present, returns `O.none`. * * @param self - The input `ReadonlyRecord`. * @param key - The key of the property to retrieve. * * @example * import { pop } from '@fp-ts/core/ReadonlyRecord' * import { some, none } from '@fp-ts/core/Option' * * assert.deepStrictEqual(pop({ a: 1, b: 2 }, "a"), some([1, { b: 2 }])) * assert.deepStrictEqual(pop({ a: 1, b: 2 }, "c"), none()) * * @category record * @since 1.0.0 */ export declare const pop: { (key: string): (self: ReadonlyRecord) => Option]>; (self: ReadonlyRecord, key: string): Option]>; }; /** * Maps a `ReadonlyRecord` into another `Record` by applying a transformation function to each of its values. * * @param self - The `ReadonlyRecord` to be mapped. * @param f - A transformation function that will be applied to each of the values in the `ReadonlyRecord`. * * @example * import { map } from "@fp-ts/core/ReadonlyRecord" * * const f = (n: number) => `-${n}` * * assert.deepStrictEqual(map({ a: 3, b: 5 }, f), { a: "-3", b: "-5" }) * * const g = (n: number, key: string) => `${key.toUpperCase()}-${n}` * * assert.deepStrictEqual(map({ a: 3, b: 5 }, g), { a: "A-3", b: "B-5" }) * * @since 1.0.0 */ export declare const map: { (f: (a: A, key: string) => B): (self: ReadonlyRecord) => Record; (self: ReadonlyRecord, f: (a: A, key: string) => B): Record; }; /** * Transforms a `ReadonlyRecord` into a `Record` by applying the function `f` to each key and value in the original `ReadonlyRecord`. * If the function returns `Some`, the key-value pair is included in the output `Record`. * * @param self - The input `ReadonlyRecord`. * @param f - The transformation function. * * @example * import { filterMap } from '@fp-ts/core/ReadonlyRecord' * import { some, none } from '@fp-ts/core/Option' * * const x = { a: 1, b: 2, c: 3 } * const f = (a: number, key: string) => a > 2 ? some(a * 2) : none() * assert.deepStrictEqual(filterMap(x, f), { c: 6 }) * * @since 1.0.0 */ export declare const filterMap: { (f: (a: A, key: string) => Option): (self: ReadonlyRecord) => Record; (self: ReadonlyRecord, f: (a: A, key: string) => Option): Record; }; /** * Selects properties from a record whose values match the given predicate. * * @param self - The `ReadonlyRecord` to filter. * @param predicate - A function that returns a `boolean` value to determine if the entry should be included in the new record. * * @example * import { filter } from '@fp-ts/core/ReadonlyRecord' * * const x = { a: 1, b: 2, c: 3, d: 4 } * assert.deepStrictEqual(filter(x, (n) => n > 2), { c: 3, d: 4 }) * * @category filtering * @since 1.0.0 */ export declare const filter: { (refinement: (a: A, key: string) => a is B): (self: ReadonlyRecord) => Record; (predicate: (a: A, key: string) => boolean): (self: ReadonlyRecord) => Record; (self: ReadonlyRecord, refinement: (a: A, key: string) => a is B): Record; (self: ReadonlyRecord, predicate: (a: A, key: string) => boolean): Record; }; /** * Given a `ReadonlyRecord` with `Option` values, returns a `Record` with only the `Some` values, with the same keys. * * @param self - A `ReadonlyRecord` with `Option` values. * * @example * import { compact } from '@fp-ts/core/ReadonlyRecord' * import { some, none } from '@fp-ts/core/Option' * * assert.deepStrictEqual( * compact({ a: some(1), b: none(), c: some(2) }), * { a: 1, c: 2 } * ) * * @category filtering * @since 1.0.0 */ export declare const compact: (self: ReadonlyRecord>) => Record; /** * Partitions the elements of a `ReadonlyRecord` into two groups: those that match a predicate, and those that don't. * * @param self - The `ReadonlyRecord` to partition. * @param f - The predicate function to apply to each element. * * @example * import { partitionMap } from '@fp-ts/core/ReadonlyRecord' * import { left, right } from '@fp-ts/core/Either' * * const x = { a: 1, b: 2, c: 3 } * const f = (n: number) => (n % 2 === 0 ? right(n) : left(n)) * assert.deepStrictEqual(partitionMap(x, f), [{ a: 1, c: 3 }, { b: 2}]) * * @category filtering * @since 1.0.0 */ export declare const partitionMap: { (f: (a: A, key: string) => Either): (self: ReadonlyRecord) => [Record, Record]; (self: ReadonlyRecord, f: (a: A, key: string) => Either): [Record, Record]; }; /** * Partitions a `ReadonlyRecord` of `Either` values into two separate records, * one with the `Left` values and one with the `Right` values. * * @param self - the `ReadonlyRecord` to partition. * * @example * import { separate } from '@fp-ts/core/ReadonlyRecord' * import { left, right } from '@fp-ts/core/Either' * * assert.deepStrictEqual( * separate({ a: left("e"), b: right(1) }), * [{ a: "e" }, { b: 1 }] * ) * * @category filtering * @since 1.0.0 */ export declare const separate: (self: ReadonlyRecord>) => [Record, Record]; /** * Partitions a `ReadonlyRecord` into two separate `Record`s based on the result of a predicate function. * * @param self - The input `ReadonlyRecord` to partition. * @param predicate - The partitioning function to determine the partitioning of each value of the `ReadonlyRecord`. * * @example * import { partition } from '@fp-ts/core/ReadonlyRecord' * * assert.deepStrictEqual( * partition({ a: 1, b: 3 }, (n) => n > 2), * [{ a: 1 }, { b: 3 }] * ) * * @category filtering * @since 1.0.0 */ export declare const partition: { (refinement: (a: A, key: string) => a is B): (self: ReadonlyRecord) => [Record, Record]; (predicate: (a: A, key: string) => boolean): (self: ReadonlyRecord) => [Record, Record]; (self: ReadonlyRecord, refinement: (a: A, key: string) => a is B): [Record, Record]; (self: ReadonlyRecord, predicate: (a: A, key: string) => boolean): [Record, Record]; }; /** * Maps each entry of a `ReadonlyRecord` to an effect and collects the results into a new record. * * @param F - an {@link applicative.Applicative Applicative} instance. * @param self - a `ReadonlyRecord` to map over. * @param f - the mapping function, which maps an entry `a` and its corresponding `key` to an effect. * * @example * import { traverse } from '@fp-ts/core/ReadonlyRecord' * import { some, none, Applicative } from '@fp-ts/core/Option' * * assert.deepStrictEqual( * traverse(Applicative)({ a: 1, b: 2 }, (n: number) => (n <= 2 ? some(n) : none())), * some({ a: 1, b: 2 }) * ) * assert.deepStrictEqual( * traverse(Applicative)({ a: 1, b: 2 }, (n: number) => (n >= 2 ? some(n) : none())), * none() * ) * * @category traversing * @since 1.0.0 */ export declare const traverse: (F: applicative.Applicative) => { (f: (a: A, key: string) => Kind): (self: ReadonlyRecord) => Kind>; (self: ReadonlyRecord, f: (a: A_1, key: string) => Kind): Kind>; }; /** * Transforms a `ReadonlyRecord` of `Kind` values into a `Kind` of `Record` values. * * @param F - an {@link applicative.Applicative Applicative} instance. * @param self - the `ReadonlyRecord` of `Kind` values. * * @example * import * as RR from '@fp-ts/core/ReadonlyRecord' * import { some, none, Applicative } from '@fp-ts/core/Option' * * const sequence = RR.sequence(Applicative) * * assert.deepStrictEqual(sequence({ a: some(1), b: some(2) }), some({ a: 1, b: 2 })) * assert.deepStrictEqual(sequence({ a: none(), b: some(2) }), none()) * * @category traversing * @since 1.0.0 */ export declare const sequence: (F: applicative.Applicative) => (self: ReadonlyRecord>) => Kind>; /** * @category instances * @since 1.0.0 */ export declare const Covariant: covariant.Covariant; /** * @category instances * @since 1.0.0 */ export declare const Invariant: invariant.Invariant; /** * @category mapping * @since 1.0.0 */ export declare const tupled: (self: ReadonlyRecord) => Record; /** * @category mapping * @since 1.0.0 */ export declare const flap: { (self: ReadonlyRecord<(a: A) => B>): (a: A) => Record; (a: A, self: ReadonlyRecord<(a: A) => B>): Record; }; /** * Maps the success value of this effect to the specified constant value. * * @category mapping * @since 1.0.0 */ export declare const as: { (b: B): <_>(self: ReadonlyRecord<_>) => Record; <_, B>(self: ReadonlyRecord<_>, b: B): Record; }; /** * @category instances * @since 1.0.0 */ export declare const Filterable: filterable.Filterable; /** * @category instances * @since 1.0.0 */ export declare const Traversable: traversable.Traversable; /** * @category traversing * @since 1.0.0 */ export declare const traverseTap: (F: applicative.Applicative) => { (f: (a: A) => Kind): (self: ReadonlyRecord) => Kind>; (self: ReadonlyRecord, f: (a: A) => Kind): Kind>; }; /** * @category filtering * @since 1.0.0 */ export declare const traversePartitionMap: (F: applicative.Applicative) => { (f: (a: A) => Kind>): (self: ReadonlyRecord) => Kind, Record]>; (self: ReadonlyRecord, f: (a: A_1) => Kind>): Kind, Record]>; }; /** * @category filtering * @since 1.0.0 */ export declare const traverseFilterMap: (F: applicative.Applicative) => { (f: (a: A) => Kind>): (self: ReadonlyRecord) => Kind>; (self: ReadonlyRecord, f: (a: A_1) => Kind>): Kind>; }; /** * @category instances * @since 1.0.0 */ export declare const TraversableFilterable: traversableFilterable.TraversableFilterable; /** * Filter values inside a context. * * @since 1.0.0 */ export declare const traverseFilter: (F: applicative.Applicative) => { (predicate: (a: A) => Kind): (self: ReadonlyRecord) => Kind>; (self: ReadonlyRecord, predicate: (a: A) => Kind): Kind>; }; /** * @since 1.0.0 */ export declare const traversePartition: (F: applicative.Applicative) => { (predicate: (a: A) => Kind): (self: ReadonlyRecord) => Kind, Record]>; (self: ReadonlyRecord, predicate: (a: A) => Kind): Kind, Record]>; }; //# sourceMappingURL=ReadonlyRecord.d.ts.map