@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()- 创建唯一 IDarrToLabel(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)- 获取带系统标识的 keysetSession(session, key)- 设置 localStorage 中的 sessiongetSession(key)- 获取 localStorage 中的 sessionremoveSession(key)- 移除 localStorage 中的 sessionextendSession(patch, key)- 扩展 session 中的部分属性setSessionStorage(session, key)- 设置 sessionStoragegetSessionStorage(key)- 获取 sessionStorageremoveSessionStorage(key)- 移除 sessionStorage
图片 URL 处理 (imgUrl)
getImgURL(url)- 获取通用图片 URLgetGaugeImgUrl(url)- 获取量表图片 URLdoctorHeadImg(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 参数,返回新的 URLgetUrlParams(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()- 生成短 UUIDuuidLong()- 生成长 UUID(由三个短UUID拼接)
对象属性获取 (get)
get(object, path, defaultValue)- 安全获取对象属性(类似 lodash.get)
Axios 适配器 (defaultAdapter)
defaultAdapter(options)- 防止重复请求的适配器options.cacheTime- 缓存时间(毫秒),设置为0不清除缓存,默认0options.cache- 是否支持缓存,默认falseoptions.noWarn- 是否禁用警告,默认false
注意事项
- 本包使用驼峰命名(camelCase)API
- 需要安装
date-fns作为 peerDependency - 部分函数需要在浏览器环境中使用
开发
npm install
npm run build
npm run devLicense
MIT
