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 🙏

© 2026 – Pkg Stats / Ryan Hefner

js-util-zhangxiaowei

v1.0.0

Published

js/ts常用工具包

Downloads

9

Readme

ts-util

一.介绍

用 typescript 写的常用工具类

通过作者平时开发积累和一些现网上零散整理,把一些常用的工具类封装起来。

二.如何使用

yarn  add  js-util-zhang
import * as util from 'js-util-zhang';

三.API

1.string

1.1 strCheck 验证

util.strCheck(str:string, type: StrEnum) => boolean

enum StrEnum {
  PHONE = 'phone', //校验手机号
  TEL = 'tel',     //检验电话
  CARD = 'card',   //检验身份证
  PWD = 'pwd',
  POSTAL = 'postal',
  QQ = 'qq',
  EMAIL = 'email',
  MONEY = 'money',
  URL = 'URL',
  IP = 'IP',
  DATE = 'date',
  NUMBER ='number',
  ENGLISH = 'english',
  CHINESE = 'chinese',
  LOWER= 'lower',
  UPPER = 'upper',
  HTML = 'HTML'
}

// example
util.strCheck('18268100000', 'phone') => true

1.2 strTransformName 两个字名字中间加空格

util.strTransformName(name: string) => cname
// example
util.strTransformName('王五') => '王 五'

1.3 hideMaxString 超出隐藏

util.hideMaxString = (str:String, len: number): String

// example
util.hideMaxString = ('123456789',  4) = > '1234...'

1.4 strlen 计算字符串长度

strlen = (str: string): number

// example
util.strlen = ('String 类型长度') = > 7.5

1.5 formatMoney 格式化金额

util.formatMoney = (str: any): string

// example
util.formatMoney = ('123456789'): '123,456,789'

1.6 maskPhone 对手机号进行去敏处理,用 *替换

util.maskPhone = (phone: string, start: number = 3, len: number = 4): string
// example
util.maskPhone = ('13641601234'): '136****1234'

1.7 maskEMail() 对电子邮箱进行去敏处理,用*替换

util.maskEMail = (email: string):string
// example
util.maskEMail = ('[email protected]'): '[email protected]'

1.8 maskName() 对姓名进行去敏处理,用*替换

util.maskName  = (name: string):string
// example
util.maskName = ('张三'): '张*'

1.9 mask() 对字符串进行去敏处理,用*替换

util.mask = (text: string, start: number, end: number, len?: number): string
// example
util.mask = ('123456', 2, 3) : '12****456'

1.10 maskIDCard() 对身份证进行去敏处理,用*替换

util.maskIDCard = (idcard: string, start: number = 6, len: number = 8): string
// example
util.maskIDCard = ('110410199001011234', 6, 10):  '110410**********34'

1.11 idcardIsValid() 检查身份证是否合法

util.idcardIsValid = (idcard: string): boolean
// example
util.idcardIsValid = ('110410199001011234'): false

1.12 contains 判断字符串是否包含某个字符串

util.contains = (input: string, searchSeq: string): boolean
// example
util.contains = ('abcdefg', 'abc'): true

1.13 equalsIgnoreCase 忽略大小写判断两个字符串是否相等

util.equalsIgnoreCase = ('equalsIgnoreCasE', 'equalsIgnoreCase'): boolean
// example
util.equalsIgnoreCase = ('equalsIgnoreCasE', 'equalsIgnoreCase'): true

1.14 equals 判断字符串是否包含某个字符串

util.equals = (input1: string | number, input2: string | number): boolean
// example
util.equals = ('equalsIgnoreCasE', 'equalsIgnoreCase'): false

1.15 containsWhitespace 判断字符串是否含有空格

util.containsWhitespace = (input: string): boolean
// example
util.containsWhitespace = ('我是 测试的字符串 '): true

1.16 deleteWhitespace 去除字符串中的空格

util.deleteWhitespace = (input: string): string
// example
util.containsWhitespace = ('我是 测试的字符串 '): '我是测试的字符串'

1.17 isAllUpperCase 判断字符串是否为大写字母

util.isAllUpperCase = (input: string): boolean
// example
util.isAllUpperCase = ('isAllLowerCase'): false

1.18 isAllLowerCase 判断字符串是否为小写字母

util.isAllLowerCase = (input: string): boolean
// example
util.isAllLowerCase = ('abcdefg'): true

1.19 isWhitespace 判断字符串是否为空格

