UNPKG

mobx-keystone-mindreframer

Version:

A MobX powered state management solution based on data trees with first class support for Typescript, snapshots, patches and much more

130 lines (129 loc) 4.11 kB
import type { Path, PathElement } from "./pathTypes"; /** * Path from an object to its immediate parent. * * @typeparam T Parent object type. */ export interface ParentPath<T extends object> { /** * Parent object. */ readonly parent: T; /** * Property name (if the parent is an object) or index number (if the parent is an array). */ readonly path: PathElement; } /** * Path from an object to its root. * * @typeparam T Root object type. */ export interface RootPath<T extends object> { /** * Root object. */ readonly root: T; /** * Path from the root to the given target, as a string array. * If the target is a root itself then the array will be empty. */ readonly path: Path; /** * Objects in the path, from root (included) until target (included). * If the target is a root then only the target will be included. */ readonly pathObjects: ReadonlyArray<unknown>; } /** * Returns the parent of the target plus the path from the parent to the target, or undefined if it has no parent. * * @typeparam T Parent object type. * @param value Target object. * @returns */ export declare function getParentPath<T extends object = any>(value: object): ParentPath<T> | undefined; /** * Returns the parent object of the target object, or undefined if there's no parent. * * @typeparam T Parent object type. * @param value Target object. * @returns */ export declare function getParent<T extends object = any>(value: object): T | undefined; /** * Returns if a given object is a model interim data object (`$`). * * @param value Object to check. * @returns true if it is, false otherwise. */ export declare function isModelDataObject(value: object): boolean; /** * Returns the root of the target plus the path from the root to get to the target. * * @typeparam T Root object type. * @param value Target object. * @returns */ export declare function getRootPath<T extends object = any>(value: object): RootPath<T>; /** * Returns the root of the target object, or itself if the target is a root. * * @typeparam T Root object type. * @param value Target object. * @returns */ export declare function getRoot<T extends object = any>(value: object): T; /** * Returns if a given object is a root object. * * @param value Target object. * @returns */ export declare function isRoot(value: object): boolean; /** * Tries to resolve a path from an object. * * @typeparam T Returned value type. * @param pathRootObject Object that serves as path root. * @param path Path as an string or number array. * @returns An object with `{ resolved: true, value: T }` or `{ resolved: false }`. */ export declare function resolvePath<T = any>(pathRootObject: object, path: Path): { resolved: true; value: T; } | { resolved: false; value?: undefined; }; /** * @ignore */ export declare const skipIdChecking: unique symbol; /** * @ignore * * Tries to resolve a path from an object while checking ids. * * @typeparam T Returned value type. * @param pathRootObject Object that serves as path root. * @param path Path as an string or number array. * @param pathIds An array of ids of the models that must be checked, null if not a model or `skipIdChecking` to skip it. * @returns An object with `{ resolved: true, value: T }` or `{ resolved: false }`. */ export declare function resolvePathCheckingIds<T = any>(pathRootObject: object, path: Path, pathIds: ReadonlyArray<string | null | typeof skipIdChecking>): { resolved: true; value: T; } | { resolved: false; value?: undefined; }; /** * Gets the path to get from a parent to a given child. * Returns an empty array if the child is actually the given parent or undefined if the child is not a child of the parent. * * @param fromParent * @param toChild * @returns */ export declare function getParentToChildPath(fromParent: object, toChild: object): Path | undefined;