開源項目分享-h5-api


  做前端幾年了,也積累了一些方法,今天拿出來和大家分享一下, 希望對大家有用


使用方法

npm i h5-api --save

引入

import { h5Api } from "h5-api";
  • ts項目需要聲明類型

方法

1. isFirstEntryEdge()

介紹: 判斷是否首次進入頁面

參數:

返回值: 首次進入返回true,否則返回false

例子:

    if ( isFirstEntryEdge() ) {
        do something...
    }

2. isPc()

介紹: 判斷設備是否為電腦

參數:

返回值: 如果是pc端返回true,否則返回false

例子:

    if ( isPc() ) {
        do something...
    }

3. pageJump( url, blank )

介紹: 跳轉頁面

參數:

  • url 跳轉的地址 string類型(開頭必須包含http或者https)
  • blank 是否在新頁面打開 boolean類型 默認值: true

返回值:

例子:

    pageJump("https://www.liuguisheng.vip", true);

4. prodeceRandomString( length, isDate )

介紹: 生成一個隨機字符串

參數:

  • length 隨機數長度 number類型
  • isDate 是否添加時間戳 boolean類型 默認值: false

返回值: 生成的字符串

例子:

    let str =  prodeceRandomString( 10, true );
    
    console.log(str);

5. moneyToCapitalsFun( n )

介紹: 判斷設備是否為電腦

參數:

  • n 小寫金額 number類型

返回值: 返回生成的大寫金額

例子:

    let capitalizeMoney =  prodeceRandomString( 10, true );
    
    console.log(capitalizeMoney);

6. getCurrentUrl ()

介紹: 獲取地址欄url

參數:

返回值: 地址欄的url

例子:

    let url = getCurrentUrl();
    
    console.log(url);

7. deepCopy ( value )

介紹: 深拷貝方法

參數:

  • value 需要拷貝的值 任意類型

返回值: 深拷貝后的值

例子:

    let obj = {
        name: "liu",
        home: "https://www.liuguisheng.vip"
    }
    let result = deepCopy(obj);
    
    console.log(obj === result);

8. emptyCheck

介紹: 校驗一個對象是否有空值

參數: 每個方法的傳參有所區別

內置方法:

  • emptyCheck.checkObject( obj, unInclude ) 校驗對象, obj為需要校驗的對象,unInclude為不需要校驗的key
  • emptyCheck.checkNumber ( num ) 校驗數字
  • emptyCheck.checkString ( str ) 校驗字符串
  • emptyCheck.checkArray ( arr ) 校驗數組

返回值: 如果有空值返回true, 否則返回false

例子:

    let obj = {
        name: "liu",
        home: "https://www.liuguisheng.vip",
        info: {
            age: 27,
            idCard: '',
            girlFriend: ''
        }
    }

    let unInclude = ['girlFriend'];

    let check = new emptyCheck();

    let result = check.checkObject(obj, unInclude);
    
    console.log(result);

9. charToPx ( length, fontSize )

介紹: 字符轉像素

參數:

  • length 字符長度 number類型
  • fontSize 字體大小 number類型

返回值: 生成的像素值

例子:

    let result = charToPx ( 30, 14 );
    console.log( result );

10. cmToPx ( cm )

介紹: 厘米轉像素

參數:

  • cm 厘米 number類型

返回值: 生成的像素值

例子:

    let result = cmToPx ( 30 );
    console.log( result );

11. arrayIntersection ( arr1, arr2 )

介紹: 兩個數組取交集

參數:

  • arr1 數組1 any[] 類型
  • arr2 數組1 any[] 類型

返回值: 生成的新數組

例子:

    let arr1 = [1,2,3,4];
    let arr2 = [3,4,5,6];
    let result = arrayIntersectionFun ( arr1, arr2 );
    console.log( result );

12. arraySubtraction ( arr1, arr2 )

介紹: 兩個數組取交集

參數:

  • arr1 數組1 any[] 類型
  • arr2 數組1 any[] 類型

返回值: 生成的新數組

例子:

    let arr1 = [1,2,3,4];
    let arr2 = [3,4,5,6];
    let result = arraySubtractionFun ( arr1, arr2 );
    console.log( result );

13. keyNameMapping ( obj, config )

介紹: 對象鍵名映射(只支持簡單對象)

參數:

  • obj 原對象 object類型
  • config 映射配置 object類型

返回值: 生成的新對象

例子:

    let obj = {
        name: "liu",
        home: "https://www.liuguisheng.vip",
        age: 27
    };
    let config = {
        name: "id",
        home: "index",
        info: "message"
    }

    let result = keyNameMapping ( obj, config );
    console.log( result );

14. telMosaic ( tel )

介紹: 手機號打碼

參數:

  • tel 手機號碼 number|字符串類型

返回值: 生成的新手機號

例子:

    let result = telMosaic ( 17698443243 );
    console.log( result );

15. idCardMosaic ( idCard )

介紹: 手機號打碼

參數:

  • idCard 身份證號碼 number|字符串類型

返回值: 生成的身份證

例子:

    let result = idCardMosaic ( 130282199508145154 );
    console.log( result );


開源協議

MIT License


源碼地址

地址

博客地址

博客


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM