npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@funren/tool-func

v1.0.1

Published

工具函数

Downloads

1

Readme

常用工具函数

  • timeStamp,//时间戳格式转换

  • totalPage,//分页计算

  • isTest,//判断当前处于测试模式

  • isWeixin,//判断微信端

  • isMobile,//判断移动端

  • isApple,//判断苹果移动端

  • isAndroid,//判断安卓移动端

  • urlParamAdd,//url参数封装

  • urlParamGet,//获取url参数

  • cookieAdd,//新增cookie值

  • cookieGet,//获取cookie值

  • sessionStorageAdd,//存储sessionStorage值

  • sessionStorageGet,//获取sessionStorage值

  • localStorageAdd,//存储localStorage值

  • localStorageGet,//获取localStorage值

  • strJoin,//自定义连接符拼接字符串

  • strTrim,//清除字符串空格

  • strDelRight,//删除右侧字符串

  • lengthRange,//验证字符串长度是否在规定范围

  • letterBegin,//验证字符串是否以字母开头

  • pureNum,//验证字符串是否为纯数字

  • isMail,//验证大陆邮政编码

  • isIdCard,//验证二代身份证

  • isURL,//验证网址

  • isQQ,//验证QQ号

  • isEmail,//验证电子邮箱

  • isPhone,//验证手机号

  • haveSpace,//判断字符串是否包含空格

  • haveCNChars,//判断字符串是否包含中文

安装方式

npm install @funren/tool-func

调用方式

const $tf = require('@funren/tool-func')
$tf.isWeixin()

Functions

cookieAdd([datas])

新增cookie;支持以json字符串形式传入多个cookie值;

Kind: global function

| Param | Type | Default | Description | | --- | --- | --- | --- | | [datas] | Json | {} | cookie名和值 |

Example

cookieAdd({
  name:'Tom',
  age:18
})

cookieGet([cookieName]) ⇒

获取cookie值;默认以json数组格式返回所有cookie;也可获取指定cookie值

Kind: global function
Returns: cookie值

| Param | Type | Default | Description | | --- | --- | --- | --- | | [cookieName] | string | "'all'" | cookie名 |

Example

cookieGet()
// return {name:'tom',age:'18'}

Example

cookieGet('name')
// return 'tom'

isAndroid() ⇒

判断是否为安卓设备

Kind: global function
Returns: Boolean

isApple() ⇒

判断苹果移动设备

Kind: global function
Returns: Boolean

isMobile() ⇒

判断是否为移动端设备访问

Kind: global function
Returns: Boolean

isTest() ⇒

判断开发测试环境;(方便外网测试及输出)

Kind: global function
Returns: Boolean;当url参数中isTest为true或1时返回true;

isWeixin() ⇒

判断微信端

Kind: global function
Returns: Boolean

localStorageAdd(obj)

存储本地数据;(可存储多个数据,支持对象存储;)

Kind: global function

| Param | Type | Description | | --- | --- | --- | | obj | json | 接收json对象格式的数据 |

Example

let opt={
   uid:'01',
   userInfo:{ //以对象方式存储
      name:'Tom',
      age:18
   }
}
localStorageAdd(opt)

localStorageGet(dataName) ⇒

获取localStorage值;可以使用'.'多级链式查询,如:userInfo.name;

Kind: global function
Returns: 参数值/undefined

| Param | Type | Description | | --- | --- | --- | | dataName | String | 需要获取的参数名 |

Example

let obj={
   uid:'001',
   userInfo:{
      name:'tom',
      age:'18',
   }
}

localStorageGet('uid')
//return 001

// 多级参数名链式查找
localStorageGet('userInfo.age')
//return 18

sessionStorageAdd(obj)

存储窗口数据;(可存储多个数据,支持对象存储;)

Kind: global function

| Param | Type | Description | | --- | --- | --- | | obj | Json | 接收json对象格式的数据 |

Example

let opt={
   uid:'01',
   userInfo:{ //以对象方式存储
      name:'Tom',
      age:18
   }
}
sessionStorageAdd(opt)

sessionStorageGet(dataName) ⇒

获取sessionStorage值;可以使用'.'多级链式查询,如:userInfo.name;

Kind: global function
Returns: 查询参数值/undefined

| Param | Type | Description | | --- | --- | --- | | dataName | String | 需要获取的参数名 |

Example

let obj={
   uid:'001',
   userInfo:{
      name:'tom',
      age:'18',
   }
}

sessionStorageGet('uid')
//return 001

