|
| 1 | +import { getCountries, getCountryCallingCode, parsePhoneNumberFromString } from "libphonenumber-js"; |
| 2 | + |
| 3 | +const regionNames = |
| 4 | + typeof Intl !== "undefined" && typeof Intl.DisplayNames === "function" |
| 5 | + ? new Intl.DisplayNames(["zh-CN"], { type: "region" }) |
| 6 | + : null; |
| 7 | + |
| 8 | +function maskSegment(value) { |
| 9 | + const text = String(value || "").trim(); |
| 10 | + if (!text) { |
| 11 | + return ""; |
| 12 | + } |
| 13 | + if (text.length <= 4) { |
| 14 | + return text; |
| 15 | + } |
| 16 | + if (text.length <= 6) { |
| 17 | + return `${text.slice(0, 2)}***${text.slice(-2)}`; |
| 18 | + } |
| 19 | + if (text.length <= 8) { |
| 20 | + return `${text.slice(0, 3)}***${text.slice(-2)}`; |
| 21 | + } |
| 22 | + return `${text.slice(0, 3)}****${text.slice(-4)}`; |
| 23 | +} |
| 24 | + |
| 25 | +export const PHONE_COUNTRY_OPTIONS = getCountries() |
| 26 | + .map((iso2) => ({ |
| 27 | + iso2, |
| 28 | + name: regionNames?.of(iso2) || iso2, |
| 29 | + callingCode: getCountryCallingCode(iso2), |
| 30 | + })) |
| 31 | + .sort((a, b) => { |
| 32 | + if (a.iso2 === "CN") return -1; |
| 33 | + if (b.iso2 === "CN") return 1; |
| 34 | + return a.name.localeCompare(b.name, "zh-CN"); |
| 35 | + }); |
| 36 | + |
| 37 | +export function normalizeBindPhone(phone, country = "CN") { |
| 38 | + const value = String(phone || "").trim(); |
| 39 | + if (!value) { |
| 40 | + return ""; |
| 41 | + } |
| 42 | + |
| 43 | + const phoneNumber = value.startsWith("+") |
| 44 | + ? parsePhoneNumberFromString(value) |
| 45 | + : parsePhoneNumberFromString(value, country || "CN"); |
| 46 | + |
| 47 | + return phoneNumber?.isValid() ? phoneNumber.number : ""; |
| 48 | +} |
| 49 | + |
| 50 | +export function normalizeStoredPhone(phone) { |
| 51 | + const value = String(phone || "").trim(); |
| 52 | + if (!value) { |
| 53 | + return ""; |
| 54 | + } |
| 55 | + |
| 56 | + return normalizeBindPhone(value, value.startsWith("+") ? undefined : "CN"); |
| 57 | +} |
| 58 | + |
| 59 | +export function detectPhoneCountry(phone) { |
| 60 | + const normalized = normalizeStoredPhone(phone); |
| 61 | + if (!normalized) { |
| 62 | + return "CN"; |
| 63 | + } |
| 64 | + |
| 65 | + return parsePhoneNumberFromString(normalized)?.country || "CN"; |
| 66 | +} |
| 67 | + |
| 68 | +export function maskPhone(phone) { |
| 69 | + const value = String(phone || "").trim(); |
| 70 | + if (!value) { |
| 71 | + return ""; |
| 72 | + } |
| 73 | + |
| 74 | + const normalized = normalizeStoredPhone(value); |
| 75 | + const phoneNumber = normalized ? parsePhoneNumberFromString(normalized) : null; |
| 76 | + if (phoneNumber?.isValid()) { |
| 77 | + const prefix = `+${phoneNumber.countryCallingCode}`; |
| 78 | + const national = maskSegment(phoneNumber.nationalNumber); |
| 79 | + return national ? `${prefix} ${national}` : prefix; |
| 80 | + } |
| 81 | + |
| 82 | + const digits = value.replace(/\D/g, ""); |
| 83 | + if (digits.length >= 7) { |
| 84 | + return maskSegment(digits); |
| 85 | + } |
| 86 | + |
| 87 | + return value; |
| 88 | +} |
0 commit comments