util.isWhitespace = (input: string): boolean
// example
util.isWhitespace = ('22.22'): false

1.20 isNumericSpace 判断字符串是否为数字、空格

util.isNumericSpace = (input: string): boolean
// example
util.isNumericSpace = ('2222aa'): false

1.20 isNegativeInteger 判断字符串是否为负整数

util.isNegativeInteger = (input: string): boolean
// example
util.isNegativeInteger = ('22.22'): false

1.21 isPositiveInteger 判断字符串是否为正整数

util.isPositiveInteger = (input: string): boolean
// example
util.isPositiveInteger = ('22.22'): true

1.22 isStringInteger 判断字符串是否为整数

util.isStringInteger = (input: string): boolean
// example
util.isStringInteger = ('22.22'): false

1.23 isDecimal 判断字符串是否为小数

util.isDecimal = (input: string): boolean
// example
util.isDecimal = ('22.22'): true

1.24 isNegativeDecimal 判断字符串是否为负小数

util.isNegativeDecimal  = (input: string): boolean
// example
util.isNegativeDecimal  = ('22.22'): false

1.25 isPositiveDecimal 判断字符串是否为正小数

util.isPositiveDecimal = (input: string): boolean
// example
util.isPositiveDecimal = ('22.22'): true

2.object

2.1 objIsNull 对象判空


util.objIsNull(obj: object) => boolean

// example
const obj = {}
util.objIsNull(obj) => false // 传参不需要判空,代码里已判空

2.2 isObject 是否是对象


util.isObject(obj: object) => boolean

// example
const obj = {}
util.objIsNull(obj) => true

3.array

3.1 arrIsNull 数组判空


util.arrIsNull(arr: any[]) => boolean

// example
const arr = []
util.arrIsNull(arr) => false // 传参不需要判空,代码里已判空

3.2 arrAttributeSort 数组排序 从小到大或者从 大到小


util.arrAttributeSort(arr: any[], attribute?: any, num?: boolean) => any[]
// desc:
数组排序
(1)arr : 传入的数组
(2)attribute: 是否根据数组对象的某个属性排序
(3)num : 从小到大 或者 从大到小
// example
let dataSort = [
{name: "教授", total: 48},
{ name: "副教授", total: 26 },
{ name: "高级工程师", total: 13 },
{ name: "研究员", total: 12 },
{ name: "多福多寿", total: 10 },
{ name: "打发第三方", total: 16 },
]
util.arrAttributeSort(dataSort, 'total', true) => [
{ name: '教授', total: 48 },
{ name: '副教授', total: 26 },
{ name: '高级工程师', total: 13 },
{ name: '研究员', total: 12 },
{ name: '多福多寿', total: 10 },
{ name: '打发第三方', total: 16 }
]

3.3 isArray 判断是否为数组


util.isArray(arr: any[]) => boolean

// example
const arr = []
util.isArray(arr) => true

4.store

4.1 storeCookieSet 设置某个 cookie


util.storeCookieSet(str: string, value: string) => void

// example
util.storeCookieSet('token', 'token')

4.2 storeCookieGet 获得 coookie 中某个值


util.storeCookieGet(str: string) => string

// example
document.cookie="token=token";
util.storeCookieGet('token') => 'token'

4.3 storeCookieDelete 删除单条 cookie


util.storeCookieDelete(str: string) => void

4.4 storeCookieRemove 删除所有 cookie


util.storeCookieRemove() =

4.5 storeLocalStorageSet 设置 localStorage 已经 JSON.stringify


util.storeLocalStorageSet(name: string, value: any) => void

4.6 storeLocalStorageGet 获取 localStorage 已经 JSON.parse


util.storeLocalStorageGet(str: string) => any

5.url

5.1 uriGetParam 获取 urL 上的某个参数


util.uriGetParam(str: string) => string

// example
www.xxx.com?a=1&b=2
util.uriGetParam('a') => '1'

6.浏览器

6.1 browserGetType 获取浏览器类型


util.browserGetType() => string

// example
util.browserGetType() => 'Opera' || 'IE' || 'Edge' || 'Firefox' || 'Safari' || 'Chrome' || 'OverIE10'

6.2 browserIsNew 是否是现代浏览器(IE11 及以上)


util.browserIsNew() => boolean

// example
// 如果是 IE11 及以上,返回 true
util.browserIsNew() => true

6.3 backScrollTop 让页面回到具体位置


util.backScrollTop()

// example
util.backScrollTop()

7.func

7.1 判断是否为函数


util.isFunc(fn: object) => boolean

// example
function aa () {}
util.isFunc(aa) => true

8. 图片

8.1 给图片路径加大小


util.changeImgurl = (url: string, width: number, height: number, hostUrl = false): string

9. 判断类型


/\*\*

- 判断是否为 undefined
  \*/
  const isUndefined: (obj: any) => boolean = \_createTypeOf('Undefined');

/\*\*

- 判断是否为 null
  \*/
  const isNull: (obj: any) => boolean = \_createTypeOf('Null');

/\*\*

- 判断是否为 Array 类型
  \*/
  const isArray: (obj: any) => boolean = Array.isArray || \_createTypeOf('Array');

/\*\*

- 判断是否为 Function 类型
  \*/
  const isFunction: (obj: any) => boolean = \_createTypeOf('Function');

/\*\*

- 判断是否为 Boolean 类型
  \*/
  const isBoolean: (obj: any) => boolean = \_createTypeOf('Boolean');

/\*\*

- 判断是否为 String 类型
  \*/
  const isString: (obj: any) => boolean = \_createTypeOf('String');

/\*\*

- 判断是否为 Number 类型
  \*/
  const isNumber: (obj: any) => boolean = \_createTypeOf('Number');

/\*\*

- 判断是否为 RegExp 类型
  \*/
  const isRegExp: (obj: any) => boolean = \_createTypeOf('RegExp');

/\*\*

- 判断是否为 Object 类型
  \*/
  const isObject: (obj: any) => boolean = \_createTypeOf('Object');

/\*\*

- 判断是否为 Date 类型
  \*/
  const isDate: (obj: any) => boolean = \_createTypeOf('Date');

/\*\*

- 判断是否为 Error 类型
  \*/
  const isError: (obj: any) => boolean = \_createTypeOf('Error');

/\*\*

- 获取对象的类型
- @param obj
  \*/

getType(obj: any): string

/\*\*

- 判断是否为空,包括空对象,空数组,空字符串,null,undefined
- @param obj 任意对象
  \*/

isEmpty(obj: any): boolean

10. 文件

10.1 openForm 导出文件


util.openForm(url, params, method)
/\*
\*@param {String} url 提交的地址 (后端的接口地址)

- @param {Object} params 提交的数据
- @param {String} method 提交的方式
  \*/
  // example
  const url = `http://192.168.22.8:3002/manage/data/export/1`
  util.openForm = (url, {}, "POST")

// 最后得到一个 excel

11.number

11.1 判断是否为整数


util.isInteger = (num: number): boolean
// example
util.isInteger(123): true

11.2 判断是否为小数


util.isFloat(num: number): boolean
// example
util.isFloat(1.555): true

11.3 检查数字是否在指定区间内


- @param num 要检测的数字
- @param start 区间的开始,默认值为 0
- @param end 区间的结束(区间不包含此边界值)

util.inRange = (num: number, start: number, end: number): boolean
// example
util.inRange(4, 8): true

11.4 生成随机数,支持随机整数和随机小数


- @param {Number} lower 下边界,默认 0
- @param {Number} upper 上边界,默认 1
- @param {Boolean} floating 随机小数的位数,默认为 0,即随机整数

util.random= (lower: number, upper: number, floating: number = 0): number
// example
util.random(1.2, 5.2, 1): 1.5

11.5 生成随机数,支持随机整数和随机小数


util.isaFinite = (num: number): boolean
// example
util.isaFinite(123): true

11.6 判断是否是 NaN


util.isaNaN = (num: number): boolean
// example
util.isaNaN(123): flase

11.7 转为数字


- @param value 要转成数字的值
  util.toNumber = (value: any): number
  // example
  util.toNumber = ('123'): 123

11.8 转为有理数


util.toFinite = (value: any): number
// example
util.toFinite(3.2) : 3.2
util.toFinite(Number.MIN_VALUE) : 5e-324

11.9 除法 保留几位小数


util.rateNumber = (value: number, divisor:  number, num: number): string
// example
util.rateNumber(788, 10, 2) : 78.80

11. 其他

11.1 动态加载 js

util.dynamicScript = (url:string, name?:string)
// example

util.dynamicScript = ('/console/static/UE/ueditor.config.js')

11.2 动态加载 css

util.dynamicScript = (url:string)
// example

util.dynamicScript = ('/console/static/UE/ueditor.css')

四. 测试

npm run test 启动 src/tests 里面是测试的实例