Files
helix/web/src/utils/phone-number-utils/index.ts
T
siujamo 965b92b47d chore: restructure into monorepo and add open-source documentation
- Rename helix-server -> server and helix-web -> web
- Remove helix-api (bruno collection and docs site)
- Merge per-module .gitignore files into the root .gitignore
- Add README, AGENTS, CONTRIBUTING and MIT LICENCE
- Add full-stack docker-compose (postgres, redis, server, web via Caddy)
  with web Dockerfile, Caddyfile and Compose server config
- Keep the Gradle wrapper jar tracked after the .gitignore merge
- Copy gradle.properties into the server Docker build image
2026-09-01 10:48:45 +08:00

29 lines
1.1 KiB
TypeScript

import { type CountryCode as RegionAbbreviation, getCountryCallingCode, parsePhoneNumberWithError, PhoneNumber } from "libphonenumber-js"
/**
* Format user's phone number as user's country/region.
*
* @param regionAbbreviation user's region abbreviation
* @param phoneNumber user's phone number
* @return formatted phone number
*/
export function formatInternationalPhoneNumber(regionAbbreviation: string, phoneNumber: string): string {
try {
const _phoneNumber = parsePhoneNumberWithError(phoneNumber, regionAbbreviation as RegionAbbreviation)
if (!_phoneNumber.isValid()) {
console.warn(`Phone number ${_phoneNumber.formatInternational()} is not valid`)
}
return _phoneNumber.formatInternational()
} catch (error) {
console.error("Phone number parsing failed:", error)
const _regionAbbreviation: RegionAbbreviation = regionAbbreviation as RegionAbbreviation
const callingCode = getCountryCallingCode(_regionAbbreviation)
return `+${callingCode} ${phoneNumber}`
}
}
export function getDefaultCountryCode(): RegionAbbreviation {
return import.meta.env.VITE_DEFAULT_REGION_CODE
}