phone, detectCountry, phoneCountries
单个国家的掩码、根据号码识别国家以及所有国家的列表 —— API 与 tinyval-mask/presets 相同。
签名
ts
function phone(country: PhoneCountry): Mask
function detectCountry(number: string, countries?: PhoneCountry[]): PhoneCountry | undefined
function phoneCountries(): CountryInfo[]
const phoneTable: Record<PhoneCountry, [dial, pattern, trunk?, detect?]> // данные пакета
function setCountries(countries: Record<string, CountryEntry | null>, options?: { replace?: boolean }): void
function getCountries(): PhoneTable // с изменениями из setCountries
// из tinyval-mask/presets, для удобства
function flag(code: string): string
function countryName(code: string, locale?: string): string注意事项
phone(country)—— 以国家代码开头的掩码:+81 99-9999-9999。国家代码可以不输入,国内长途前缀也可以不输入:09012345678→+81 90-1234-5678。detectCountry按包含国家代码的数字识别,不考虑默认国家:detectCountry('9012345678')是+90(土耳其)。phoneCountries()—— 按代码字母顺序排列的 245 个国家。名称通过countryName以界面语言获取。setCountries为本包的所有函数修改、添加(ab: […])和删除(xk: null)国家;replace: true—— 只保留传入的国家。请在启动时、创建掩码之前调用一次。可编辑的文件 ——npx tinyval-phone countries,见 自定义国家和国旗。
示例
ts
import { mask } from 'tinyval-mask';
import { phone, detectCountry, phoneCountries, countryName } from 'tinyval-phone';
mask('09012345678', phone('jp')); // '+81 90-1234-5678'
mask('0612345678', phone('fr')); // '+33 6 12 34 56 78'
detectCountry('+971501234567'); // 'ae'
detectCountry('+1 242 359 1234'); // 'bs'
// свой выбор страны
const options = phoneCountries().map((c) => ({
value: c.code,
label: `${c.flag} ${countryName(c.code, 'ru')} +${c.dial}`,
}));