a-calc 3.0.0-beta.20260201.4 → 3.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 +107 -81
- package/browser/index.js +2 -2
- package/calc.d.ts +0 -17
- package/cjs/index.js +2 -2
- package/es/index.js +2 -2
- package/mcp-server/src/index.js +0 -0
- package/package.json +132 -131
- package/src/wasm/wasm_calc.d.ts +167 -0
- package/src/wasm/wasm_calc.js +910 -0
- package/src/wasm/wasm_calc_bg.wasm +0 -0
- package/src/wasm/wasm_calc_bg.wasm.d.ts +49 -0
package/README.md
CHANGED
|
@@ -1,128 +1,154 @@
|
|
|
1
1
|
# a-calc
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**📖 Full Documentation: [https://a-calc.vercel.app/](https://a-calc.vercel.app/)**
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/a-calc) [](https://www.npmjs.com/package/a-calc)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
A powerful and easy-to-use JavaScript library for precision arithmetic and number formatting.
|
|
8
8
|
|
|
9
|
-
##
|
|
9
|
+
## Features
|
|
10
10
|
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
- **
|
|
11
|
+
- **Precision Arithmetic** — Solves JavaScript floating-point issues (`0.1 + 0.2 = 0.3`)
|
|
12
|
+
- **Rich Formatting** — Thousands separators, percentages, fractions, scientific notation, compact format
|
|
13
|
+
- **Unit Arithmetic** — Calculate with units attached to numbers
|
|
14
|
+
- **Chain API** — Fluent chainable arithmetic (`cadd(1,2).mul(3)()`)
|
|
15
|
+
- **Aggregation** — `calc_sum`, `calc_avg`, `calc_max`, `calc_min`, `calc_count`
|
|
16
|
+
- **Multiple Compute Modes** — Decimal (default), BigInt, WASM
|
|
17
|
+
- **High Performance** — Fastest among similar libraries
|
|
18
|
+
- **TypeScript** — Full type support with smart inference
|
|
17
19
|
|
|
18
|
-
##
|
|
20
|
+
## Install
|
|
19
21
|
|
|
20
22
|
```bash
|
|
21
23
|
npm install a-calc
|
|
22
24
|
```
|
|
23
25
|
|
|
24
|
-
##
|
|
26
|
+
## Quick Start
|
|
25
27
|
|
|
26
28
|
```javascript
|
|
27
|
-
import { calc, fmt } from "a-calc";
|
|
29
|
+
import { calc, fmt, cadd } from "a-calc";
|
|
28
30
|
|
|
29
|
-
//
|
|
30
|
-
calc("0.1 + 0.2");
|
|
31
|
-
|
|
32
|
-
// 复杂表达式
|
|
31
|
+
// Precision arithmetic
|
|
32
|
+
calc("0.1 + 0.2"); // "0.3"
|
|
33
33
|
calc("0.1 + 0.2 * 0.3 / 0.4 * (0.5 + 0.6)"); // "0.265"
|
|
34
34
|
|
|
35
|
-
//
|
|
36
|
-
calc("a + b", { a: 1, b: 2 });
|
|
35
|
+
// Variables
|
|
36
|
+
calc("a + b", { a: 1, b: 2 }); // "3"
|
|
37
|
+
calc("price * qty | =2", { price: 9.9, qty: 3 }); // "29.70"
|
|
38
|
+
|
|
39
|
+
// Formatting
|
|
40
|
+
calc("1234567 | ,"); // "1,234,567"
|
|
41
|
+
calc("1234567 | =2,"); // "1,234,567.00"
|
|
42
|
+
calc("1234567 | !c"); // "1.23M"
|
|
37
43
|
|
|
38
|
-
//
|
|
39
|
-
fmt("
|
|
44
|
+
// Direct formatting (v3.0)
|
|
45
|
+
fmt(1234567, "=2,"); // "1,234,567.00"
|
|
46
|
+
fmt(1234567, "!c"); // "1.23M"
|
|
40
47
|
|
|
41
|
-
//
|
|
42
|
-
|
|
48
|
+
// Chain API (v3.0)
|
|
49
|
+
cadd(100, 200, 300)(); // "600"
|
|
50
|
+
cadd(100, 200).mul(2)("=2,"); // "600.00"
|
|
51
|
+
|
|
52
|
+
// Aggregation (v3.0)
|
|
53
|
+
import { calc_sum, calc_avg, calc_max, calc_min } from "a-calc";
|
|
54
|
+
calc_sum("price", [{ price: 10 }, { price: 20 }]); // "30"
|
|
55
|
+
calc_avg("score", [{ score: 80 }, { score: 90 }]); // "85"
|
|
43
56
|
```
|
|
44
57
|
|
|
45
|
-
##
|
|
58
|
+
## Core API
|
|
46
59
|
|
|
47
|
-
### calc(expr,
|
|
60
|
+
### calc(expr, options?)
|
|
48
61
|
|
|
49
|
-
|
|
62
|
+
Evaluate expression with optional variables and formatting.
|
|
50
63
|
|
|
51
64
|
```javascript
|
|
52
|
-
calc("a * b + c", { a: 1, b: 2, c: 3 });
|
|
53
|
-
calc("100 + 200 | ,");
|
|
54
|
-
calc("0.1% + 0.2%", { _unit: true });
|
|
65
|
+
calc("a * b + c", { a: 1, b: 2, c: 3 }); // "5"
|
|
66
|
+
calc("100 + 200 | ,"); // "300"
|
|
67
|
+
calc("0.1% + 0.2%", { _unit: true }); // "0.3%"
|
|
68
|
+
calc("a > 10 ? a * 0.9 : a", { a: 15 }); // "13.5"
|
|
55
69
|
```
|
|
56
70
|
|
|
57
|
-
### fmt(value)
|
|
71
|
+
### fmt(value, format?)
|
|
58
72
|
|
|
59
|
-
|
|
73
|
+
Format a number directly (v3.0 new API).
|
|
60
74
|
|
|
61
75
|
```javascript
|
|
62
|
-
fmt(
|
|
63
|
-
fmt(
|
|
64
|
-
fmt(
|
|
76
|
+
fmt(1234567, ","); // "1,234,567"
|
|
77
|
+
fmt(0.1234, "=2"); // "0.12"
|
|
78
|
+
fmt(1234567, "!c:wan"); // "123.45万"
|
|
65
79
|
```
|
|
66
80
|
|
|
67
|
-
###
|
|
68
|
-
|
|
69
|
-
|
|
|
70
|
-
|
|
71
|
-
| `=N`
|
|
72
|
-
| `<=N` |
|
|
73
|
-
| `>=N` |
|
|
74
|
-
| `,`
|
|
75
|
-
| `+`
|
|
76
|
-
|
|
|
77
|
-
|
|
|
78
|
-
| `!e`
|
|
79
|
-
| `!n`
|
|
80
|
-
| `!
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
|
85
|
-
|
|
|
86
|
-
|
|
|
87
|
-
| `~+` |
|
|
88
|
-
|
|
89
|
-
|
|
81
|
+
### Formatting Reference
|
|
82
|
+
|
|
83
|
+
| Token | Description | Example |
|
|
84
|
+
|-------|-------------|---------|
|
|
85
|
+
| `=N` | Fixed N decimals | `=2` → "1.00" |
|
|
86
|
+
| `<=N` | At most N decimals | `<=2` → "1.1" |
|
|
87
|
+
| `>=N` | At least N decimals | `>=2` → "1.00" |
|
|
88
|
+
| `,` | Thousands separator | "1,000" |
|
|
89
|
+
| `+` | Show positive sign | "+1" |
|
|
90
|
+
| `%%` | Percentage | "50%" |
|
|
91
|
+
| `//` | Fraction | "1/2" |
|
|
92
|
+
| `!e` | Scientific notation | "1e+3" |
|
|
93
|
+
| `!n` | Output as number | returns `number` |
|
|
94
|
+
| `!c` | Compact format | "1.23M" |
|
|
95
|
+
| `!c:preset` | Compact preset | `!c:wan` → "1.23万" |
|
|
96
|
+
| `!t:preset` | Thousands preset | `!t:eu` → "1.234,50" |
|
|
97
|
+
| `!i:N` | Integer zero-padding | `!i:3` → "005" |
|
|
98
|
+
| `~-` | Truncate (default) | |
|
|
99
|
+
| `~5` | Round half up | |
|
|
100
|
+
| `~6` | Banker's rounding | |
|
|
101
|
+
| `~+` | Round up | |
|
|
102
|
+
|
|
103
|
+
### Chain API (v3.0)
|
|
90
104
|
|
|
91
105
|
```javascript
|
|
92
|
-
|
|
93
|
-
|
|
106
|
+
import { cadd, csub, cmul, cdiv } from "a-calc";
|
|
107
|
+
|
|
108
|
+
cadd(1, 2, 3)(); // "6"
|
|
109
|
+
cadd(10).sub(3).mul(2)(); // "14"
|
|
110
|
+
cadd(1000, 2000)("=2,"); // "3,000.00"
|
|
94
111
|
```
|
|
95
112
|
|
|
96
|
-
|
|
113
|
+
### Standalone Arithmetic (v3.0)
|
|
97
114
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
| **a-calc** | 423ms | 3650ms |
|
|
101
|
-
| mathjs@13.0.2 | 610ms | 5724ms |
|
|
102
|
-
| math-expression-evaluator | 701ms | 6764ms |
|
|
103
|
-
| mathjs@12.4.3 | 3791ms | 36948ms |
|
|
115
|
+
```javascript
|
|
116
|
+
import { add, sub, mul, div } from "a-calc";
|
|
104
117
|
|
|
105
|
-
|
|
118
|
+
add(0.1, 0.2); // 0.3 (number)
|
|
119
|
+
mul(3, 4, 5); // 60 (number)
|
|
106
120
|
|
|
107
|
-
-
|
|
108
|
-
-
|
|
109
|
-
|
|
121
|
+
// String output with r-prefix
|
|
122
|
+
import { radd, rmul } from "a-calc";
|
|
123
|
+
radd("0.1", "0.2"); // "0.3" (string)
|
|
124
|
+
```
|
|
110
125
|
|
|
111
|
-
|
|
126
|
+
### Configuration (v3.0)
|
|
112
127
|
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
128
|
+
```javascript
|
|
129
|
+
import { set_config, reset_config } from "a-calc";
|
|
130
|
+
|
|
131
|
+
set_config({ _error: 0 }); // Error returns 0 instead of "-"
|
|
132
|
+
set_config({ _fmt: "=2," }); // Global default format
|
|
133
|
+
set_config({ _compact_default: "wan" }); // Default compact preset
|
|
134
|
+
reset_config(); // Reset all
|
|
118
135
|
```
|
|
119
136
|
|
|
120
|
-
##
|
|
137
|
+
## Performance
|
|
121
138
|
|
|
122
|
-
|
|
139
|
+
| Library | 50,000 ops | 500,000 ops |
|
|
140
|
+
|---------|-----------|------------|
|
|
141
|
+
| **a-calc** | 423ms | 3650ms |
|
|
142
|
+
| mathjs@13.0.2 | 610ms | 5724ms |
|
|
143
|
+
| math-expression-evaluator | 701ms | 6764ms |
|
|
144
|
+
| mathjs@12.4.3 | 3791ms | 36948ms |
|
|
145
|
+
|
|
146
|
+
## Documentation
|
|
123
147
|
|
|
124
|
-
|
|
148
|
+
For complete API reference, advanced features (unit conversion, compute modes, format groups, etc.), visit:
|
|
125
149
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
150
|
+
**📖 [https://a-calc.vercel.app/](https://a-calc.vercel.app/)**
|
|
151
|
+
|
|
152
|
+
## License
|
|
153
|
+
|
|
154
|
+
[MIT](./LICENSE)
|