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.
@@ -0,0 +1,474 @@
1
+ # Decimal Utils - 高精度计算工具函数
2
+
3
+ 基于 Decimal.js 封装的常用计算函数,返回 number 类型,自动处理异常。
4
+
5
+ ## 特点
6
+
7
+ ✅ **安全可靠** - 自动处理异常,无效输入返回 0
8
+ ✅ **简单易用** - 直接返回 number 类型,无需手动转换
9
+ ✅ **高精度** - 基于 Decimal.js,解决浮点数精度问题
10
+ ✅ **类型友好** - 支持 number、string、Decimal 类型输入
11
+
12
+ ## 使用方法
13
+
14
+ ### 导入
15
+
16
+ ```typescript
17
+ import {
18
+ add,
19
+ subtract,
20
+ multiply,
21
+ divide,
22
+ equals,
23
+ greaterThan,
24
+ lessThan,
25
+ greaterThanOrEqual,
26
+ lessThanOrEqual,
27
+ round,
28
+ ceil,
29
+ floor,
30
+ abs,
31
+ negate
32
+ } from 'your-package-name'
33
+ ```
34
+
35
+ ## API
36
+
37
+ ### add - 加法
38
+
39
+ 两个数相加。
40
+
41
+ **参数:**
42
+ - `a`: 第一个数(number | string | Decimal)
43
+ - `b`: 第二个数(number | string | Decimal)
44
+
45
+ **返回:** number
46
+
47
+ **示例:**
48
+
49
+ ```typescript
50
+ add(0.1, 0.2) // 0.3
51
+ add('10.5', '20.3') // 30.8
52
+ add(100, 50) // 150
53
+
54
+ // 异常处理
55
+ add('invalid', 10) // 0
56
+ add(NaN, 10) // 0
57
+ ```
58
+
59
+ ### subtract - 减法
60
+
61
+ 两个数相减。
62
+
63
+ **参数:**
64
+ - `a`: 被减数(number | string | Decimal)
65
+ - `b`: 减数(number | string | Decimal)
66
+
67
+ **返回:** number
68
+
69
+ **示例:**
70
+
71
+ ```typescript
72
+ subtract(0.3, 0.1) // 0.2
73
+ subtract('100', '30') // 70
74
+ subtract(50, 20) // 30
75
+
76
+ // 异常处理
77
+ subtract('invalid', 10) // 0
78
+ ```
79
+
80
+ ### multiply - 乘法
81
+
82
+ 两个数相乘。
83
+
84
+ **参数:**
85
+ - `a`: 第一个数(number | string | Decimal)
86
+ - `b`: 第二个数(number | string | Decimal)
87
+
88
+ **返回:** number
89
+
90
+ **示例:**
91
+
92
+ ```typescript
93
+ multiply(0.1, 0.2) // 0.02
94
+ multiply('10.5', '2') // 21
95
+ multiply(5, 3) // 15
96
+
97
+ // 异常处理
98
+ multiply('invalid', 10) // 0
99
+ ```
100
+
101
+ ### divide - 除法
102
+
103
+ 两个数相除。
104
+
105
+ **参数:**
106
+ - `a`: 被除数(number | string | Decimal)
107
+ - `b`: 除数(number | string | Decimal)
108
+
109
+ **返回:** number(除数为 0 时返回 0)
110
+
111
+ **示例:**
112
+
113
+ ```typescript
114
+ divide(0.3, 0.1) // 3
115
+ divide('100', '4') // 25
116
+ divide(10, 2) // 5
117
+
118
+ // 除数为 0
119
+ divide(10, 0) // 0
120
+
121
+ // 异常处理
122
+ divide('invalid', 10) // 0
123
+ ```
124
+
125
+ ### equals - 判断相等
126
+
127
+ 判断两个数是否相等。
128
+
129
+ **参数:**
130
+ - `a`: 第一个数(number | string | Decimal)
131
+ - `b`: 第二个数(number | string | Decimal)
132
+
133
+ **返回:** boolean
134
+
135
+ **示例:**
136
+
137
+ ```typescript
138
+ equals(0.1 + 0.2, 0.3) // true(解决了浮点数精度问题)
139
+ equals('10.5', 10.5) // true
140
+ equals(100, 100) // true
141
+ equals(100, 200) // false
142
+
143
+ // 异常处理
144
+ equals('invalid', 10) // false
145
+ ```
146
+
147
+ ### greaterThan - 大于
148
+
149
+ 判断 a 是否大于 b。
150
+
151
+ **示例:**
152
+
153
+ ```typescript
154
+ greaterThan(10, 5) // true
155
+ greaterThan(5, 10) // false
156
+ greaterThan('10.5', '10.4') // true
157
+ ```
158
+
159
+ ### lessThan - 小于
160
+
161
+ 判断 a 是否小于 b。
162
+
163
+ **示例:**
164
+
165
+ ```typescript
166
+ lessThan(5, 10) // true
167
+ lessThan(10, 5) // false
168
+ lessThan('10.4', '10.5') // true
169
+ ```
170
+
171
+ ### greaterThanOrEqual - 大于等于
172
+
173
+ 判断 a 是否大于等于 b。
174
+
175
+ **示例:**
176
+
177
+ ```typescript
178
+ greaterThanOrEqual(10, 10) // true
179
+ greaterThanOrEqual(10, 5) // true
180
+ greaterThanOrEqual(5, 10) // false
181
+ ```
182
+
183
+ ### lessThanOrEqual - 小于等于
184
+
185
+ 判断 a 是否小于等于 b。
186
+
187
+ **示例:**
188
+
189
+ ```typescript
190
+ lessThanOrEqual(10, 10) // true
191
+ lessThanOrEqual(5, 10) // true
192
+ lessThanOrEqual(10, 5) // false
193
+ ```
194
+
195
+ ### round - 四舍五入
196
+
197
+ 四舍五入到指定小数位。
198
+
199
+ **参数:**
200
+ - `value`: 数值(number | string | Decimal)
201
+ - `decimalPlaces`: 小数位数,默认 2
202
+
203
+ **返回:** number
204
+
205
+ **示例:**
206
+
207
+ ```typescript
208
+ round(1.2345) // 1.23
209
+ round(1.2345, 3) // 1.235
210
+ round(1.235, 2) // 1.24
211
+ round('10.567', 1) // 10.6
212
+
213
+ // 异常处理
214
+ round('invalid') // 0
215
+ ```
216
+
217
+ ### ceil - 向上取整
218
+
219
+ 向上取整到指定小数位。
220
+
221
+ **参数:**
222
+ - `value`: 数值(number | string | Decimal)
223
+ - `decimalPlaces`: 小数位数,默认 2
224
+
225
+ **返回:** number
226
+
227
+ **示例:**
228
+
229
+ ```typescript
230
+ ceil(1.231) // 1.24
231
+ ceil(1.231, 1) // 1.3
232
+ ceil('10.001', 0) // 11
233
+ ```
234
+
235
+ ### floor - 向下取整
236
+
237
+ 向下取整到指定小数位。
238
+
239
+ **参数:**
240
+ - `value`: 数值(number | string | Decimal)
241
+ - `decimalPlaces`: 小数位数,默认 2
242
+
243
+ **返回:** number
244
+
245
+ **示例:**
246
+
247
+ ```typescript
248
+ floor(1.239) // 1.23
249
+ floor(1.239, 1) // 1.2
250
+ floor('10.999', 0) // 10
251
+ ```
252
+
253
+ ### abs - 绝对值
254
+
255
+ 取绝对值。
256
+
257
+ **参数:**
258
+ - `value`: 数值(number | string | Decimal)
259
+
260
+ **返回:** number
261
+
262
+ **示例:**
263
+
264
+ ```typescript
265
+ abs(-10) // 10
266
+ abs(10) // 10
267
+ abs('-5.5') // 5.5
268
+ ```
269
+
270
+ ### negate - 取反
271
+
272
+ 取反(正数变负数,负数变正数)。
273
+
274
+ **参数:**
275
+ - `value`: 数值(number | string | Decimal)
276
+
277
+ **返回:** number
278
+
279
+ **示例:**
280
+
281
+ ```typescript
282
+ negate(10) // -10
283
+ negate(-10) // 10
284
+ negate('5.5') // -5.5
285
+ ```
286
+
287
+ ## 实际应用场景
288
+
289
+ ### 商品价格计算
290
+
291
+ ```typescript
292
+ import { add, multiply, subtract } from 'your-package-name'
293
+
294
+ const price = 19.99
295
+ const quantity = 3
296
+ const discount = 5
297
+
298
+ // 小计
299
+ const subtotal = multiply(price, quantity) // 59.97
300
+
301
+ // 折扣后价格
302
+ const total = subtract(subtotal, discount) // 54.97
303
+
304
+ console.log(`总价: ${total}`)
305
+ ```
306
+
307
+ ### 购物车总价
308
+
309
+ ```typescript
310
+ import { add, multiply } from 'your-package-name'
311
+
312
+ const items = [
313
+ { price: 19.99, quantity: 2 },
314
+ { price: 29.99, quantity: 1 },
315
+ { price: 9.99, quantity: 3 }
316
+ ]
317
+
318
+ let total = 0
319
+ items.forEach(item => {
320
+ const itemTotal = multiply(item.price, item.quantity)
321
+ total = add(total, itemTotal)
322
+ })
323
+
324
+ console.log(`购物车总价: ${total}`) // 99.94
325
+ ```
326
+
327
+ ### 税费计算
328
+
329
+ ```typescript
330
+ import { multiply, add } from 'your-package-name'
331
+
332
+ const amount = 100
333
+ const taxRate = 0.13 // 13% 税率
334
+
335
+ // 计算税额
336
+ const tax = multiply(amount, taxRate) // 13
337
+
338
+ // 含税总额
339
+ const totalWithTax = add(amount, tax) // 113
340
+
341
+ console.log(`含税总额: ${totalWithTax}`)
342
+ ```
343
+
344
+ ### 折扣计算
345
+
346
+ ```typescript
347
+ import { multiply, subtract } from 'your-package-name'
348
+
349
+ const originalPrice = 199.99
350
+ const discountRate = 0.2 // 20% 折扣
351
+
352
+ // 折扣金额
353
+ const discountAmount = multiply(originalPrice, discountRate) // 40
354
+
355
+ // 折后价
356
+ const finalPrice = subtract(originalPrice, discountAmount) // 159.99
357
+
358
+ console.log(`折后价: ${finalPrice}`)
359
+ ```
360
+
361
+ ### 平均值计算
362
+
363
+ ```typescript
364
+ import { add, divide } from 'your-package-name'
365
+
366
+ const scores = [85.5, 92.3, 78.9, 88.7, 95.2]
367
+
368
+ // 求和
369
+ let sum = 0
370
+ scores.forEach(score => {
371
+ sum = add(sum, score)
372
+ })
373
+
374
+ // 平均值
375
+ const average = divide(sum, scores.length) // 88.12
376
+
377
+ console.log(`平均分: ${average}`)
378
+ ```
379
+
380
+ ### 价格比较
381
+
382
+ ```typescript
383
+ import { greaterThan, lessThan, equals } from 'your-package-name'
384
+
385
+ const price1 = 19.99
386
+ const price2 = 20.00
387
+
388
+ if (lessThan(price1, price2)) {
389
+ console.log('价格1更便宜')
390
+ }
391
+
392
+ if (equals(price1, price2)) {
393
+ console.log('价格相同')
394
+ }
395
+
396
+ if (greaterThan(price1, price2)) {
397
+ console.log('价格1更贵')
398
+ }
399
+ ```
400
+
401
+ ### 金额四舍五入
402
+
403
+ ```typescript
404
+ import { round, ceil, floor } from 'your-package-name'
405
+
406
+ const amount = 123.456
407
+
408
+ // 四舍五入到 2 位小数
409
+ const rounded = round(amount) // 123.46
410
+
411
+ // 向上取整
412
+ const ceiledAmount = ceil(amount) // 123.46
413
+
414
+ // 向下取整
415
+ const flooredAmount = floor(amount) // 123.45
416
+
417
+ console.log(`四舍五入: ${rounded}`)
418
+ console.log(`向上取整: ${ceiledAmount}`)
419
+ console.log(`向下取整: ${flooredAmount}`)
420
+ ```
421
+
422
+ ### 百分比计算
423
+
424
+ ```typescript
425
+ import { divide, multiply } from 'your-package-name'
426
+
427
+ const total = 500
428
+ const part = 125
429
+
430
+ // 计算百分比
431
+ const percentage = multiply(divide(part, total), 100) // 25
432
+
433
+ console.log(`占比: ${percentage}%`)
434
+
435
+ // 从百分比计算数值
436
+ const percent = 25
437
+ const value = divide(multiply(total, percent), 100) // 125
438
+
439
+ console.log(`25% 的值: ${value}`)
440
+ ```
441
+
442
+ ## TypeScript 支持
443
+
444
+ 完整的 TypeScript 类型支持。
445
+
446
+ ```typescript
447
+ import { add, multiply, equals } from 'your-package-name'
448
+
449
+ const sum: number = add(10, 20)
450
+ const product: number = multiply(5, 3)
451
+ const isEqual: boolean = equals(10, 10)
452
+ ```
453
+
454
+ ## 注意事项
455
+
456
+ - 所有函数都会自动处理异常,无效输入返回 0(比较函数返回 false)
457
+ - 除法运算中,除数为 0 时返回 0
458
+ - 推荐使用字符串传入数值,避免浮点数精度问题
459
+ - 所有函数返回 number 类型,方便直接使用
460
+ - 如需更复杂的操作,请使用 Decimal 类
461
+
462
+ ## 与 Decimal 类的区别
463
+
464
+ | 特性 | Decimal Utils | Decimal 类 |
465
+ |------|--------------|-----------|
466
+ | 返回类型 | number | Decimal |
467
+ | 异常处理 | 自动处理,返回 0 | 需要手动处理 |
468
+ | 使用场景 | 简单计算 | 复杂计算、链式调用 |
469
+ | 易用性 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ |
470
+ | 功能丰富度 | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
471
+
472
+ **建议:**
473
+ - 简单的加减乘除、比较操作 → 使用 Decimal Utils
474
+ - 复杂的链式计算、需要保持精度 → 使用 Decimal 类
@@ -0,0 +1,244 @@
1
+ import Decimal from 'decimal.js'
2
+
3
+ /**
4
+ * 安全地创建 Decimal 实例
5
+ * @param value 数值
6
+ * @returns Decimal 实例,如果无效则返回 Decimal(0)
7
+ */
8
+ function safeDecimal(value: number | string | Decimal): Decimal {
9
+ try {
10
+ if (value instanceof Decimal) {
11
+ return value
12
+ }
13
+ const decimal = new Decimal(value)
14
+ if (decimal.isNaN()) {
15
+ return new Decimal(0)
16
+ }
17
+ return decimal
18
+ } catch {
19
+ return new Decimal(0)
20
+ }
21
+ }
22
+
23
+ /**
24
+ * 加法运算
25
+ * @param a 第一个数
26
+ * @param b 第二个数
27
+ * @returns 相加结果(number 类型)
28
+ */
29
+ export function add(a: number | string | Decimal, b: number | string | Decimal): number {
30
+ try {
31
+ const decimalA = safeDecimal(a)
32
+ const decimalB = safeDecimal(b)
33
+ return decimalA.plus(decimalB).toNumber()
34
+ } catch {
35
+ return 0
36
+ }
37
+ }
38
+
39
+ /**
40
+ * 减法运算
41
+ * @param a 被减数
42
+ * @param b 减数
43
+ * @returns 相减结果(number 类型)
44
+ */
45
+ export function subtract(a: number | string | Decimal, b: number | string | Decimal): number {
46
+ try {
47
+ const decimalA = safeDecimal(a)
48
+ const decimalB = safeDecimal(b)
49
+ return decimalA.minus(decimalB).toNumber()
50
+ } catch {
51
+ return 0
52
+ }
53
+ }
54
+
55
+ /**
56
+ * 乘法运算
57
+ * @param a 第一个数
58
+ * @param b 第二个数
59
+ * @returns 相乘结果(number 类型)
60
+ */
61
+ export function multiply(a: number | string | Decimal, b: number | string | Decimal): number {
62
+ try {
63
+ const decimalA = safeDecimal(a)
64
+ const decimalB = safeDecimal(b)
65
+ return decimalA.times(decimalB).toNumber()
66
+ } catch {
67
+ return 0
68
+ }
69
+ }
70
+
71
+ /**
72
+ * 除法运算
73
+ * @param a 被除数
74
+ * @param b 除数
75
+ * @returns 相除结果(number 类型),除数为 0 时返回 0
76
+ */
77
+ export function divide(a: number | string | Decimal, b: number | string | Decimal): number {
78
+ try {
79
+ const decimalA = safeDecimal(a)
80
+ const decimalB = safeDecimal(b)
81
+
82
+ // 除数为 0 时返回 0
83
+ if (decimalB.isZero()) {
84
+ return 0
85
+ }
86
+
87
+ return decimalA.dividedBy(decimalB).toNumber()
88
+ } catch {
89
+ return 0
90
+ }
91
+ }
92
+
93
+ /**
94
+ * 判断两个数是否相等
95
+ * @param a 第一个数
96
+ * @param b 第二个数
97
+ * @returns 是否相等
98
+ */
99
+ export function equals(a: number | string | Decimal, b: number | string | Decimal): boolean {
100
+ try {
101
+ const decimalA = safeDecimal(a)
102
+ const decimalB = safeDecimal(b)
103
+ return decimalA.equals(decimalB)
104
+ } catch {
105
+ return false
106
+ }
107
+ }
108
+
109
+ /**
110
+ * 判断 a 是否大于 b
111
+ * @param a 第一个数
112
+ * @param b 第二个数
113
+ * @returns a > b
114
+ */
115
+ export function greaterThan(a: number | string | Decimal, b: number | string | Decimal): boolean {
116
+ try {
117
+ const decimalA = safeDecimal(a)
118
+ const decimalB = safeDecimal(b)
119
+ return decimalA.greaterThan(decimalB)
120
+ } catch {
121
+ return false
122
+ }
123
+ }
124
+
125
+ /**
126
+ * 判断 a 是否小于 b
127
+ * @param a 第一个数
128
+ * @param b 第二个数
129
+ * @returns a < b
130
+ */
131
+ export function lessThan(a: number | string | Decimal, b: number | string | Decimal): boolean {
132
+ try {
133
+ const decimalA = safeDecimal(a)
134
+ const decimalB = safeDecimal(b)
135
+ return decimalA.lessThan(decimalB)
136
+ } catch {
137
+ return false
138
+ }
139
+ }
140
+
141
+ /**
142
+ * 判断 a 是否大于等于 b
143
+ * @param a 第一个数
144
+ * @param b 第二个数
145
+ * @returns a >= b
146
+ */
147
+ export function greaterThanOrEqual(a: number | string | Decimal, b: number | string | Decimal): boolean {
148
+ try {
149
+ const decimalA = safeDecimal(a)
150
+ const decimalB = safeDecimal(b)
151
+ return decimalA.greaterThanOrEqualTo(decimalB)
152
+ } catch {
153
+ return false
154
+ }
155
+ }
156
+
157
+ /**
158
+ * 判断 a 是否小于等于 b
159
+ * @param a 第一个数
160
+ * @param b 第二个数
161
+ * @returns a <= b
162
+ */
163
+ export function lessThanOrEqual(a: number | string | Decimal, b: number | string | Decimal): boolean {
164
+ try {
165
+ const decimalA = safeDecimal(a)
166
+ const decimalB = safeDecimal(b)
167
+ return decimalA.lessThanOrEqualTo(decimalB)
168
+ } catch {
169
+ return false
170
+ }
171
+ }
172
+
173
+ /**
174
+ * 四舍五入到指定小数位
175
+ * @param value 数值
176
+ * @param decimalPlaces 小数位数,默认 2
177
+ * @returns 四舍五入后的结果(number 类型)
178
+ */
179
+ export function round(value: number | string | Decimal, decimalPlaces = 2): number {
180
+ try {
181
+ const decimal = safeDecimal(value)
182
+ return decimal.toDecimalPlaces(decimalPlaces).toNumber()
183
+ } catch {
184
+ return 0
185
+ }
186
+ }
187
+
188
+ /**
189
+ * 向上取整到指定小数位
190
+ * @param value 数值
191
+ * @param decimalPlaces 小数位数,默认 2
192
+ * @returns 向上取整后的结果(number 类型)
193
+ */
194
+ export function ceil(value: number | string | Decimal, decimalPlaces = 2): number {
195
+ try {
196
+ const decimal = safeDecimal(value)
197
+ return decimal.toDecimalPlaces(decimalPlaces, Decimal.ROUND_CEIL).toNumber()
198
+ } catch {
199
+ return 0
200
+ }
201
+ }
202
+
203
+ /**
204
+ * 向下取整到指定小数位
205
+ * @param value 数值
206
+ * @param decimalPlaces 小数位数,默认 2
207
+ * @returns 向下取整后的结果(number 类型)
208
+ */
209
+ export function floor(value: number | string | Decimal, decimalPlaces = 2): number {
210
+ try {
211
+ const decimal = safeDecimal(value)
212
+ return decimal.toDecimalPlaces(decimalPlaces, Decimal.ROUND_FLOOR).toNumber()
213
+ } catch {
214
+ return 0
215
+ }
216
+ }
217
+
218
+ /**
219
+ * 取绝对值
220
+ * @param value 数值
221
+ * @returns 绝对值(number 类型)
222
+ */
223
+ export function abs(value: number | string | Decimal): number {
224
+ try {
225
+ const decimal = safeDecimal(value)
226
+ return decimal.abs().toNumber()
227
+ } catch {
228
+ return 0
229
+ }
230
+ }
231
+
232
+ /**
233
+ * 取反
234
+ * @param value 数值
235
+ * @returns 取反后的值(number 类型)
236
+ */
237
+ export function negate(value: number | string | Decimal): number {
238
+ try {
239
+ const decimal = safeDecimal(value)
240
+ return decimal.negated().toNumber()
241
+ } catch {
242
+ return 0
243
+ }
244
+ }