harmony-utils之FormatUtil,格式化工具類
harmony-utils 簡(jiǎn)介與說(shuō)明
harmony-utils 一款功能豐富且極易上手的HarmonyOS工具庫(kù),借助眾多實(shí)用工具類,致力于助力開發(fā)者迅速構(gòu)建鴻蒙應(yīng)用。其封裝的工具涵蓋了APP、設(shè)備、屏幕、授權(quán)、通知、線程間通信、彈框、吐司、生物認(rèn)證、用戶首選項(xiàng)、拍照、相冊(cè)、掃碼、文件、日志、異常捕獲、字符、字符串、數(shù)字、集合、日期、隨機(jī)、base64、加密、解密、JSON等一系列的功能和作,能夠滿足各種不同的開發(fā)需求。
picker_utils 是harmony-utils拆分出來(lái)的一個(gè)子庫(kù),包含 PickerUtil、PhotoHelper、ScanUtil。
下載安裝ohpm i @pura/harmony-utils
ohpm i @pura/picker_utils
//全局初始化方法,在UIAbility的onCreate方法中初始化 AppUtil.init()
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
AppUtil.init(this.context);
}
API方法與使用
isPhone 判斷傳入的電話號(hào)碼格式是否正確
let phone: string = "19869062586";
let bl = FormatUtil.isPhone(phone);
getPhoneFormat 對(duì)電話號(hào)碼進(jìn)行格式化
let phone: string = "19869062586";
let format = FormatUtil.getPhoneFormat(phone);
getPhoneLocationName 獲取電話號(hào)碼歸屬地
let phone: string = "19869062586";
let locationName = FormatUtil.getPhoneLocationName(phone);
transliterator 將輸入字符串從源格式轉(zhuǎn)換為目標(biāo)格式(中文漢字轉(zhuǎn)為拼音)
let str = FormatUtil.transliterator("中國(guó)");
getFormatPercentage 格式化百分比,將數(shù)字轉(zhuǎn)化從百分比字符串
let percentage = FormatUtil.getFormatPercentage(0.491);
LogUtil.error(`percentage: ${percentage}`);
getFormatPhone 格式化手機(jī)號(hào)碼,隱藏中間四位
let phone: string = "18969062528";
let str = FormatUtil.getFormatPhone(phone);
getFormatCardNo 格式化身份證號(hào)碼,隱藏中間部分?jǐn)?shù)字
let str = FormatUtil.getFormatCardNo("110101199001011234");
LogUtil.error(`格式化后的身份證號(hào): ${str}`);
getFormatFileSize 格式化文件大小
let str = FormatUtil.getFormatFileSize(1024102400);
LogUtil.error(`文件大小: ${str}`);
getTruncateText 縮短長(zhǎng)文本,超出部分用省略號(hào)表示
let str = FormatUtil.getTruncateText("女人什么年齡是最好?任何年齡都可以。因?yàn)槊總€(gè)年齡段都有不一樣的美。看XX的穿搭你就會(huì)發(fā)現(xiàn)成熟女性的魅力,反而會(huì)更加的吸引人。", 20);
LogUtil.error(`縮短長(zhǎng)文本: ${str}`);
getIconFont 解析iconFont字符
let str = FormatUtil.getIconFont("e631");
LogUtil.error(`getIconFont: ${str}`);
getQueryValue 獲取url里的參數(shù),Key對(duì)應(yīng)的Value
let url = "https://blog.csdn.net/article?spm=blog1024&name=李斯";
let value1 = FormatUtil.getQueryValue(url, 'spm');
let value2 = FormatUtil.getQueryValue(url, 'name');
let value3 = FormatUtil.getQueryValue(url, 'mona');
getParamsUrl 將參數(shù)拼接在url上,返回新的url
let map = new Map< string, Any >();
map.set("id", 100011);
map.set("name", '王五');
map.set("sex", undefined);
let url1 = FormatUtil.getParamsUrl("https://blog.csdn.net?id=100012", map);
let obj: Record< string, Object > = {};
obj['key'] = 'show';
obj['user'] = '王五';
obj['pd'] = 0;
obj['type'] = 'csaitab';
let url2 = FormatUtil.getParamsUrl("https://blog.csdn.net?id=100012", obj);
創(chuàng)作不易,請(qǐng)給童長(zhǎng)老點(diǎn)贊
審核編輯 黃宇
-
Harmony
+關(guān)注
關(guān)注
0文章
108瀏覽量
3017
發(fā)布評(píng)論請(qǐng)先 登錄
評(píng)論