h5-api

h5 api 集合

View project on GitHub

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. moneyToCapitals( 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 );

16. judgeValueType ( value )

介绍: 判断某个值的类型

参数:

  • value 值 任意类型

返回值: 类型

例子:

    let result = judgeValueType ( [1,2,3,4] );
    console.log( result );



开源协议

MIT License


源码地址

地址

博客地址

博客