UNPKG

mobx-keystone

Version:

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

108 lines (107 loc) 3.22 kB
import { Path, PathElement } from './pathTypes'; /** * Path from an object to its immediate parent. * * @template 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. * * @template 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. * * @template 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. * * @template 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. * * @template 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. * * @template 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. * * @template 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; }; /** * 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;