Skip to main content

Utility Functions & Helpers

The SDK includes several utility functions to simplify common operations like phone number formatting, retry logic, text processing, and more.

Phone Number Utilities

formatPhoneNumber()

Formats phone numbers to international format with + prefix.
Parameters:
  • phoneNumber - Phone number string to format
Returns:
  • Formatted phone number with + prefix
Examples:

validatePhoneNumber()

Validates international phone number format.

Retry Logic & Error Handling

withRetry()

Executes a function with automatic retry logic and exponential backoff.
Parameters:
  • fn - Async function to execute
  • options - Retry configuration options
Options:
Examples:

delay()

Promise-based delay function for creating pauses.
Parameters:
  • ms - Milliseconds to delay
Examples:

Text Processing

truncateText()

Truncates text to specified length with ellipsis.
Parameters:
  • text - Text to truncate
  • maxLength - Maximum character length
Examples:

sanitizeText()

Removes control characters and normalizes whitespace.
Parameters:
  • text - Text to sanitize
Examples:

URL Utilities

isValidUrl()

Validates URL format and accessibility.
Parameters:
  • url - URL string to validate
Examples:

Array Utilities

chunk()

Splits arrays into smaller chunks for batch processing.
Parameters:
  • array - Array to split
  • size - Chunk size
Examples:

JSON Utilities

safeJsonParse()

Safely parses JSON with fallback values.
Parameters:
  • json - JSON string to parse
  • fallback - Fallback value if parsing fails
Examples:

Function Utilities

debounce()

Debounces function execution to prevent rapid repeated calls.
Parameters:
  • func - Function to debounce
  • wait - Delay in milliseconds
Examples:

Usage Examples

Comprehensive Message Processing

Input Validation and Sanitization

Rate Limiting with Utilities

This utility system provides essential building blocks for robust WhatsApp message processing, input validation, and error handling.