// 多级参数名链式查找
sessionStorageGet('userInfo.age')
//return 18

strDelRight(string, [number]) ⇒

删除字符串右侧

Kind: global function
Returns: 新字符串

| Param | Type | Default | Description | | --- | --- | --- | --- | | string | String | | 必须,需要处理的字符串 | | [number] | Number | 1 | 需要删除右侧的几位 |

haveSpace(str) ⇒

判断字符串是否包含空格

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | str | String |

haveCNChars(str) ⇒

判断字符串是否包含中文

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | str | String |

strJoin([arr], [string]) ⇒

自定义连接符拼接字符串

Kind: global function
Returns: 返回拼接完成后的字符串

| Param | Type | Default | Description | | --- | --- | --- | --- | | [arr] | Array | | 需要拼接的字符串数组 | | [string] | String | | 连接符 |

Example

strJoin(['a','b','c'],'-')
//return a-b-c

strTrim(str, [type]) ⇒

字符串空格处理

Kind: global function
Returns: 字符串

| Param | Type | Default | Description | | --- | --- | --- | --- | | str | String | | 需处理的字符串 | | [type] | 'middle' | 'left' | 'right' | 'all' | 'middle' | type 类型 |

Example

let str=' 8 8 ';

strTrim(str);
//8 8清除了左右空格

strTrim(str,'all');
//88清除全部空格

strTrim(str,'left');
//8 8 清除左侧空格

strTrim(str,'right');
// 8 8清除了右侧空格

lengthRange(value, minLength, maxLength) ⇒

验证字符串长度范围

Kind: global function
Returns: Boolean

| Param | Type | Description | | --- | --- | --- | | value | String | | | minLength | Number | 最小长度 | | maxLength | Number | 最大长度 |

Example

let str='ab'
lengthRange(str,1,2)

letterBegin(value) ⇒

验证字符串是否以字母开头

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

pureNum(value) ⇒

验证字符串是否为纯数字

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

isMail(value) ⇒

验证大陆邮政编码

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

isIdCard(value) ⇒

验证二代身份证;共18位,最后一位可为x

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

isURL(value) ⇒

验证是否为网址

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

isQQ(value) ⇒

验证QQ号;非0开头的5~13位整数

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

isEmail(value) ⇒

验证电子邮箱

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

isPhone(value) ⇒

验证手机号

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

isTel(value) ⇒

验证电话号码

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

timeStamp([time], [type], [separator]) ⇒

将时间戳转换成自定义格式

Kind: global function
Returns: -返回自定义日期

| Param | Type | Default | Description | | --- | --- | --- | --- | | [time] | Number | | 时间戳 | | [type] | String | all | 自定义输出格式,年、月、日、时、分、秒(n、y、r、s、f、m);默认all输出所有; | | [separator] | String | | 自定义连接符 |

Example (年-月-日)

timeStamp(1618298335,'nyr','-')
//return 2020-01-02

Example (年、月、日、时、分、秒可任意组合)

timeStamp(1618298335,'nr','|')
//return 2020|02

totalPage(total, pageNum) ⇒

分页计算;通过数据总条数与每页显示数据数量计算出总页数;

Kind: global function
Returns: 返回计算出的总页数

| Param | Type | Description | | --- | --- | --- | | total | Number | 数据总条数 | | pageNum | Number | 每页显示几条数据 |

urlParamAdd([datas], [url]) ⇒

url参数封装(可添加多个);支持中文字符串传参,输出base-64编码字符串

Kind: global function
Returns: 返回参数组合,如:name='tom'&age=18

| Param | Type | Default | Description | | --- | --- | --- | --- | | [datas] | Json | {} | url参数 | | [url] | String | '' | 需要拼接的域名 |

Example

urlParamAdd({
   name:'tom',
   age:18
})

//return bmFtZSUzRCVFNSVCQyVBMCVFNCVCOCU4OSUyNmFnZSUzRDIw

Example

* urlParamAdd({
   name:'tom',
   age:18
},'http://localhost:8080')

//return http://localhost:8080?bmFtZSUzRCVFNSVCQyVBMCVFNCVCOCU4OSUyNmFnZSUzRDIw

urlParamGet([paramName]) ⇒

获取url中参数,可解码URI和base-64

Kind: global function
Returns: 默认返回所有url参数,指定获取参数后则返回指定参数

| Param | Type | Default | Description | | --- | --- | --- | --- | | [paramName] | string | "'all'" | 默认获取url中所有参数,也可指定获取url中某个参数 |

Example

