Overview
The currency module provides utilities for formatting monetary values and fetching Venezuelan exchange rates from the BCV (Banco Central de Venezuela). Module:lib/currency.ts
formatCurrency
Formats a numeric value as currency using the Intl.NumberFormat API with caching for performance.Function Signature
Parameters
The numeric value to format
The ISO 4217 currency code (e.g., “USD”, “VES”)
The locale string for formatting. Defaults to Venezuelan Spanish
Returns
The formatted currency string, or ”—” if the value is not finite
Examples
The function uses an internal cache (
formatterCache) to avoid recreating Intl.NumberFormat instances, improving performance when formatting multiple values with the same currency/locale combination.formatUsd
Convenience function for formatting USD currency values.Function Signature
Parameters
The numeric value to format as USD
Returns
The formatted USD string
Example
formatVes
Convenience function for formatting VES (Venezuelan Bolivar) currency values.Function Signature
Parameters
The numeric value to format as VES
Returns
The formatted VES string
Example
fetchBcvRate
Fetches the current official USD to VES exchange rate from the BCV via the DolarAPI.Function Signature
Parameters
Optional AbortSignal for cancelling the request
Returns
Example
useBcvRate
React hook for managing BCV exchange rate data with automatic loading, error handling, and optional refresh intervals.Hook Signature
Parameters
Configuration options for the hook
Optional interval in milliseconds for automatically refreshing the rate
Returns
An object containing the rate state and control functions
The current exchange rate, or
null if not yet loaded or on errortrue while the rate is being fetchedError message if the fetch failed, otherwise
nullFunction to manually trigger a rate refresh
Examples
The hook automatically cleans up on unmount and handles abort scenarios properly. It also respects component mounting state to avoid setting state on unmounted components.
Types
ExchangeRate
UseBcvRateOptions
useBcvRate hook.
UseBcvRateResult
useBcvRate hook.
Performance Considerations
-
Formatter Caching: The
formatCurrencyfunction cachesIntl.NumberFormatinstances by locale and currency combination, avoiding repeated instantiation overhead. -
Request Deduplication: When using
useBcvRate, the hook manages a single request at a time and prevents race conditions with mount state tracking. -
No-Store Cache: The
fetchBcvRatefunction usescache: "no-store"to ensure fresh exchange rate data on every request.
Error Handling
All currency functions handle edge cases gracefully:formatCurrencyreturns ”—” for non-finite values (NaN, Infinity)fetchBcvRatereturnsnullon network errors or invalid responsesuseBcvRateexposes error state with user-friendly messages in Spanish
