@hhfenpm/utils 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,46 +11,48 @@ npm install @hhfenpm/utils
11
11
  ## 使用
12
12
 
13
13
  ```javascript
14
- // 按需导入
15
- import { format_date, validatePhone, getSession } from "@hhfenpm/utils";
14
+ // 按需导入(推荐使用驼峰命名)
15
+ import { formatDate, validatePhone, getSession } from "@hhfenpm/utils";
16
16
 
17
17
  // 或导入整个模块
18
18
  import * as utils from "@hhfenpm/utils";
19
19
  ```
20
20
 
21
+ **注意:** 本包导出的是驼峰命名(camelCase)的 API。如果您的项目需要向后兼容的下划线命名(如 `format_date`),请通过项目本地的 utils 层导入,这些层会提供向后兼容的别名。
22
+
21
23
  ## 功能模块
22
24
 
23
25
  ### 格式化 (format)
24
26
 
25
- - `format_date(timestamp, str)` - 日期格式化
27
+ - `formatDate(timestamp, formatStr)` - 日期格式化
26
28
  - `formatDateMinute(timestamp)` - 日期时间格式化(精确到分钟)
27
- - `format_decimal(number, num, scale)` - 小数转百分比(数字)
28
- - `format_decimal_string(number, num, scale)` - 小数转百分比(字符串)
29
- - `format_money(number, num, scale)` - 金额格式化
30
- - `format_number(num)` - 数字格式化(千分位)
31
- - `create_guid()` - 创建唯一 ID
32
- - `arr_label(arr, props)` - 数组转标签对象
33
- - `arr_obj(arr, props)` - 数组转对象
34
- - `copy_content(content)` - 复制到剪贴板
35
- - `calculate_height(item, container)` - 计算高度
36
- - `formatTxt(text)` - 文字截断
37
- - `set_cursor(dom, start, end)` - 设置光标位置
38
- - `sleep(time)` - 同步 setTimeout
29
+ - `formatDecimal(number, decimalPlaces, scale)` - 小数转百分比(数字)
30
+ - `formatDecimalString(number, decimalPlaces, scale)` - 小数转百分比(字符串)
31
+ - `formatMoney(number, decimalPlaces, scale)` - 金额格式化
32
+ - `formatNumber(num)` - 数字格式化(千分位)
33
+ - `createGuid()` - 创建唯一 ID
34
+ - `arrToLabel(arr, props)` - 数组转标签对象
35
+ - `arrToObj(arr, props)` - 数组转对象
36
+ - `copyContent(content)` - 复制文本到剪贴板
37
+ - `calculateHeight(itemId, containerId)` - 计算高度(判断元素高度是否超过容器)
38
+ - `formatText(text, maxLength)` - 文字截断(超过指定长度显示...)
39
+ - `setCursor(dom, start, end)` - 设置光标位置(用于contenteditable元素)
40
+ - `sleep(time)` - 同步 setTimeout(Promise版本)
39
41
  - `detectZoom()` - 检测浏览器缩放比例
40
42
  - `findNodeOfTree(tree, id, keyMap)` - 递归查找树节点
41
- - `copyToClip(content, type)` - 复制到剪贴板(兼容版)
43
+ - `copyToClip(content, type)` - 复制文本到剪贴板(兼容版)
42
44
  - `hidePhone(phone)` - 手机号脱敏
43
45
  - `escapeRegExp(string)` - 转义正则表达式特殊字符
44
46
 
45
47
  ### 验证 (validate)
46
48
 
47
- - `validateFieldAsync(prop, form)` - 基于 Promise 的表单字段验证
48
- - `validatePhone(rule, value, callback)` - 手机号验证
49
+ - `validateFieldAsync(prop, form)` - 基于 Promise 的表单字段验证(避免回调地狱)
50
+ - `validatePhone(rule, value, callback)` - 手机号验证规则
49
51
  - `isEmptyObj(obj)` - 判断对象是否为空
50
- - `isEqual(a, b)` - 深度相等比较
52
+ - `isEqual(a, b)` - 深度比较两个值是否相等(不包含引用比较)
51
53
  - `isEmpty(value)` - 判断值是否为空
52
54
  - `getBrowserInfo()` - 获取浏览器信息
53
- - `isSupported()` - 检测是否支持媒体设备
55
+ - `isSupported()` - 检测浏览器是否支持媒体设备(getUserMedia)
54
56
  - `validateMeaninglessConsecutiveSymbols(input)` - 校验无意义连续符号
55
57
 
56
58
  ### 缓存 (cache)
@@ -59,30 +61,31 @@ import * as utils from "@hhfenpm/utils";
59
61
  - `cache.set(key, data)` - 设置缓存
60
62
  - `cache.get(key)` - 获取缓存
61
63
  - `cache.clear(key)` - 清除缓存
62
- - `buildUniqueUrl(url, method, params, data)` - 构建唯一的 URL 键值
64
+ - `buildUniqueUrl(url, method, params, data)` - 构建唯一的 URL 键值(用于请求缓存)
63
65
 
64
66
  ### Session 管理 (session)
65
67
 
66
- - `set_system_key(key)` - 设置系统标识
67
- - `get_system_key(key)` - 获取带系统标识的 key
68
+ - `setSystemKey(key)` - 设置系统标识前缀
69
+ - `getSystemKey(key)` - 获取带系统标识的 key
68
70
  - `setSession(session, key)` - 设置 localStorage 中的 session
69
71
  - `getSession(key)` - 获取 localStorage 中的 session
70
72
  - `removeSession(key)` - 移除 localStorage 中的 session
71
- - `extentSession(patch, key)` - 扩展 session 中的部分属性
73
+ - `extendSession(patch, key)` - 扩展 session 中的部分属性(推荐使用)
74
+ - `extentSession(patch, key)` - 扩展 session 中的部分属性(向后兼容,拼写错误但保留)
72
75
  - `setSessionStorage(session, key)` - 设置 sessionStorage
