Website : rimsha.abasa.com
backdoor
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
var
/
www
/
mudeer-web-backup30April26
/
src
/
lib
/
Filename :
country-phone-utils.ts
back
Copy
import { getNames, getCode } from 'country-list'; import { getCountryCallingCode, getCountries, type Country } from 'react-phone-number-input'; export interface CountryOption { code: string; name: string; callingCode: string; } /** * Get list of countries with their codes, names, and calling codes * Uses country-list for country names and react-phone-number-input for calling codes */ export function getCountryPhoneList(): CountryOption[] { const countryNames = getNames(); const phoneCountries = getCountries(); const countries: CountryOption[] = []; // Iterate through all country names from country-list countryNames.forEach((countryName) => { const isoCode = getCode(countryName); if (isoCode && phoneCountries.includes(isoCode as Country)) { try { const callingCode = getCountryCallingCode(isoCode as Country); countries.push({ code: isoCode, name: countryName, callingCode: `+${callingCode}`, }); } catch (error) { // Skip countries without calling codes console.warn(`No calling code found for ${isoCode} (${countryName})`); } } }); // Sort alphabetically by country name return countries.sort((a, b) => a.name.localeCompare(b.name)); } /** * Get country option by ISO code */ export function getCountryByCode(code: string): CountryOption | undefined { const countries = getCountryPhoneList(); return countries.find((country) => country.code === code); } /** * Get country option by calling code */ export function getCountryByCallingCode(callingCode: string): CountryOption | undefined { const countries = getCountryPhoneList(); return countries.find((country) => country.callingCode === callingCode); } /** * Parse phone number to extract country code and number */ export function parsePhoneNumber(phoneNumber: string): { countryCode: string; number: string } | null { if (!phoneNumber || !phoneNumber.startsWith('+')) { return null; } const countries = getCountryPhoneList(); // Try to match the longest calling code first const sortedCountries = countries.sort((a, b) => b.callingCode.length - a.callingCode.length); for (const country of sortedCountries) { if (phoneNumber.startsWith(country.callingCode)) { return { countryCode: country.code, number: phoneNumber.substring(country.callingCode.length).trim(), }; } } return null; }