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

@web-xhh/web-utils

v1.0.8

Published

前端通用工具方法

Readme

Description

web前端通用工具方法

Install

npm i @web-xhh/web-utils -S

Usage

按需导入

例如:

import { trimFn } from '@web-xhh/web-utils'

API

throttle

/**
 * @description 节流(支持设置立即执行)
 * @param {function} fn 执行的函数
 * @param {number} wait 执行间隔时长,默认1s
 * @param {boolean} immediate 是否立即执行函数,默认 true
 * @returns {function} 
 */

debounce

/**
 * @description 防抖(支持设置立即执行)
 * @param {function} fn 执行的函数
 * @param {number} wait 执行间隔时长,默认500ms
 * @param {boolean} immediate 是否立即执行函数
 * @returns {function} 
 */

deepClone

/**
 * @description 深拷贝
 * @param {object} obj 原对象
 * @returns {object} 深拷贝的新对象
 */

deepMerge

/**
 * @description JS对象深度合并
 * @param {object} target 需要拷贝的对象
 * @param {object} source 拷贝的来源对象
 * @returns {object|boolean} 深度合并后的对象或者false(入参有不是对象)
 */

equals

/**
 * @description 判断是否全等
 * @param a 变量a
 * @param b 变量b
 * @returns {boolean} 
 */

uniqueList

/**
 * @description 数组去重
 * @param {array} 原数组
 * @returns {array} 去重后的数组
 */

byIdUniqueList

/**
 * @description 对象数组去重
 * @param {array} 原数组
 * @param {string} 去重的键值,默认id
 * @returns {array} 去重后的数组
 */

twoDecimalfn

/**
 * @description 保留2位小数(金额等)
 * @param {number|string} value
 * @returns {string} 
 */

setThousandsSeparator

/**
 * @description 实现千位分隔符
 * @param {number|string} num 数字
 * @param {boolean} needPoint 是否需要保留2位小数
 * @returns {string} 
 */

digitUppercase

/**
 * @description 数字转化为大写金额
 * @param {number|string}
 * @returns {string} 
 */

priceFormat

/**
 * @description 数字格式化
 * @param {number|string} number 要格式化的数字
 * @param {number} decimals 保留几位小数,默认 0
 * @param {string} decimalPoint 小数点符号,默认 .
 * @param {string} thousandsSeparator 千分位符号,默认 ,
 * @returns {string} 格式化后的数字
 */

numberMul

/**
 * @description 小数点相乘算法(精度问题)
 * @param {number|string} arg1
 * @param {number|string} arg2
 * @returns {number} 
 */

addNum

/**
 * @description 小数点相加
 * @param {number|string} arg1
 * @param {number|string} arg2
 * @returns {number} 
 */

minusNum

/**
 * @description 小数点相减
 * @param {number|string} arg1
 * @param {number|string} arg2
 * @returns {number} 
 */

handleToFixed

/**
 * @description toFixed()保留指定小数位
 * @param {number} number 需要转化的数字
 * @param {number} d 小数点保留位数
 * @returns {string}
 */

getHidePhone

/**
 * @description 隐藏手机号
 * @param {String} content 手机号
 * @param {Number} hideLen 隐藏的长度,默认4
 * @param {String} symbol 隐藏使用的符号,默认*
 * @param {String} padStartOrEnd 默认end
 * @param {Boolean} removeNan 是否移除非数字,默认true
 * @returns {String} 隐藏后的内容
 */

randomNum

/**
 * @description 生成指定范围随机数
 * @param {number} min
 * @param {number} max
 * @returns {number} 随机数
 */

getRandomColor

/**
 * @description 随机颜色生成
 * @returns {string}
 */

intersectionArray

/**
 * @description 多数组取交集
 * @param {array} a 数组 例如:[1,2,3,4]
 * @param {array} arr 剩余数组
 * @returns {array} 交集
 */

indexOfMax

/**
 * @description 查找数组中最大值的索引
 * @param {array} arr 数组
 * @returns {number} 最大值的索引
 */

indexOfMin

/**
 * @description 查找数组中最小值的索引
 * @param {array} arr 数组
 * @returns {number} 最小值的索引
 */

transBinary

/**
 * @description 将10进制转换成n进制
 * @param {number} num 数字
 * @returns {number} n进制,默认10
 */

arrayToTree

/**
 * @description 数组转树形结构(id, parentId)
 * @param {array} array 原数组
 * @param {number} parentId 父节点
 * @returns {array} 树形结构
 */

treeToArray

/**
 * @description 树形结构转数组(children)
 * @param {array} tree 树形结构
 * @returns {array} 数组
 */

getStrCount

/**
 * @description 查找字符串中出现最多的字符和个数
 * @param {string} str
 * @returns {object} 
 */

getKebabCase

/**
 * @description 驼峰命名转换成短横线命名
 * @param {string} str
 * @returns {string} 
 */

getCamelCase

/**
 * @description 短横线命名转换成驼峰命名
 * @param {string} str
 * @returns {string} 
 */

toCDB

/**
 * @description 全角转换为半角
 * @param {string} str
 * @returns {string} 
 */

toDBC

/**
 * @description 半角转换为全角
 * @param {string} str
 * @returns {string} 
 */

getUrlParams

/**
 * @description 获取url参数值
 * @param {string} searchValue 例如: '?userId=1',默认 window.location.search
 * @param {string} name key值 例如: userId
 * @returns {string}
 */

paramsToObj

/**
 * @description url解析成键值对
 * @param {string} url
 * @returns {object} 
 */

queryParams

/**
 * @description 对象转url参数
 * @param {object} data 对象
 * @param {Boolean} isPrefix 是否自动加上"?",默认true
 * @param {string} arrayFormat 规则 indices|brackets|repeat|comma
 * @returns {string} 
 */

