Utility Functions
A collection of utility functions for common tasks like styling, routing, encryption, and string manipulation.
This document provides an overview of utility functions used throughout the application for common tasks like styling, routing, encryption, and string manipulation.
Styling Utilities
cn(...inputs)
Combines Tailwind classes using clsx
and tailwind-merge
for conflict resolution.
Example usage:
Routing & Navigation
isRouteActive()
Checks if a route is active based on the current path and specified depth.
Parameters
targetLink
- The link to check againstcurrentRoute
- The current routedepth
- How far down segments should be matched
Example usage:
pagePathname(metaUrl)
Extracts the page pathname based on file location in the app directory.
Example usage:
Encryption & Security
Encryption functions require an ENCRYPTION_KEY environment variable to be set.
encrypt(text)
Encrypts text using AES-256-CTR algorithm.
Example usage:
decrypt(encryptedText)
Decrypts previously encrypted text.
Example usage:
String Manipulation
slugify(text)
Converts text into URL-friendly slugs.
Example usage:
capitalizeFirstLetter(str)
Capitalizes the first letter of a string.
Example usage:
Validation
Regular Expressions
Common regular expressions used for validation:
Usage examples:
nameRegex
- Validates names (letters, numbers, spaces, hyphens)slugRegex
- Validates URL-friendly slugsemailRegex
- Validates email addressespasswordRegex
- Validates strong passwords
transformErrorToDetails(error)
Transforms Zod validation errors into readable strings.
Example usage:
Image & URL Utilities
placeholderImageUrl(options)
Generates placeholder image URLs with customizable colors and text.
Example usage:
absoluteUrl(path)
Converts relative paths to absolute URLs using the app's base URL.
Example usage:
Key Features
Modular Design
🧩 Small, focused utilities that do one thing well
Type Safety
🔄 Full TypeScript support with proper type definitions
Error Handling
🛡️ Robust error handling with meaningful messages
Zero Dependencies
📦 Minimal external dependencies for core utilities
Additional Utilities
formatDate(date)
Formats dates in a user-friendly format.
Example usage:
debounce(func, wait)
Debounces a function to limit how often it can be called.
Example usage:
Usage Guidelines
All utility functions are located in the lib/
directory and can
be imported directly from their respective files or through the barrel
exports.
For better code organization and bundle size optimization:
- Import specific utilities directly from their source files when possible
- Use the barrel exports for convenience when importing multiple utilities
- Keep utility functions pure and side-effect free
- Write comprehensive tests for utility functions
Example of importing: