react-util-tools 1.0.0

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 ADDED
@@ -0,0 +1,254 @@
1
+ # React Tools
2
+
3
+ 一个实用的 JavaScript/TypeScript 工具库集合,包含节流防抖、日期处理、设备检测、金额格式化、高精度计算等常用功能。
4
+
5
+ ## 特性
6
+
7
+ ✨ **功能丰富** - 涵盖日常开发中的常用工具函数
8
+ 🌳 **Tree Shaking** - 支持按需导入,只打包使用的代码
9
+ 📘 **TypeScript** - 完整的类型支持
10
+ 📦 **零配置** - 开箱即用
11
+ 🎯 **高质量** - 基于成熟的第三方库封装
12
+
13
+ ## 安装
14
+
15
+ ```bash
16
+ npm install @dj49846917/react-tools
17
+ ```
18
+
19
+ ## 快速开始
20
+
21
+ ```typescript
22
+ import {
23
+ throttle,
24
+ formatMoney,
25
+ formatDate,
26
+ getOS,
27
+ Decimal
28
+ } from '@dj49846917/react-tools'
29
+
30
+ // 节流函数
31
+ const handleScroll = throttle(() => {
32
+ console.log('scrolling...')
33
+ }, 1000)
34
+
35
+ // 金额格式化
36
+ const price = formatMoney(1234.56) // "¥1,234.56"
37
+
38
+ // 日期格式化
39
+ const date = formatDate(new Date(), 'yyyy-MM-dd') // "2024-11-10"
40
+
41
+ // 设备检测
42
+ const os = getOS() // "MacOS" | "Windows" | "iOS" | "Android" ...
43
+
44
+ // 高精度计算
45
+ const total = new Decimal('0.1').plus('0.2') // Decimal(0.3)
46
+ ```
47
+
48
+ ## 模块列表
49
+
50
+ ### 🎯 Throttle - 节流防抖
51
+ - `throttle()` - 节流函数
52
+ - `debounce()` - 防抖函数
53
+
54
+ [查看文档](./src/throttle/README.md)
55
+
56
+ ### 🌐 Address - URL 参数处理
57
+ - `getQueryParam()` - 获取单个参数
58
+ - `getAllQueryParams()` - 获取所有参数
59
+ - `getQueryParamAll()` - 获取同名参数数组
60
+
61
+ [查看文档](./src/address/README.md)
62
+
63
+ ### 📱 Device - 设备检测
64
+ - `getOS()` - 获取操作系统
65
+ - `getBrowser()` - 获取浏览器类型
66
+ - `getBrowserEngine()` - 获取浏览器内核
67
+ - `isMobile()` / `isTablet()` / `isDesktop()` - 设备类型判断
68
+ - `isIOS()` / `isAndroid()` / `isWeChat()` - 平台判断
69
+ - 更多...
70
+
71
+ [查看文档](./src/device/README.md)
72
+
73
+ ### 💰 Format - 格式化工具
74
+ - `formatMoney()` - 金额格式化
75
+ - `parseMoney()` - 金额反格式化
76
+ - `formatNumber()` - 数字格式化
77
+ - `formatMoneyToChinese()` - 金额转中文大写
78
+ - `formatPercent()` - 百分比格式化
79
+
80
+ [查看文档](./src/format/README.md)
81
+
82
+ ### 📅 Date - 日期处理
83
+ 基于 date-fns 封装的日期工具:
84
+ - `formatDate()` - 格式化日期
85
+ - `formatRelativeTime()` - 相对时间(如"3分钟前")
86
+ - `getDaysDiff()` - 计算天数差
87
+ - `addDaysToDate()` - 增加天数
88
+ - `getStartOfDay()` / `getEndOfDay()` - 一天的开始/结束
89
+ - 更多...
90
+
91
+ [查看文档](./src/date/README.md)
92
+
93
+ #### UTC 时区工具
94
+ - `formatUTC()` - UTC 时间格式化
95
+ - `toUTC()` / `fromUTC()` - 时区转换
96
+ - `getUTCYearStartTimestamp()` - 年份第一天时间戳
97
+ - `getUTCWeeksInYear()` - 获取一年有多少周
98
+ - `getUTCWeekStart()` / `getUTCWeekEnd()` - 获取周的开始/结束时间
99
+ - 更多...
100
+
101
+ [查看文档](./src/date/utc/README.md)
102
+
103
+ ### 🔢 Decimal - 高精度计算
104
+ 基于 decimal.js 的高精度十进制运算:
105
+ - 解决 JavaScript 浮点数精度问题
106
+ - 支持任意精度的加减乘除
107
+ - 适用于金融计算、科学计算等场景
108
+
109
+ [查看文档](./src/decimal/README.md)
110
+
111
+ ## 使用示例
112
+
113
+ ### 节流防抖
114
+
115
+ ```typescript
116
+ import { throttle, debounce } from '@dj49846917/react-tools'
117
+
118
+ // 滚动事件节流
119
+ const handleScroll = throttle(() => {
120
+ console.log('scrolling...')
121
+ }, 1000)
122
+
123
+ // 搜索输入防抖
124
+ const handleSearch = debounce((keyword: string) => {
125
+ console.log('searching:', keyword)
126
+ }, 500)
127
+ ```
128
+
129
+ ### URL 参数处理
130
+
131
+ ```typescript
132
+ import { getQueryParam, getAllQueryParams } from '@dj49846917/react-tools'
133
+
134
+ // URL: ?id=123&name=test
135
+ const id = getQueryParam('id') // "123"
136
+ const params = getAllQueryParams() // { id: "123", name: "test" }
137
+ ```
138
+
139
+ ### 设备检测
140
+
141
+ ```typescript
142
+ import { getOS, getBrowser, isMobile } from 'react-tools'
143
+
144
+ const os = getOS() // "MacOS"
145
+ const browser = getBrowser() // "Chrome"
146
+
147
+ if (isMobile()) {
148
+ // 移动端处理
149
+ }
150
+ ```
151
+
152
+ ### 金额格式化
153
+
154
+ ```typescript
155
+ import { formatMoney, parseMoney, formatMoneyToChinese } from 'react-tools'
156
+
157
+ formatMoney(1234.56) // "¥1,234.56"
158
+ formatMoney(1234.56, { symbol: '$' }) // "$1,234.56"
159
+
160
+ parseMoney('¥1,234.56') // 1234.56
161
+
162
+ formatMoneyToChinese(1234.56) // "壹仟贰佰叁拾肆元伍角陆分"
163
+ ```
164
+
165
+ ### 日期处理
166
+
167
+ ```typescript
168
+ import { formatDate, formatRelativeTime, getDaysDiff } from 'react-tools'
169
+
170
+ formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss')
171
+ // "2024-11-10 15:30:45"
172
+
173
+ formatRelativeTime(new Date(Date.now() - 5 * 60 * 1000))
174
+ // "5 分钟前"
175
+
176
+ getDaysDiff(new Date('2024-11-15'), new Date('2024-11-10'))
177
+ // 5
178
+ ```
179
+
180
+ ### UTC 时区处理
181
+
182
+ ```typescript
183
+ import {
184
+ formatUTC,
185
+ getUTCYearStartTimestamp,
186
+ getUTCWeeksInYear,
187
+ getUTCWeekStart
188
+ } from 'react-tools'
189
+
190
+ // 格式化为 UTC 时间
191
+ formatUTC(new Date()) // "2024-11-10 07:30:00"
192
+
193
+ // 获取 2024 年第一天的时间戳
194
+ getUTCYearStartTimestamp(2024) // 1704067200000
195
+
196
+ // 获取 2024 年有多少周
197
+ getUTCWeeksInYear(2024) // 52
198
+
199
+ // 获取第 10 周的开始时间
200
+ getUTCWeekStart(2024, 10) // Date 对象
201
+ ```
202
+
203
+ ### 高精度计算
204
+
205
+ ```typescript
206
+ import { Decimal } from 'react-tools'
207
+
208
+ // 解决浮点数精度问题
209
+ const result = new Decimal('0.1').plus('0.2')
210
+ console.log(result.toString()) // "0.3"
211
+
212
+ // 金额计算
213
+ const price = new Decimal('19.99')
214
+ const quantity = new Decimal('3')
215
+ const total = price.times(quantity)
216
+ console.log(total.toFixed(2)) // "59.97"
217
+ ```
218
+
219
+ ## TypeScript 支持
220
+
221
+ 完整的 TypeScript 类型支持,开箱即用。
222
+
223
+ ```typescript
224
+ import { Decimal, formatMoney, getOS } from 'react-tools'
225
+
226
+ const amount: Decimal = new Decimal('100')
227
+ const formatted: string = formatMoney(1234.56)
228
+ const os: string = getOS()
229
+ ```
230
+
231
+ ## Tree Shaking
232
+
233
+ 支持按需导入,只打包使用的代码:
234
+
235
+ ```typescript
236
+ // 只导入需要的函数
237
+ import { formatMoney, Decimal } from 'react-tools'
238
+
239
+ // 打包时只会包含 formatMoney 和 Decimal 相关代码
240
+ ```
241
+
242
+ ## 依赖
243
+
244
+ - `date-fns` - 日期处理
245
+ - `date-fns-tz` - 时区处理
246
+ - `decimal.js` - 高精度计算
247
+
248
+ ## 许可证
249
+
250
+ MIT
251
+
252
+ ## 作者
253
+
254
+ alice & bobby