/** * @module FormReader * Utilities for reading form data into typed objects. * Handles type conversion based on input types and data-type attributes. * * @example * // Basic form reading * const form = document.querySelector('form'); * const data = readData(form); * * // Type-safe mapping to a class instance * const user = mapFormToClass(form, new UserDTO()); */ /** * Maps form field values to a class instance's properties. * Automatically converts values based on input types (checkbox, number, date). * * Form field names must match property names on the target instance. * * @template T - The type of the class instance * @param form - The HTML form element to read from * @param instance - The class instance to populate * @param options - Configuration options * @param options.throwOnMissingProperty - Throw if form field has no matching property * @param options.throwOnMissingField - Throw if class property has no matching form field * @returns The populated instance * * @example * class UserDTO { * name: string = ''; * email: string = ''; * age: number = 0; * newsletter: boolean = false; * } * * const form = document.querySelector('form'); * const user = mapFormToClass(form, new UserDTO()); * console.log(user.name, user.age, user.newsletter); * * @example * // With validation * const user = mapFormToClass(form, new UserDTO(), { * throwOnMissingProperty: true, // Catch typos in form field names * throwOnMissingField: true // Ensure all DTO fields are in form * }); */ export declare function mapFormToClass(form: HTMLFormElement, instance: T, options?: { throwOnMissingProperty?: boolean; throwOnMissingField?: boolean; }): T; /** * Configuration options for form reading operations. */ export interface FormReaderOptions { /** Prefix to strip from field names when mapping to properties */ prefix?: string; /** If true, checkboxes return their value instead of true/false */ disableBinaryCheckbox?: boolean; /** If true, radio buttons return their value instead of true/false */ disableBinaryRadioButton?: boolean; } /** * Gets the appropriate type converter function for a form element. * Uses the `data-type` attribute if present, otherwise infers from input type. * * @param element - The form element to get a converter for * @returns A function that converts string values to the appropriate type * * @example * // With data-type attribute * * const converter = getDataConverter(input); * converter('42'); // Returns: 42 (number) * * @example * // Inferred from input type * * const converter = getDataConverter(checkbox); * converter('true'); // Returns: true (boolean) */ export declare function getDataConverter(element: HTMLElement): ConverterFunc; /** * Reads all form data into a plain object with automatic type conversion. * Handles multiple values (e.g., multi-select) and custom form-associated elements. * * Type conversion is based on: * 1. `data-type` attribute if present (number, boolean, string, Date) * 2. Input type (checkbox, number, date, etc.) * 3. Falls back to string * * @param form - The HTML form element to read * @returns Object with property names matching field names * * @example * // HTML form *
* * * * *
* * // Reading the form * const data = readData(form); * // Returns: { username: 'john', age: 25, active: true, colors: ['red', 'blue'] } * * @example * // With custom form elements *
* * * * const data = readData(form); 1*/ export declare function readData>(form: HTMLFormElement): T; /** * Function type for converting string form values to typed values. */ export type ConverterFunc = (value: string) => unknown; /** * Supported data-type attribute values for explicit type conversion. */ export type DataType = 'number' | 'boolean' | 'string' | 'Date'; /** * Supported HTML input types for automatic type inference. */ export type InputType = 'tel' | 'text' | 'checkbox' | 'radio' | 'number' | 'color' | 'date' | 'datetime-local' | 'month' | 'week' | 'time'; /** * Converts string values to booleans. * Handles 'true'/'false' strings and numeric values (>0 is true). * * @param value - String value to convert * @returns Boolean value or undefined if empty * @throws Error if value cannot be interpreted as boolean */ export declare function BooleanConverter(value?: string): boolean | undefined; /** * Converts string values to numbers. * * @param value - String value to convert * @returns Number value or undefined if empty * @throws Error if value is not a valid number */ export declare function NumberConverter(value?: string): number | undefined; /** * Converts string values to Date objects. * Supports both ISO format (`2024-01-15`) and locale-specific formats * (`01/15/2024` for en-US, `15.01.2024` for de, etc.) based on the * current i18n locale. * * @param value - Date string in ISO or locale format * @returns Date object * @throws Error if value is not a valid date * * @example * // ISO format (from ) * DateConverter('2024-01-15') // Date(2024, 0, 15) * * // Locale format (from ) * // with locale set to 'sv': 2024-01-15 * // with locale set to 'en-US': 01/15/2024 * // with locale set to 'de': 15.01.2024 */ export declare function DateConverter(value: string): Date | undefined; /** * Creates a converter function based on the data-type attribute value. * * @param dataType - The data-type attribute value * @returns Appropriate converter function for the type */ export declare function createConverterFromDataType(dataType: DataType): ConverterFunc; /** * Creates a converter function based on HTML input type. * Handles special types like checkbox, date, time, week, and month. * * @param inputType - The HTML input type attribute value * @returns Appropriate converter function for the type */ export declare function createConverterFromInputType(inputType: InputType): ConverterFunc;