Utility Functions
Relevant source files
The following files were used as context for generating this wiki page:
Utility functions in the CoolTools project provide essential helper logic for data manipulation, performance optimization, and API interaction. These functions are centralized to ensure consistency across the application, particularly in handling phone specifications, formatting display data, and managing asynchronous requests to the mobile specs API.
The utility layer acts as a bridge between raw data retrieved from external sources and the UI components, handling tasks such as price formatting, date calculations, and debouncing user input to prevent excessive API calls.
Core Helper Functions
The primary utility module contains generic helpers used for string manipulation, time formatting, and performance control.
String and Data Formatting
These functions transform raw data strings into human-readable formats or extract numeric values for calculations.
formatPrice: Takes an optional string and extracts the first numeric match to return a formatted USD currency string (e.g., "$999").extractNumericValue: A robust parser that finds the first sequence of digits in a string and returns it as a number, defaulting to 0 if no match is found.formatRelativeTime: Calculates the difference between a provided date string and the current time, returning descriptive labels like "Today", "Yesterday", or "X months ago".
Sources: cooltools/lib/utils.ts:16-42
Performance Utilities
To maintain application responsiveness, the project implements a standard debounce function.
export function debounce<T extends (...args: any[]) => any>(
func: T,
wait: number
): (...args: Parameters<T>) => void {
let timeout: NodeJS.Timeout;
return function executedFunction(...args: Parameters<T>) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
Sources: cooltools/lib/utils.ts:2-14
This is used extensively in the UI, such as in the ComparePage to delay search requests until the user stops typing for 300ms.
Sources: cooltools/app/compare/page.tsx:69-80
API Utility Layer
The API utility layer manages interactions with the external api-mobilespecs.azharimm.dev service. It includes built-in caching and request deduplication to minimize network overhead.
Request Flow and Caching
The system uses an in-memory Map to store responses with a Time-To-Live (TTL) of 5 minutes. Additionally, it tracks pendingRequests to prevent duplicate simultaneous calls for the same resource.
Sources: cooltools/lib/phoneApi.ts:6-23
Specialized Data Retrieval
The utility layer exports several functions for specific data needs:
| Function | Purpose |
|---|---|
getPhoneDetails | Fetches full specifications with a retry logic (default 2 retries). |
searchPhones | Queries the API for phone models matching a string. |
getLatestPhones | Retrieves a list of the most recently released devices. |
getTopPhones | Fetches devices sorted by user interest/popularity. |
Sources: cooltools/lib/phoneApi.ts:108-164
UI and Animation Utilities
The HomePage implements internal utility logic for visual engagement, specifically an animateStats function that uses a setInterval to increment counter values for marketing statistics.
Sources: cooltools/app/page.tsx:34-58
Data Schema Utilities
The project defines its data structure using Drizzle ORM in shared/schema.ts. This serves as a utility for the backend/database layer to ensure type safety and consistent field names across the phones, comparisons, and benchmarks tables.
- Phones Table: Defines fields for display size, ppi, antutu scores, and battery capacity.
- JSONB Storage: The
fullSpecsfield utilizesjsonbto store the complete unmapped API response for future-proofing.
Sources: cooltools/shared/schema.ts:4-65
Utility functions within CoolTools provide the necessary infrastructure for efficient data handling, ranging from API request management to UI-level formatting. By centralizing these helpers in lib/utils.ts and lib/phoneApi.ts, the application ensures a performant and consistent experience across search, comparison, and detail views.