getObjectURL

/**
 * @description 创建 Blob URL
 * @param file 对象
 * @returns {string}
 */

base64ToFile

/**
 * @description 将base64 的图片转换成file对象上传
 * @param {string} data 图片地址
 * @returns file类型的文件
 */

compressImgFile

/**
 * @description 压缩图片文件
 * @param {File} file 图片文件
 * @param {Number} quality 压缩质量
 * @returns {Promise<File | Blob>} 压缩后的文件blob(失败返回file)
 */

getUserAgent

/**
 * @description 判断浏览器环境
 * @returns {object}
 */

getStyle

/**
 * @description 获取元素样式
 * @param {object} obj dom对象
 * @param {string} attr 属性
 * @returns {string} 属性值
 */

isLeapYear

/**
 * @description 判断闰年
 * @param {string} year 年
 * @returns {boolean}
 */

formatDate

/**
 * @description 格式化日期
 * @param {string|date|number} date 日期
 * @param {string} format 需要转化的格式('yyyy-MM-dd hh:mm:ss.S 第q季度 ww)
 * @returns {string} 格式化后的日期
 */

formatPastTime

/**
 * @description 将时间转换为 刚刚、几秒前、几分钟前、几小时前、几天前、几月前、几年前或按传入格式显示
 * @param {object} date 传入日期
 * @param {Boolean} type 默认"default",大于等于一天可选值 -|/|.|年月日|月日|年
 * @param {string} padZero 显示日期时是否补0,默认true
 * @returns {string}
 */

diffDays

/**
 * @description 两个日期相差多少天
 * @param {string} date
 * @param {string} otherDate
 * @returns {number}
 */

addDays

/**
 * @description 加减 N 天
 * @param {string} date 原日期
 * @param {string|number} value 加减的天数
 * @returns {string} 新日期
 */

getThisWeek

/**
 * @description 获取本周日期
 * @param {string} type 1 本周开始日期到结束日期, 2 本周开始日期到今天
 * @returns {array} [t1, t2]
 */

getLastWeek

/**
 * @description 获取上周开始日期到结束日期
 * @returns {array} [t1, t2]
 */

getThisMonth

/**
 * @description 获取本月日期
 * @param {string} type 1 本月开始日期到结束日期, 2 本月开始日期到今天
 * @returns {array} [t1, t2]
 */

getLastMonth

/**
 * @description 获取上月开始日期到结束日期
 * @returns {array} [t1, t2]
 */

getProperty

/**
 * @description 获取某个对象下的属性,用于通过类似'a.b.c'的形式去获取一个对象的的属性的形式
 * @param {object} obj 对象
 * @param {string} key 需要获取的属性字段
 * @returns {*}
 */

setProperty

/**
 * @description 设置对象的属性值,例如'a.b.c'的形式进行设置
 * @param {object} obj 对象
 * @param {string} key 需要设置的属性
 * @param {string} value 设置的值
 */

isEmail

/**
 * @description 验证电子邮箱格式
 * @param {string} value 电子邮箱
 * @returns {boolean}
 */

isMobile

/**
 * @description 验证用户手机格式
 * @param {string} value 手机号码
 * @returns {boolean}
 */

isLandlinePhone

/**
 * @description 是否是固定电话
 * @param {string} value 电话
 * @returns {boolean}
 */

isUrl

/**
 * @description 验证URL格式
 * @param {string} value URL
 * @returns {boolean}
 */

isWeixin

/**
 * @description 是否是微信浏览器
 * @returns {boolean}
 */

isSafari

/**
 * @description 是否是Safari浏览器
 * @returns {boolean}
 */

trimFn

/**
 * @description 去除空格
 * @param {string} str 需要去除空格的字符串
 * @param {string} pos both|left|right|all 默认both
 * @returns {string} 去除空格后的字符串
 */

isChinese

/**
 * @description 是否是中文
 * @param {string} value
 * @returns {boolean}
 */

getCharLen

/**
 * @description 获取字符个数
 * @param {string} str
 * @returns {number}
 */

isPromise

/**
 * @description 是否是promise对象
 * @param {object} value
 * @returns {boolean}
 */

padZero

/**
 * @description 补零操作(比如日期的月或日)
 * @param {string|number} value 需要补零的变量
 * @param {number} len (补零后的)总长度,默认2
 * @param {string|number} zero 0 默认是补0
 * @returns {string}
 */

isPalindrome

/**
 * @description 使用双指针验证是否是回文串
 * @param {string} str 
 * @returns {boolean}
 */

isIDCard

/**
 * @description 身份证号验证
 * @param {string} 身份证号
 * @returns {boolean}
 */

getTouchDirection

/**
 * @description 获取手指滑动方向
 * @param {number} startx
 * @param {number} starty
 * @param {number} endx
 * @param {number} endy
 * @returns {number} 0 未滑动,1 向上,2 向下,3 向左,4 向右
 */

handleExportFile

/**
 * @description 处理后端返回的文件流(接口请求设置 responseType: 'blob')
 * @param {object} res 接口返回的文件流
 * @param {string} fileNameValue 获取文件名,默认 filename= 截取
 */

toPageTop

/**
 * @description 回到页面顶部
 */

isDarkMode

/**
 * @description 是否是黑暗应用模式
 * @returns {boolean}
 */

setCliboardCopy

/**
 * @description 复制文本
 * @param {String} content 要复制的内容
 */

scrollToView

/**
 * @description 元素滚动到可视区
 * @param {object} element dom
 * @param {string} behavior auto|smooth 默认 smooth
 * @param {string} block start|center|end|nearest 默认 start
 * @param {string} inline start|center|end|nearest 默认 nearest
 */

toggleFullScreen

/**
 * @description 全屏切换
 */