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 CHANGED
@@ -1,128 +1,154 @@
1
1
  # a-calc
2
2
 
3
- **官网文档:[https://a-calc.vercel.app/](https://a-calc.vercel.app/)**
3
+ **📖 Full Documentation: [https://a-calc.vercel.app/](https://a-calc.vercel.app/)**
4
4
 
5
5
  [![npm version](https://img.shields.io/npm/v/a-calc.svg)](https://www.npmjs.com/package/a-calc) [![npm downloads](https://img.shields.io/npm/dw/a-calc)](https://www.npmjs.com/package/a-calc)
6
6
 
7
- 强大易用的 JavaScript 精度计算与格式化库。
7
+ A powerful and easy-to-use JavaScript library for precision arithmetic and number formatting.
8
8
 
9
- ## 特性
9
+ ## Features
10
10
 
11
- - **精确计算** - 解决 JavaScript 浮点数精度问题
12
- - **强大格式化** - 千分位、百分比、分数、科学计数法
13
- - **单位计算** - 支持带单位的数字运算
14
- - **完整舍入** - 去尾、进一、四舍五入、四舍六入
15
- - **高性能** - 同类库中最快
16
- - **TypeScript** - 完整类型支持
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"); // "0.3"
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 }); // "3"
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("1000000 | ,"); // "1,000,000"
44
+ // Direct formatting (v3.0)
45
+ fmt(1234567, "=2,"); // "1,234,567.00"
46
+ fmt(1234567, "!c"); // "1.23M"
40
47
 
41
- // 计算并格式化
42
- calc("111111 + 11111 | ,=2"); // "122,222.00"
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
- ## 核心 API
58
+ ## Core API
46
59
 
47
- ### calc(expr, data?)
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 }); // "5"
53
- calc("100 + 200 | ,"); // "300"
54
- calc("0.1% + 0.2%", { _unit: true }); // "0.3%"
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("1234567 | ,"); // "1,234,567"
63
- fmt("0.12345 | =2"); // "0.12"
64
- fmt("0.5 | %"); // "50%"
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` | 固定 N 位小数 | `=2` → "1.00" |
72
- | `<=N` | 最多 N 位小数 | `<=2` → "1.1" |
73
- | `>=N` | 最少 N 位小数 | `>=2` → "1.00" |
74
- | `,` | 千分位 | `1000` "1,000" |
75
- | `+` | 保留正号 | `1` "+1" |
76
- | `%` | 百分比 | `0.5` "50%" |
77
- | `/` | 分数 | `0.5` "1/2" |
78
- | `!e` | 科学计数法 | `1000` "1e+3" |
79
- | `!n` | 输出数字类型 | 返回 number |
80
- | `!u` | 去除单位 | "100%" → "100" |
81
-
82
- ### 舍入规则
83
-
84
- | 参数 | 说明 |
85
- | ---- | ---------------------- |
86
- | `~-` | 去尾(默认) |
87
- | `~+` | 进一 |
88
- | `~5` | 四舍五入 |
89
- | `~6` | 四舍六入(银行家舍入) |
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
- calc("0.55 | =1 ~5"); // "0.6"
93
- calc("0.65 | =1 ~6"); // "0.6"
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
- | 库 | 50,000 次 | 500,000 次 |
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
- - [快速开始](./.docs/QUICK_START.md)
108
- - [完整文档索引](./.docs/README.md)
109
- - [贡献指南](./.docs/CONTRIBUTING.md)
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
- ```bash
114
- pnpm install # 安装依赖
115
- pnpm dev # 开发模式
116
- pnpm build # 构建
117
- pnpm test # 测试
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
- [MIT](./LICENSE)
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
- - [GitHub](https://github.com/Autumn-one/a-calc-old)
127
- - [npm](https://www.npmjs.com/package/a-calc)
128
- - [问题反馈](https://github.com/Autumn-one/a-calc-old/issues)
150
+ **📖 [https://a-calc.vercel.app/](https://a-calc.vercel.app/)**
151
+
152
+ ## License
153
+
154
+ [MIT](./LICENSE)