UNPKG

@estarlincito/utils

Version:

A collection of utility functions designed to simplify and speed up development tasks in JavaScript and TypeScript projects.

103 lines (74 loc) • 2.67 kB
# formZed Utility Function The `formZed` function provides predefined field configurations for commonly used form inputs, such as name, email, phone number, and password. It helps maintain consistency in form validation and input attributes. ## šŸ“Œ Features • Returns input field configurations, including `placeholder`, `type`, `maxLength`, and `minLength`. • Standardizes form input attributes for common fields. • Supports various input types, such as `text`, `email`, `password`, and `tel`. • Automatically generates a title by formatting the field name. • Ensures validation constraints are met. ## šŸš€ Installation To use this utility in your project, you can install it via `pnpm`, `npm`, or `yarn` if packaged as a module. 1. Install the utility: ```bash pnpm add @estarlincito/utils # or npm install @estarlincito/utils # or yarn add @estarlincito/utils ``` 2. Import the function in your project: ```ts import { formZed } from '@estarlincito/utils'; ``` ## ⚔ Usage The `formZed` function simplifies the process of configuring form fields with predefined values. ### Example 1: Get Configuration for an Email Input ```ts const emailField = formZed('email'); console.log(emailField); /* { placeholder: 'name@domain.com', type: 'email', maxLength: 30, minLength: 5, title: 'Email' } */ ``` ### Example 2: Get Configuration for a Password Input ```ts const passwordField = formZed('password'); console.log(passwordField); /* { placeholder: '4ppl3-1Ph0ne', type: 'password', maxLength: 30, minLength: 8, title: 'Password' } */ ``` ## āŒ Incorrect Usage ```ts const field = formZed('address'); // āŒ Returns undefined as 'address' is not defined in formZed ``` ## āœ… Correct Usage ```ts const field = formZed('username'); // āœ… Returns configuration for 'username' ``` ## šŸ›  How It Works • Accepts a field name and looks up its predefined configuration. • If the field is found, it returns an object containing its attributes. • The field title is automatically generated by capitalizing the first letter and replacing hyphens with spaces. • If the field is not found, it logs an error using `handleError` and returns `undefined`. ## āš ļø Error Handling If an invalid field name is provided, `formZed` calls `handleError` and logs an error message: ```ts handleError(`${name} was not found in (fiels) /lib/zed.ts`); ``` This ensures better debugging and prevents unexpected behavior. ## šŸ“ License This project is licensed under the MIT License - see the [LICENSE](../LICENSE) file for details. **Author:** Estarlin R ([estarlincito.com](https://estarlincito.com))