react-util-tools 1.0.24 → 1.0.26
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 +128 -3
- package/dist/index.cjs +437 -2
- package/dist/index.d.cts +68 -1
- package/dist/index.d.ts +68 -1
- package/dist/index.js +373 -1
- package/package.json +8 -3
- package/src/excel/README.md +388 -0
- package/src/excel/example.md +425 -0
- package/src/excel/index.ts +245 -0
- package/src/index.ts +78 -0
- package/src/string/README.md +441 -0
- package/src/string/index.ts +527 -0
package/README.md
CHANGED
|
@@ -60,6 +60,16 @@ const total = new Decimal('0.1').plus('0.2') // Decimal(0.3)
|
|
|
60
60
|
|
|
61
61
|
[查看文档](./src/address/README.md)
|
|
62
62
|
|
|
63
|
+
### 🍪 Cookie - Cookie 操作
|
|
64
|
+
- `setCookie()` - 设置 Cookie
|
|
65
|
+
- `getCookie()` - 获取 Cookie
|
|
66
|
+
- `removeCookie()` - 删除 Cookie
|
|
67
|
+
- `hasCookie()` - 检查 Cookie 是否存在
|
|
68
|
+
- `getAllCookies()` - 获取所有 Cookie
|
|
69
|
+
- `clearAllCookies()` - 清除所有 Cookie
|
|
70
|
+
|
|
71
|
+
[查看文档](./src/cookie/README.md)
|
|
72
|
+
|
|
63
73
|
### 📱 Device - 设备检测
|
|
64
74
|
- `getOS()` - 获取操作系统
|
|
65
75
|
- `getBrowser()` - 获取浏览器类型
|
|
@@ -76,6 +86,8 @@ const total = new Decimal('0.1').plus('0.2') // Decimal(0.3)
|
|
|
76
86
|
- `formatNumber()` - 数字格式化
|
|
77
87
|
- `formatMoneyToChinese()` - 金额转中文大写
|
|
78
88
|
- `formatPercent()` - 百分比格式化
|
|
89
|
+
- `maskEmail()` - 邮箱脱敏
|
|
90
|
+
- `unmaskEmail()` - 邮箱解脱敏
|
|
79
91
|
|
|
80
92
|
[查看文档](./src/format/README.md)
|
|
81
93
|
|
|
@@ -108,6 +120,44 @@ const total = new Decimal('0.1').plus('0.2') // Decimal(0.3)
|
|
|
108
120
|
|
|
109
121
|
[查看文档](./src/decimal/README.md)
|
|
110
122
|
|
|
123
|
+
#### Decimal Utils - 高精度计算工具函数
|
|
124
|
+
- `add()` - 加法运算
|
|
125
|
+
- `subtract()` - 减法运算
|
|
126
|
+
- `multiply()` - 乘法运算
|
|
127
|
+
- `divide()` - 除法运算
|
|
128
|
+
- `equals()` - 判断相等
|
|
129
|
+
- `greaterThan()` / `lessThan()` - 大小比较
|
|
130
|
+
- `round()` / `ceil()` / `floor()` - 取整
|
|
131
|
+
- `abs()` / `negate()` - 绝对值/取反
|
|
132
|
+
|
|
133
|
+
[查看文档](./src/decimal/utils/README.md)
|
|
134
|
+
|
|
135
|
+
### 📊 Excel - Excel 文件处理
|
|
136
|
+
基于 SheetJS (xlsx) 的 Excel 文件处理工具:
|
|
137
|
+
- `XLSX` - 完整的 SheetJS 对象
|
|
138
|
+
- `readExcelFile()` - 读取 Excel 文件
|
|
139
|
+
- `exportJSONToExcel()` - 导出 JSON 为 Excel
|
|
140
|
+
- `workbookToJSON()` - WorkBook 转 JSON
|
|
141
|
+
- `jsonToWorkbook()` - JSON 转 WorkBook
|
|
142
|
+
- `readExcelToJSON()` - 直接读取为 JSON
|
|
143
|
+
- 支持多种格式(.xlsx, .xls, .csv 等)
|
|
144
|
+
- 完整保留 SheetJS 所有功能
|
|
145
|
+
|
|
146
|
+
[查看文档](./src/excel/README.md)
|
|
147
|
+
|
|
148
|
+
### 🔤 String - 字符串工具
|
|
149
|
+
强大的字符串处理工具集合:
|
|
150
|
+
- `camelCase()` / `snakeCase()` / `kebabCase()` - 命名转换
|
|
151
|
+
- `truncate()` / `padStart()` / `padEnd()` - 字符串操作
|
|
152
|
+
- `maskPhone()` / `maskIdCard()` / `maskBankCard()` - 数据脱敏
|
|
153
|
+
- `isValidPhone()` / `isValidEmail()` / `isValidUrl()` - 格式验证
|
|
154
|
+
- `randomString()` / `uuid()` - 随机生成
|
|
155
|
+
- `toBase64()` / `fromBase64()` - Base64 编解码
|
|
156
|
+
- `escapeHtml()` / `stripHtml()` - HTML 处理
|
|
157
|
+
- 更多...
|
|
158
|
+
|
|
159
|
+
[查看文档](./src/string/README.md)
|
|
160
|
+
|
|
111
161
|
## 使用示例
|
|
112
162
|
|
|
113
163
|
### 节流防抖
|
|
@@ -136,6 +186,26 @@ const id = getQueryParam('id') // "123"
|
|
|
136
186
|
const params = getAllQueryParams() // { id: "123", name: "test" }
|
|
137
187
|
```
|
|
138
188
|
|
|
189
|
+
### Cookie 操作
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
import { setCookie, getCookie, removeCookie } from '@dj49846917/react-tools'
|
|
193
|
+
|
|
194
|
+
// 设置 Cookie
|
|
195
|
+
setCookie('token', 'abc123', {
|
|
196
|
+
expires: 7 * 24 * 60 * 60, // 7天(秒)
|
|
197
|
+
path: '/',
|
|
198
|
+
secure: true,
|
|
199
|
+
sameSite: 'Strict'
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
// 获取 Cookie
|
|
203
|
+
const token = getCookie('token') // "abc123"
|
|
204
|
+
|
|
205
|
+
// 删除 Cookie
|
|
206
|
+
removeCookie('token')
|
|
207
|
+
```
|
|
208
|
+
|
|
139
209
|
### 设备检测
|
|
140
210
|
|
|
141
211
|
```typescript
|
|
@@ -152,7 +222,7 @@ if (isMobile()) {
|
|
|
152
222
|
### 金额格式化
|
|
153
223
|
|
|
154
224
|
```typescript
|
|
155
|
-
import { formatMoney, parseMoney, formatMoneyToChinese } from 'react-tools'
|
|
225
|
+
import { formatMoney, parseMoney, formatMoneyToChinese, maskEmail } from 'react-tools'
|
|
156
226
|
|
|
157
227
|
formatMoney(1234.56) // "¥1,234.56"
|
|
158
228
|
formatMoney(1234.56, { symbol: '$' }) // "$1,234.56"
|
|
@@ -160,6 +230,9 @@ formatMoney(1234.56, { symbol: '$' }) // "$1,234.56"
|
|
|
160
230
|
parseMoney('¥1,234.56') // 1234.56
|
|
161
231
|
|
|
162
232
|
formatMoneyToChinese(1234.56) // "壹仟贰佰叁拾肆元伍角陆分"
|
|
233
|
+
|
|
234
|
+
// 邮箱脱敏
|
|
235
|
+
maskEmail('dj49846917@proton.me') // "dj4***@proton.me"
|
|
163
236
|
```
|
|
164
237
|
|
|
165
238
|
### 日期处理
|
|
@@ -203,9 +276,9 @@ getUTCWeekStart(2024, 10) // Date 对象
|
|
|
203
276
|
### 高精度计算
|
|
204
277
|
|
|
205
278
|
```typescript
|
|
206
|
-
import { Decimal } from 'react-tools'
|
|
279
|
+
import { Decimal, add, multiply, divide } from 'react-tools'
|
|
207
280
|
|
|
208
|
-
//
|
|
281
|
+
// 使用 Decimal 类
|
|
209
282
|
const result = new Decimal('0.1').plus('0.2')
|
|
210
283
|
console.log(result.toString()) // "0.3"
|
|
211
284
|
|
|
@@ -214,6 +287,57 @@ const price = new Decimal('19.99')
|
|
|
214
287
|
const quantity = new Decimal('3')
|
|
215
288
|
const total = price.times(quantity)
|
|
216
289
|
console.log(total.toFixed(2)) // "59.97"
|
|
290
|
+
|
|
291
|
+
// 使用工具函数(返回 number 类型)
|
|
292
|
+
add(0.1, 0.2) // 0.3
|
|
293
|
+
multiply(19.99, 3) // 59.97
|
|
294
|
+
divide(100, 3) // 33.333...
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### Excel 文件处理
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
import { readExcelToJSON, exportJSONToExcel } from 'react-tools'
|
|
301
|
+
|
|
302
|
+
// 读取 Excel 文件
|
|
303
|
+
async function handleFileUpload(file: File) {
|
|
304
|
+
const data = await readExcelToJSON(file)
|
|
305
|
+
console.log(data) // [{ name: '张三', age: 25 }, ...]
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// 导出 JSON 为 Excel
|
|
309
|
+
function exportData() {
|
|
310
|
+
const data = [
|
|
311
|
+
{ name: '张三', age: 25, city: '北京' },
|
|
312
|
+
{ name: '李四', age: 30, city: '上海' }
|
|
313
|
+
]
|
|
314
|
+
exportJSONToExcel(data, 'users.xlsx', 'UserList')
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### 字符串处理
|
|
319
|
+
|
|
320
|
+
```typescript
|
|
321
|
+
import {
|
|
322
|
+
camelCase,
|
|
323
|
+
maskPhone,
|
|
324
|
+
isValidEmail,
|
|
325
|
+
randomString,
|
|
326
|
+
uuid
|
|
327
|
+
} from 'react-tools'
|
|
328
|
+
|
|
329
|
+
// 命名转换
|
|
330
|
+
camelCase('hello-world') // 'helloWorld'
|
|
331
|
+
|
|
332
|
+
// 数据脱敏
|
|
333
|
+
maskPhone('13812345678') // '138****5678'
|
|
334
|
+
|
|
335
|
+
// 格式验证
|
|
336
|
+
isValidEmail('test@example.com') // true
|
|
337
|
+
|
|
338
|
+
// 随机生成
|
|
339
|
+
randomString(8) // 'aB3xY9Zk'
|
|
340
|
+
uuid() // '550e8400-e29b-41d4-a716-446655440000'
|
|
217
341
|
```
|
|
218
342
|
|
|
219
343
|
## TypeScript 支持
|
|
@@ -244,6 +368,7 @@ import { formatMoney, Decimal } from 'react-tools'
|
|
|
244
368
|
- `date-fns` - 日期处理
|
|
245
369
|
- `date-fns-tz` - 时区处理
|
|
246
370
|
- `decimal.js` - 高精度计算
|
|
371
|
+
- `xlsx` - Excel 文件处理
|
|
247
372
|
|
|
248
373
|
## 许可证
|
|
249
374
|
|