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));}
}