73
76
  - `getSessionStorage(key)` - 获取 sessionStorage
74
77
  - `removeSessionStorage(key)` - 移除 sessionStorage
75
78
 
76
79
  ### 图片 URL 处理 (imgUrl)
77
80
 
78
- - `getImgURL(url)` - 获取图片 URL
81
+ - `getImgURL(url)` - 获取通用图片 URL
79
82
  - `getGaugeImgUrl(url)` - 获取量表图片 URL
80
- - `doctor_head_img(url)` - 获取医生头像 URL
83
+ - `doctorHeadImg(url, defaultUrl)` - 获取医生头像 URL
81
84
 
82
85
  ### 设备检测 (device)
83
86
 
84
- - `isTablet()` - 检测是否为平板
85
- - `getDeviceType()` - 获取设备类型
87
+ - `isTablet()` - 检测当前设备是否为平板
88
+ - `getDeviceType()` - 获取设备类型('desktop' | 'tablet' | 'mobile')
86
89
  - `isTouchDevice()` - 检测是否为触摸设备
87
90
  - `getDeviceInfo()` - 获取设备信息
88
91
 
@@ -92,25 +95,27 @@ import * as utils from "@hhfenpm/utils";
92
95
  - `toQueryString(obj)` - 对象转查询字符串
93
96
  - `urlToJson(selfUrl)` - URL 转 JSON 对象
94
97
  - `getQueryString(name, url)` - 获取 URL 中的指定参数
95
- - `setUrl(json, originUrl)` - 设置 URL 参数
96
- - `get_url_params(url)` - 获取 URL 参数(兼容旧 API)
98
+ - `setUrl(json, originUrl)` - 设置 URL 参数,返回新的 URL
99
+ - `getUrlParams(url)` - 获取 URL 参数(简化版)
97
100
 
98
101
  ### 通用工具 (tools)
99
102
 
100
103
  - `convertMilliseconds(ms, secondsTo2decimal)` - 毫秒转时分秒
101
- - `myDebounce(fn, time)` - 防抖函数
102
- - `debounce(fn, delay)` - 防抖函数(兼容版)
104
+ - `debounce(fn, delay)` - 防抖函数
105
+ - `myDebounce(fn, delay)` - 防抖函数(别名,向后兼容)
103
106
  - `throttle(fn, delay)` - 节流函数
104
- - `mapStringifyReplacer(key, value)` - Map 序列化替换器
105
- - `mapParseReviver(key, value)` - Map 反序列化恢复器
106
- - `getHalfYearAgoToday(reduce)` - 获取 N 个月前的日期
107
+ - `mapStringifyReplacer(key, value)` - Map 序列化替换器(用于 JSON.stringify)
108
+ - `mapParseReviver(key, value)` - Map 反序列化恢复器(用于 JSON.parse)
109
+ - `getHalfYearAgoToday(reduce)` - 获取 N 个月前的今天
107
110
  - `fileDownloadHelper` - 文件下载助手
108
- - `copyText(text)` - 复制文本到剪贴板
111
+ - `fileDownloadHelper.download(blob, filename)` - 下载文件
112
+ - `fileDownloadHelper.getFilenameFromHeaders(headers, defaultName)` - 从响应头获取文件名
113
+ - `copyText(text)` - 复制文本到剪贴板(Promise版本)
109
114
 
110
115
  ### UUID 生成 (uuid)
111
116
 
112
117
  - `uuid()` - 生成短 UUID
113
- - `uuidLong()` - 生成长 UUID
118
+ - `uuidLong()` - 生成长 UUID(由三个短UUID拼接)
114
119
 
115
120
  ### 对象属性获取 (get)
116
121
 
@@ -119,12 +124,28 @@ import * as utils from "@hhfenpm/utils";
119
124
  ### Axios 适配器 (defaultAdapter)
120
125
 
121
126
  - `defaultAdapter(options)` - 防止重复请求的适配器
127
+ - `options.cacheTime` - 缓存时间(毫秒),设置为0不清除缓存,默认0
128
+ - `options.cache` - 是否支持缓存,默认false
129
+ - `options.noWarn` - 是否禁用警告,默认false
130
+
131
+ ## 向后兼容说明
132
+
133
+ 本包导出的是标准的驼峰命名(camelCase)API。如果您需要向后兼容的下划线命名(如 `format_date`、`format_decimal` 等),请通过项目本地的 utils 层导入:
134
+
135
+ ```javascript
136
+ // ✅ 推荐:通过项目 utils 层导入(提供向后兼容)
137
+ import { format_date, format_decimal } from '@/utils/format'
138
+
139
+ // ⚠️ 不推荐:直接导入 npm 包(只有驼峰命名)
140
+ import { formatDate, formatDecimal } from '@hhfenpm/utils'
141
+ ```
122
142
 
123
143
  ## 注意事项
124
144
 
125
- 1. **API 兼容性**:所有导出的函数名都保持与原业务代码中的命名一致(使用下划线命名),确保兼容旧 API。
145
+ 1. **API 命名**:本包使用标准的驼峰命名(camelCase)。向后兼容的下划线命名由项目本地的 utils 层提供。
126
146
  2. **依赖要求**:需要安装 `date-fns` 作为 peerDependency。
127
147
  3. **浏览器环境**:部分函数需要在浏览器环境中使用,在 Node.js 环境中会返回默认值或警告。
148
+ 4. **拼写说明**:`extentSession` 是历史遗留的拼写错误,建议使用 `extendSession`,但 `extentSession` 仍然可用以保持向后兼容。
128
149
 
129
150
  ## 开发
130
151