url='http://localhost:8080/?name=tom&age=18';

urlParamGet()
// return {name:'tom',age:'18'}

urlParamGet('name')
// return 'tom'

工具目录

timeStamp,//时间戳格式转换
totalPage,//分页计算
isTest,//判断当前处于测试模式
isWeixin,//判断微信端
isMobile,//判断移动端
isApple,//判断苹果移动端
isAndroid,//判断安卓移动端
urlParamAdd,//url参数封装
urlParamGet,//获取url参数
cookieAdd,//新增cookie值
cookieGet,//获取cookie值
sessionStorageAdd,//存储sessionStorage值
sessionStorageGet,//获取sessionStorage值
localStorageAdd,//存储localStorage值
localStorageGet,//获取localStorage值
strJoin,//自定义连接符拼接字符串
strTrim,//清除字符串空格
strDelRight,//删除右侧字符串
lengthRange,//验证字符串长度是否在规定范围
letterBegin,//验证字符串是否以字母开头
pureNum,//验证字符串是否为纯数字
isMail,//验证大陆邮政编码
isIdCard,//验证二代身份证
isURL,//验证网址
isQQ,//验证QQ号
isEmail,//验证电子邮箱
isPhone,//验证手机号
haveSpace,//判断字符串是否包含空格
haveCNChars,//判断字符串是否包含中文

Functions

cookieAdd([datas])

新增cookie;支持以json字符串形式传入多个cookie值;

Kind: global function

| Param | Type | Default | Description | | --- | --- | --- | --- | | [datas] | Json | {} | cookie名和值 |

Example

cookieAdd({
  name:'Tom',
  age:18
})

cookieGet([cookieName]) ⇒

获取cookie值;默认以json数组格式返回所有cookie;也可获取指定cookie值

Kind: global function
Returns: cookie值

| Param | Type | Default | Description | | --- | --- | --- | --- | | [cookieName] | string | "'all'" | cookie名 |

Example

cookieGet()
// return {name:'tom',age:'18'}

Example

cookieGet('name')
// return 'tom'

isAndroid() ⇒

判断是否为安卓设备

Kind: global function
Returns: Boolean

isApple() ⇒

判断苹果移动设备

Kind: global function
Returns: Boolean

isMobile() ⇒

判断是否为移动端设备访问

Kind: global function
Returns: Boolean

isTest() ⇒

判断开发测试环境;(方便外网测试及输出)

Kind: global function
Returns: Boolean;当url参数中isTest为true或1时返回true;

isWeixin() ⇒

判断微信端

Kind: global function
Returns: Boolean

localStorageAdd(obj)

存储本地数据;(可存储多个数据,支持对象存储;)

Kind: global function

| Param | Type | Description | | --- | --- | --- | | obj | json | 接收json对象格式的数据 |

Example

let opt={
   uid:'01',
   userInfo:{ //以对象方式存储
      name:'Tom',
      age:18
   }
}
localStorageAdd(opt)

localStorageGet(dataName) ⇒

获取localStorage值;可以使用'.'多级链式查询,如:userInfo.name;

Kind: global function
Returns: 参数值/undefined

| Param | Type | Description | | --- | --- | --- | | dataName | String | 需要获取的参数名 |

Example

let obj={
   uid:'001',
   userInfo:{
      name:'tom',
      age:'18',
   }
}

localStorageGet('uid')
//return 001

// 多级参数名链式查找
localStorageGet('userInfo.age')
//return 18

sessionStorageAdd(obj)

存储窗口数据;(可存储多个数据,支持对象存储;)

Kind: global function

| Param | Type | Description | | --- | --- | --- | | obj | Json | 接收json对象格式的数据 |

Example

let opt={
   uid:'01',
   userInfo:{ //以对象方式存储
      name:'Tom',
      age:18
   }
}
sessionStorageAdd(opt)

sessionStorageGet(dataName) ⇒

获取sessionStorage值;可以使用'.'多级链式查询,如:userInfo.name;

Kind: global function
Returns: 查询参数值/undefined

| Param | Type | Description | | --- | --- | --- | | dataName | String | 需要获取的参数名 |

Example

let obj={
   uid:'001',
   userInfo:{
      name:'tom',
      age:'18',
   }
}

sessionStorageGet('uid')
//return 001

// 多级参数名链式查找
sessionStorageGet('userInfo.age')
//return 18

strDelRight(string, [number]) ⇒

删除字符串右侧

Kind: global function
Returns: 新字符串

