import type { ImmutableArray } from "./array.js"; import type { EntryObject } from "./entry.js"; import type { AnyCaller } from "./function.js"; import type { DeepPartial } from "./object.js"; /** Data object. */ export type Data = { readonly [K in string]: unknown; }; /** Partial data object (values can be explicitly `undefined`). */ export type PartialData = { readonly [K in keyof T]?: T[K] | undefined; }; /** Key for a data object prop. */ export type DataKey = keyof T & string; /** Value for a data object prop. */ export type DataValue = T[keyof T & string]; /** Prop for a data object. */ export type DataProp = { readonly [K in DataKey]: readonly [K, T[K]]; }[DataKey]; /** Database is a set of named data objects. */ export type Database = { readonly [K in string]: Data; }; /** * Flattened data object with every branch node of the data, flattened into `a.c.b` format. * - Includes all branches, i.e. `{ a: { a2: number } }` will be `{ "a": object, "a.a2": number }` */ export type BranchData = EntryObject>; /** Key for a flattened data object with deep keys flattened into `a.c.b` format. */ export type BranchKey = BranchProp[0]; /** Value for a flattened data object with deep keys flattened into `a.c.b` format. */ export type BranchValue = BranchProp[1]; /** Prop for a flattened data object with deep keys flattened into `a.c.b` format. */ export type BranchProp = { readonly [K in DataKey]: (T[K] extends Data ? readonly [null, T[K]] | BranchProp : readonly [null, T[K]]) extends infer E ? E extends readonly [infer KK, infer VV] ? readonly [KK extends string ? `${K}.${KK}` : K, VV] : never : never; }[DataKey]; /** * Flattened data object with only leaf nodes of the data, flattened into `a.c.b` format. * - Only include leaf nodes, i.e. `{ a: { a2: number } }` will be `{ "a.a2": number }` */ export type LeafData = EntryObject>; /** Key for a flattened data object with deep keys flattened into `a.c.b` format. */ export type LeafKey = LeafProp[0]; /** Value for a flattened data object with deep keys flattened into `a.c.b` format. */ export type LeafValue = LeafProp[1]; /** Prop for a flattened data object with deep keys flattened into `a.c.b` format. */ export type LeafProp = { readonly [K in DataKey]: (T[K] extends Data ? LeafProp : readonly [null, T[K]]) extends infer E ? E extends readonly [infer KK, infer VV] ? readonly [KK extends string ? `${K}.${KK}` : K, VV] : never : never; }[DataKey]; /** Is an unknown value a data object? */ export declare function isData(value: unknown): value is Data; /** Assert that an unknown value is a data object. */ export declare function assertData(value: unknown, caller?: AnyCaller): asserts value is Data; /** Convert a data object or set of `DataProp` props for that object back into the full object. */ export declare function getData(input: T): T; export declare function getData(input: T | Iterable>): Partial; /** Is an unknown value the key for an own prop of a data object. */ export declare const isDataProp: (data: T, key: unknown) => key is DataKey; /** Assert that an unknown value is the key for an own prop of a data object. */ export declare function assertDataProp(data: T, key: unknown, caller?: AnyCaller): asserts key is DataKey; /** Get the props of a data object as a set of entries. */ export declare function getDataProps(data: T): ImmutableArray>; export declare function getDataProps(data: T | Partial): ImmutableArray>; /** Get the props of a data object as a set of entries. */ export declare function getDataKeys(data: T): ImmutableArray>; export declare function getDataKeys(data: T | Partial): ImmutableArray>; /** Get an optional (possibly deep) prop from a data object, or `undefined` if it doesn't exist. */ export declare function getDataProp = BranchKey>(data: T, key: K): BranchData[K]; export declare function getDataProp = BranchKey>(data: DeepPartial, key: K): BranchData[K] | undefined; export declare function getDataProp(data: Data, key: string): unknown;