@conform-to/dom
Version:
A set of opinionated helpers built on top of the Constraint Validation API
67 lines (66 loc) • 2.73 kB
TypeScript
/**
* Construct a form data with the submitter value.
* It utilizes the submitter argument on the FormData constructor from modern browsers
* with fallback to append the submitter value in case it is not unsupported.
*
* @see https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData#parameters
*/
export declare function getFormData(form: HTMLFormElement, submitter?: HTMLInputElement | HTMLButtonElement | null): FormData;
/**
* Returns the paths from a name based on the JS syntax convention
* @example
* ```js
* const paths = getPaths('todos[0].content'); // ['todos', 0, 'content']
* ```
*/
export declare function getPaths(name: string | undefined): Array<string | number>;
/**
* Returns a formatted name from the paths based on the JS syntax convention
* @example
* ```js
* const name = formatPaths(['todos', 0, 'content']); // "todos[0].content"
* ```
*/
export declare function formatPaths(paths: Array<string | number>): string;
/**
* Format based on a prefix and a path
*/
export declare function formatName(prefix: string | undefined, path?: string | number): string;
/**
* Check if a name match the prefix paths
*/
export declare function isPrefix(name: string, prefix: string): boolean;
/**
* Compare the parent and child paths to get the relative paths
* Returns null if the child paths do not start with the parent paths
*/
export declare function getChildPaths(parentNameOrPaths: string | Array<string | number>, childName: string): (string | number)[] | null;
/**
* Assign a value to a target object by following the paths
*/
export declare function setValue(target: Record<string, any>, name: string, valueFn: (currentValue?: unknown) => unknown): void;
/**
* Retrive the value from a target object by following the paths
*/
export declare function getValue(target: unknown, name: string): unknown;
/**
* Check if the value is a plain object
*/
export declare function isPlainObject(obj: unknown): obj is Record<string | number | symbol, unknown>;
/**
* Check if the value is a File
*/
export declare function isFile(obj: unknown): obj is File;
/**
* Normalize value by removing empty object or array, empty string and null values
*/
export declare function normalize<Type extends Record<string, unknown>>(value: Type, acceptFile?: boolean): Type | undefined;
export declare function normalize<Type extends Array<unknown>>(value: Type, acceptFile?: boolean): Type | undefined;
export declare function normalize(value: unknown, acceptFile?: boolean): unknown | undefined;
/**
* Flatten a tree into a dictionary
*/
export declare function flatten(data: unknown, options?: {
resolve?: (data: unknown) => unknown;
prefix?: string;
}): Record<string, unknown>;