| Param | Type | Default | Description | | --- | --- | --- | --- | | string | String | | 必须,需要处理的字符串 | | [number] | Number | 1 | 需要删除右侧的几位 |

haveSpace(str) ⇒

判断字符串是否包含空格

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | str | String |

haveCNChars(str) ⇒

判断字符串是否包含中文

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | str | String |

strJoin([arr], [string]) ⇒

自定义连接符拼接字符串

Kind: global function
Returns: 返回拼接完成后的字符串

| Param | Type | Default | Description | | --- | --- | --- | --- | | [arr] | Array | | 需要拼接的字符串数组 | | [string] | String | | 连接符 |

Example

strJoin(['a','b','c'],'-')
//return a-b-c

strTrim(str, [type]) ⇒

字符串空格处理

Kind: global function
Returns: 字符串

| Param | Type | Default | Description | | --- | --- | --- | --- | | str | String | | 需处理的字符串 | | [type] | 'middle' | 'left' | 'right' | 'all' | 'middle' | type 类型 |

Example

let str=' 8 8 ';

strTrim(str);
//8 8清除了左右空格

strTrim(str,'all');
//88清除全部空格

strTrim(str,'left');
//8 8 清除左侧空格

strTrim(str,'right');
// 8 8清除了右侧空格

lengthRange(value, minLength, maxLength) ⇒

验证字符串长度范围

Kind: global function
Returns: Boolean

| Param | Type | Description | | --- | --- | --- | | value | String | | | minLength | Number | 最小长度 | | maxLength | Number | 最大长度 |

Example

let str='ab'
lengthRange(str,1,2)

letterBegin(value) ⇒

验证字符串是否以字母开头

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

pureNum(value) ⇒

验证字符串是否为纯数字

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

isMail(value) ⇒

验证大陆邮政编码

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

isIdCard(value) ⇒

验证二代身份证;共18位,最后一位可为x

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

isURL(value) ⇒

验证是否为网址

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

isQQ(value) ⇒

验证QQ号;非0开头的5~13位整数

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

isEmail(value) ⇒

验证电子邮箱

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

isPhone(value) ⇒

验证手机号

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

isTel(value) ⇒

验证电话号码

Kind: global function
Returns: Boolean

| Param | Type | | --- | --- | | value | String |

timeStamp([time], [type], [separator]) ⇒

将时间戳转换成自定义格式

Kind: global function
Returns: -返回自定义日期

| Param | Type | Default | Description | | --- | --- | --- | --- | | [time] | Number | | 时间戳 | | [type] | String | all | 自定义输出格式,年、月、日、时、分、秒(n、y、r、s、f、m);默认all输出所有; | | [separator] | String | | 自定义连接符 |

Example (年-月-日)

timeStamp(1618298335,'nyr','-')
//return 2020-01-02

Example (年、月、日、时、分、秒可任意组合)

timeStamp(1618298335,'nr','|')
//return 2020|02

totalPage(total, pageNum) ⇒

分页计算;通过数据总条数与每页显示数据数量计算出总页数;

Kind: global function
Returns: 返回计算出的总页数

| Param | Type | Description | | --- | --- | --- | | total | Number | 数据总条数 | | pageNum | Number | 每页显示几条数据 |

urlParamAdd([datas], [url]) ⇒

url参数封装(可添加多个);支持中文字符串传参,输出base-64编码字符串

Kind: global function
Returns: 返回参数组合,如:name='tom'&age=18

| Param | Type | Default | Description | | --- | --- | --- | --- | | [datas] | Json | {} | url参数 | | [url] | String | '' | 需要拼接的域名 |

Example

urlParamAdd({
   name:'tom',
   age:18
})

//return bmFtZSUzRCVFNSVCQyVBMCVFNCVCOCU4OSUyNmFnZSUzRDIw

Example

* urlParamAdd({
   name:'tom',
   age:18
},'http://localhost:8080')

//return http://localhost:8080?bmFtZSUzRCVFNSVCQyVBMCVFNCVCOCU4OSUyNmFnZSUzRDIw

urlParamGet([paramName]) ⇒

获取url中参数,可解码URI和base-64

Kind: global function
Returns: 默认返回所有url参数,指定获取参数后则返回指定参数

| Param | Type | Default | Description | | --- | --- | --- | --- | | [paramName] | string | "'all'" | 默认获取url中所有参数,也可指定获取url中某个参数 |

Example

url='http://localhost:8080/?name=tom&age=18';

urlParamGet()
// return {name:'tom',age:'18'}

urlParamGet('name')
// return 'tom'