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

utils-xhh

v1.0.20

Published

前端通用工具方法

Downloads

53

Readme

Install

npm i utils-xhh -S

Usage

按需导入

例如:

import { trimFn } from 'utils-xhh'

API

throttle

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

debounce

/**
 * @description 防抖(支持设置立即执行)
 * @param {function} fn 执行的函数
 * @param {number} wait 执行间隔时长
 * @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} 去重后的数组
 */

twoDecimalfn

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

setThousandsSeparator

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

priceFormat

/**
 * @description 数字格式化
 * @param {number|string} number 要格式化的数字
 * @param {number} decimals 保留几位小数
 * @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}
*/

getStrCount

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

getUrlParams

/**
 * @description 获取url参数值
 * @param {string} name key值
 * @returns {string} 
 */

paramsToObj

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

queryParams

/**
 * @description 对象转url参数
 * @param {object} data 对象
 * @param {Boolean} isPrefix 是否自动加上"?"
 * @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类型的文件
 */

getUserAgent

/**
 * @description 判断浏览器环境(isTablet,isPhone,isAndroid,isMobile,isPc,isChrome)
 * @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 需要转化的格式
 * @returns {string} 格式化后的日期
 */

diffDays

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

addDays

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

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}
 */

isPromise

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

padZero

/**
 * @description 补零操作(比如日期的月或日)
 * @param {string} value 需要补零的变量
 * @param {string} num (补零后的)总长度
 * @returns {string}
 */

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}
*/

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
*/