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

@hhfenpm/utils

v1.0.5

Published

通用工具函数库,包含格式化、验证、缓存、Session管理、图片URL处理、设备检测、URL处理、UUID生成等常用工具

Readme

@hhfenpm/utils

通用工具函数库

安装

npm install @hhfenpm/utils

使用

import { formatDate, validatePhone, getSession } from "@hhfenpm/utils"

功能模块

格式化 (format)

  • formatDate(timestamp, formatStr) - 日期格式化
  • formatDateMinute(timestamp) - 日期时间格式化(精确到分钟)
  • formatDecimal(number, decimalPlaces, scale) - 小数转百分比(数字)
  • formatDecimalString(number, decimalPlaces, scale) - 小数转百分比(字符串)
  • formatMoney(number, decimalPlaces, scale) - 金额格式化
  • formatNumber(num) - 数字格式化(千分位)
  • createGuid() - 创建唯一 ID
  • arrToLabel(arr, props) - 数组转标签对象
  • arrToObj(arr, props) - 数组转对象
  • copyContent(content) - 复制文本到剪贴板
  • calculateHeight(itemId, containerId) - 计算高度(判断元素高度是否超过容器)
  • formatText(text, maxLength) - 文字截断(超过指定长度显示...)
  • setCursor(dom, start, end) - 设置光标位置(用于contenteditable元素)
  • sleep(time) - 同步 setTimeout(Promise版本)
  • detectZoom() - 检测浏览器缩放比例
  • findNodeOfTree(tree, id, keyMap) - 递归查找树节点
  • copyToClip(content, type) - 复制文本到剪贴板(兼容版)
  • hidePhone(phone) - 手机号脱敏
  • escapeRegExp(string) - 转义正则表达式特殊字符

验证 (validate)

  • validateFieldAsync(prop, form) - 基于 Promise 的表单字段验证(避免回调地狱)
  • validatePhone(rule, value, callback) - 手机号验证规则
  • isEmptyObj(obj) - 判断对象是否为空
  • isEqual(a, b) - 深度比较两个值是否相等(不包含引用比较)
  • isEmpty(value) - 判断值是否为空
  • getBrowserInfo() - 获取浏览器信息
  • isSupported() - 检测浏览器是否支持媒体设备(getUserMedia)
  • validateMeaninglessConsecutiveSymbols(input) - 校验无意义连续符号

缓存 (cache)

  • cache - 内存缓存对象
    • cache.set(key, data) - 设置缓存
    • cache.get(key) - 获取缓存
    • cache.clear(key) - 清除缓存
  • buildUniqueUrl(url, method, params, data) - 构建唯一的 URL 键值(用于请求缓存)

Session 管理 (session)

  • setSystemKey(key) - 设置系统标识前缀
  • getSystemKey(key) - 获取带系统标识的 key
  • setSession(session, key) - 设置 localStorage 中的 session
  • getSession(key) - 获取 localStorage 中的 session
  • removeSession(key) - 移除 localStorage 中的 session
  • extendSession(patch, key) - 扩展 session 中的部分属性
  • setSessionStorage(session, key) - 设置 sessionStorage
  • getSessionStorage(key) - 获取 sessionStorage
  • removeSessionStorage(key) - 移除 sessionStorage

图片 URL 处理 (imgUrl)

  • getImgURL(url) - 获取通用图片 URL
  • getGaugeImgUrl(url) - 获取量表图片 URL
  • doctorHeadImg(url, defaultUrl) - 获取医生头像 URL

设备检测 (device)

  • isTablet() - 检测当前设备是否为平板
  • getDeviceType() - 获取设备类型('desktop' | 'tablet' | 'mobile')
  • isTouchDevice() - 检测是否为触摸设备
  • getDeviceInfo() - 获取设备信息

URL 处理 (url)

  • queryToObj(str) - 查询字符串转对象
  • toQueryString(obj) - 对象转查询字符串
  • urlToJson(selfUrl) - URL 转 JSON 对象
  • getQueryString(name, url) - 获取 URL 中的指定参数
  • setUrl(json, originUrl) - 设置 URL 参数,返回新的 URL
  • getUrlParams(url) - 获取 URL 参数(简化版)

通用工具 (tools)

  • convertMilliseconds(ms, secondsTo2decimal) - 毫秒转时分秒
  • debounce(fn, delay) - 防抖函数
  • myDebounce(fn, delay) - 防抖函数(别名,向后兼容)
  • throttle(fn, delay) - 节流函数
  • mapStringifyReplacer(key, value) - Map 序列化替换器(用于 JSON.stringify)
  • mapParseReviver(key, value) - Map 反序列化恢复器(用于 JSON.parse)
  • getHalfYearAgoToday(reduce) - 获取 N 个月前的今天
  • fileDownloadHelper - 文件下载助手
    • fileDownloadHelper.download(blob, filename) - 下载文件
    • fileDownloadHelper.getFilenameFromHeaders(headers, defaultName) - 从响应头获取文件名
  • copyText(text) - 复制文本到剪贴板(Promise版本)

UUID 生成 (uuid)

  • uuid() - 生成短 UUID
  • uuidLong() - 生成长 UUID(由三个短UUID拼接)

对象属性获取 (get)

  • get(object, path, defaultValue) - 安全获取对象属性(类似 lodash.get)

Axios 适配器 (defaultAdapter)

  • defaultAdapter(options) - 防止重复请求的适配器
    • options.cacheTime - 缓存时间(毫秒),设置为0不清除缓存,默认0
    • options.cache - 是否支持缓存,默认false
    • options.noWarn - 是否禁用警告,默认false

注意事项

  • 本包使用驼峰命名(camelCase)API
  • 需要安装 date-fns 作为 peerDependency
  • 部分函数需要在浏览器环境中使用

开发

npm install
npm run build
npm run dev

License

MIT