import { i18n } from '@kit.LocalizationKit';
/**
* 格式化工具类
* 提供电话号码格式化、归属地查询、字符转换等功能。
* author: 鸿蒙布道师
* since: 2025/04/14
*/
export class FormatUtil {
/**
* 判断传入的电话号码格式是否正确。
* @param phone - 待验证的电话号码
* @param country - 电话号码所属国家或地区代码,默认为 "CN"(中国)
* @param option - 电话号码格式化选项,默认为 NATIONAL
* @returns 如果电话号码格式正确返回 true,否则返回 false
*/
static isPhone(
phone: string,
country: string = "CN",
option?: i18n.PhoneNumberFormatOptions
): boolean {
if (!phone || !country) {
throw new Error("Phone number and country code cannot be empty.");
}
const phoneNumberFormat = new i18n.PhoneNumberFormat(country, option);
return phoneNumberFormat.isValidNumber(phone);
}
/**
* 对电话号码进行格式化。
* @param phone - 待格式化的电话号码
* @param country - 电话号码所属国家或地区代码,默认为 "CN"(中国)
* @param option - 电话号码格式化选项,默认为 NATIONAL
* @returns 格式化后的电话号码字符串
*/
static getPhoneFormat(
phone: string,
country: string = "CN",
option?: i18n.PhoneNumberFormatOptions
): string {
if (!phone || !country) {
throw new Error("Phone number and country code cannot be empty.");
}
const phoneNumberFormat = new i18n.PhoneNumberFormat(country, option);
return phoneNumberFormat.format(phone);
}
/**
* 获取电话号码归属地。
* @param phone - 待查询的电话号码
* @param locale - 区域 ID,默认为 "zh-CN"
* @param country - 电话号码所属国家或地区代码,默认为 "CN"(中国)
* @param option - 电话号码格式化选项,默认为 NATIONAL
* @returns 电话号码归属地名称
*/
static getPhoneLocationName(
phone: string,
locale: string = "zh-CN",
country: string = "CN",
option?: i18n.PhoneNumberFormatOptions
): string {
if (!phone || !country || !locale) {
throw new Error("Phone number, country code, and locale cannot be empty.");
}
const phoneNumberFormat = new i18n.PhoneNumberFormat(country, option);
return phoneNumberFormat.getLocationName(phone, locale);
}
/**
* 将输入字符串从源格式转换为目标格式(如中文汉字转为拼音)。
* @param text - 输入字符串
* @returns 转换后的字符串
*/
static transliterator(text: string): string {
if (!text) {
throw new Error("Input text cannot be empty.");
}
const transliterator = i18n.Transliterator.getInstance('Any-Latn');
return transliterator.transform(text);
}
/**
* 解析 iconFont 字符。
* @param iconFont - iconFont 字符(如 "e631")
* @returns 解析后的 Unicode 字符
*/
static getIconFont(iconFont: string): string {
if (!iconFont) {
throw new Error("Icon font string cannot be empty.");
}
return String.fromCharCode(parseInt(iconFont, 16));
}
}
代码如下:
import { i18n } from '@kit.LocalizationKit';
/**
* 格式化工具类
* 提供电话号码格式化、归属地查询、字符转换等功能。
* author: 鸿蒙布道师
* since: 2025/04/14
*/
export class FormatUtil {
/**
* 判断传入的电话号码格式是否正确。
* @param phone - 待验证的电话号码
* @param country - 电话号码所属国家或地区代码,默认为 "CN"(中国)
* @param option - 电话号码格式化选项,默认为 NATIONAL
* @returns 如果电话号码格式正确返回 true,否则返回 false
*/
static isPhone(
phone: string,
country: string = "CN",
option?: i18n.PhoneNumberFormatOptions
): boolean {
if (!phone || !country) {
throw new Error("Phone number and country code cannot be empty.");
}
const phoneNumberFormat = new i18n.PhoneNumberFormat(country, option);
return phoneNumberFormat.isValidNumber(phone);
}
/**
* 对电话号码进行格式化。
* @param phone - 待格式化的电话号码
* @param country - 电话号码所属国家或地区代码,默认为 "CN"(中国)
* @param option - 电话号码格式化选项,默认为 NATIONAL
* @returns 格式化后的电话号码字符串
*/
static getPhoneFormat(
phone: string,
country: string = "CN",
option?: i18n.PhoneNumberFormatOptions
): string {
if (!phone || !country) {
throw new Error("Phone number and country code cannot be empty.");
}
const phoneNumberFormat = new i18n.PhoneNumberFormat(country, option);
return phoneNumberFormat.format(phone);
}
/**
* 获取电话号码归属地。
* @param phone - 待查询的电话号码
* @param locale - 区域 ID,默认为 "zh-CN"
* @param country - 电话号码所属国家或地区代码,默认为 "CN"(中国)
* @param option - 电话号码格式化选项,默认为 NATIONAL
* @returns 电话号码归属地名称
*/
static getPhoneLocationName(
phone: string,
locale: string = "zh-CN",
country: string = "CN",
option?: i18n.PhoneNumberFormatOptions
): string {
if (!phone || !country || !locale) {
throw new Error("Phone number, country code, and locale cannot be empty.");
}
const phoneNumberFormat = new i18n.PhoneNumberFormat(country, option);
return phoneNumberFormat.getLocationName(phone, locale);
}
/**
* 将输入字符串从源格式转换为目标格式(如中文汉字转为拼音)。
* @param text - 输入字符串
* @returns 转换后的字符串
*/
static transliterator(text: string): string {
if (!text) {
throw new Error("Input text cannot be empty.");
}
const transliterator = i18n.Transliterator.getInstance('Any-Latn');
return transliterator.transform(text);
}
/**
* 解析 iconFont 字符。
* @param iconFont - iconFont 字符(如 "e631")
* @returns 解析后的 Unicode 字符
*/
static getIconFont(iconFont: string): string {
if (!iconFont) {
throw new Error("Icon font string cannot be empty.");
}
return String.fromCharCode(parseInt(iconFont, 16));
}
}