46 lines
1.3 KiB
TypeScript
Raw Normal View History

/**
* Freebit Provider Utilities
*
* Provider-specific utilities for Freebit SIM API integration
*/
/**
* Normalize account identifier (remove formatting)
* Removes all non-digit characters from account string
*/
export function normalizeAccount(account: string): string {
return account.replace(/[^0-9]/g, "");
}
/**
* Validate account format (10-11 digits for Japanese phone numbers)
*/
export function validateAccount(account: string): boolean {
const normalized = normalizeAccount(account);
return /^\d{10,11}$/.test(normalized);
}
/**
* Format date for Freebit API (YYYYMMDD)
*/
export function formatDateForApi(date: Date): string {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}${month}${day}`;
}
/**
* Parse date from Freebit API format (YYYYMMDD)
* @returns Date object or null if invalid format
*/
export function parseDateFromApi(dateString: string): Date | null {
if (!/^\d{8}$/.test(dateString)) return null;
const year = parseInt(dateString.substring(0, 4), 10);
const month = parseInt(dateString.substring(4, 6), 10) - 1; // Month is 0-indexed
const day = parseInt(dateString.substring(6, 8), 10);
return new Date(year, month, day);
}