@wzo/calc 0.0.1
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/LICENSE +21 -0
- package/README.md +115 -0
- package/README.zh.md +115 -0
- package/dist/index.cjs +1354 -0
- package/dist/index.d.cts +449 -0
- package/dist/index.d.mts +449 -0
- package/dist/index.mjs +1324 -0
- package/package.json +67 -0
- package/src/index.ts +31 -0
- package/src/utils/aggregate.ts +109 -0
- package/src/utils/calc.ts +100 -0
- package/src/utils/chain.ts +127 -0
- package/src/utils/config.ts +59 -0
- package/src/utils/format.ts +366 -0
- package/src/utils/parser.ts +310 -0
- package/src/utils/precision.ts +442 -0
- package/src/utils/standalone.ts +126 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 nowo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="./logo.svg" width="88" height="88" alt="@wzo/calc" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">@wzo/calc</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
Precision math + number formatting — zero runtime dependencies, BigInt all the way down
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<a href="https://www.npmjs.com/package/@wzo/calc"><img src="https://img.shields.io/npm/v/@wzo/calc?color=cb3837&logo=npm&logoColor=white" alt="npm version" /></a>
|
|
13
|
+
<img src="https://img.shields.io/badge/TypeScript-Ready-3178C6?logo=typescript&logoColor=white" alt="TypeScript" />
|
|
14
|
+
<img src="https://img.shields.io/badge/dependencies-0-44CC11" alt="zero dependencies" />
|
|
15
|
+
<img src="https://img.shields.io/badge/target-ES2022-F7DF1E?logo=javascript&logoColor=black" alt="ES2022" />
|
|
16
|
+
<img src="https://img.shields.io/badge/license-MIT-blue" alt="MIT" />
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
<p align="center"><b>English</b> | <a href="./README.zh.md">简体中文</a></p>
|
|
20
|
+
|
|
21
|
+
JavaScript's native floating-point gets it wrong (`0.1 + 0.2 === 0.30000000000000004`). `@wzo/calc` uses BigInt for strict decimal rational arithmetic, and provides type-friendly number formatting.
|
|
22
|
+
|
|
23
|
+
## ✨ Features
|
|
24
|
+
|
|
25
|
+
- 🎯 **Precision arithmetic** — represented internally as `digits × 10^(-exp)`; every rational operation is exactly correct
|
|
26
|
+
- 🧮 **Expression evaluation** — `calc('1 + 2 * 3')`: pure arithmetic + math functions (`abs`/`min`/`max`/`pow`/`mod`/`clamp`…); use template interpolation for variables
|
|
27
|
+
- 🪢 **Computation / display separation** — `calc` throws on error (for computation); `fmt` is the display variant of calc — supports arithmetic and falls back to `_error` on failure (for template rendering)
|
|
28
|
+
- 🎨 **Type-friendly formatting** — thousands separators, percent, compact, fraction, scientific, 4 rounding modes — all configured via an `IFormat` object (IDE completion, mistakes caught at write time)
|
|
29
|
+
- 🔗 **Chaining / standalone functions / aggregation** and more API styles
|
|
30
|
+
- 📦 **Zero runtime dependencies**, runs in the browser and Node, complete TypeScript types
|
|
31
|
+
|
|
32
|
+
## 📦 Install
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pnpm add @wzo/calc
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## 🚀 Quick start
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { calc, calcSum, chainAdd, fmt } from '@wzo/calc'
|
|
42
|
+
|
|
43
|
+
// Precision arithmetic
|
|
44
|
+
calc('0.1 + 0.2') // "0.3"
|
|
45
|
+
|
|
46
|
+
// Template interpolation instead of variables + formatting (IFormat object)
|
|
47
|
+
calc('9.9 * 3', { _fmt: { decimals: 2 } }) // "29.70"
|
|
48
|
+
|
|
49
|
+
// Direct formatting
|
|
50
|
+
fmt(1234567, { decimals: 2, thousands: true }) // "1,234,567.00"
|
|
51
|
+
fmt(1234567, { compact: 'zh' }) // "123.4567万"
|
|
52
|
+
|
|
53
|
+
// Chaining
|
|
54
|
+
chainAdd(10).sub(3).mul(2)() // "14"
|
|
55
|
+
|
|
56
|
+
// Aggregation
|
|
57
|
+
calcSum('price', [{ price: 10 }, { price: 20 }]) // "30"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## 🧮 Expression power
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
calc('(1 + 2) * 3') // four operations + parentheses
|
|
64
|
+
calc('max(3, 5) * 2') // math functions
|
|
65
|
+
calc(`${price} * 1.07`) // use template interpolation for variables
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Built-in math functions (exact): `abs` `sign` `floor` `ceil` `round` `trunc` `pow` `mod` `min` `max` `clamp`.
|
|
69
|
+
|
|
70
|
+
> Expressions are pure arithmetic — no variables / conditionals / comparisons / logic. **Use template interpolation for variables, and write conditionals outside in JS**: `a > 100 ? calc(\`${a} * 0.9\`) : String(a)`.
|
|
71
|
+
|
|
72
|
+
## 🎨 Formatting (`IFormat` object)
|
|
73
|
+
|
|
74
|
+
| Field | Description |
|
|
75
|
+
| :--- | :--- |
|
|
76
|
+
| `decimals` | Decimal places: `number` fixed, `{ min, max }` range |
|
|
77
|
+
| `rounding` | `'truncate'` (default) / `'halfUp'` / `'banker'` / `'ceil'` (with JS aliases `trunc`/`round`/`halfEven`) |
|
|
78
|
+
| `thousands` | `true` US-style / `'eu'` / `'in'` |
|
|
79
|
+
| `compact` | `true` K/M/B/T / `'zh'` (万/亿) |
|
|
80
|
+
| `clamp` | `[min, max]` value range limit |
|
|
81
|
+
| `output` | `'percent'` / `'fraction'` / `'scientific'` / `'number'` (or symbols `%%` `//` `e` `num`) |
|
|
82
|
+
| `plus` | Show the plus sign |
|
|
83
|
+
| `pad` | Zero-pad the integer part to N digits |
|
|
84
|
+
|
|
85
|
+
## 🧰 Main API
|
|
86
|
+
|
|
87
|
+
| API | Purpose |
|
|
88
|
+
| :--- | :--- |
|
|
89
|
+
| `calc(expr, options?)` | Expression evaluation + optional formatting (throws on error) |
|
|
90
|
+
| `fmt(value, options?)` | Display variant of calc: supports arithmetic + formatting (falls back on error) |
|
|
91
|
+
| `chainAdd/chainSub/chainMul/chainDiv` | Chained operations |
|
|
92
|
+
| `add/sub/mul/div` (→ number), `addStr/subStr/mulStr/divStr` (→ string) | Standalone operations |
|
|
93
|
+
| `calcSum/calcAvg/calcMax/calcMin` | Array / object-array aggregation |
|
|
94
|
+
| `setConfig/resetConfig/getConfig` | Global config (error fallback, default format, precision) |
|
|
95
|
+
|
|
96
|
+
> `div` / `divStr` / `chainDiv` / `calcAvg` accept a trailing `{ _precision }` to set the division precision for that call only, without polluting the global config.
|
|
97
|
+
|
|
98
|
+
See the [full documentation](https://nowo.github.io/calc) for details.
|
|
99
|
+
|
|
100
|
+
## 🌐 Runtime
|
|
101
|
+
|
|
102
|
+
The build output targets **ES2022** and uses **BigInt** and `Array.prototype.at` internally, so the runtime must satisfy:
|
|
103
|
+
|
|
104
|
+
| Environment | Minimum version |
|
|
105
|
+
| :--- | :--- |
|
|
106
|
+
| Chrome / Edge | 92 |
|
|
107
|
+
| Firefox | 90 |
|
|
108
|
+
| Safari (macOS / iOS) | 15.4 |
|
|
109
|
+
| Node.js | 16.6 |
|
|
110
|
+
|
|
111
|
+
That covers all major browsers from March 2022 onward. BigInt is the hard lower bound — it cannot be polyfilled.
|
|
112
|
+
|
|
113
|
+
## 📄 License
|
|
114
|
+
|
|
115
|
+
[MIT](./LICENSE) © nowo
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="./logo.svg" width="88" height="88" alt="@wzo/calc" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">@wzo/calc</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
精度数学 + 数字格式化库 —— 0 运行时依赖,内部全程 BigInt
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<a href="https://www.npmjs.com/package/@wzo/calc"><img src="https://img.shields.io/npm/v/@wzo/calc?color=cb3837&logo=npm&logoColor=white" alt="npm version" /></a>
|
|
13
|
+
<img src="https://img.shields.io/badge/TypeScript-Ready-3178C6?logo=typescript&logoColor=white" alt="TypeScript" />
|
|
14
|
+
<img src="https://img.shields.io/badge/dependencies-0-44CC11" alt="zero dependencies" />
|
|
15
|
+
<img src="https://img.shields.io/badge/target-ES2022-F7DF1E?logo=javascript&logoColor=black" alt="ES2022" />
|
|
16
|
+
<img src="https://img.shields.io/badge/license-MIT-blue" alt="MIT" />
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
<p align="center"><a href="./README.md">English</a> | <b>简体中文</b></p>
|
|
20
|
+
|
|
21
|
+
JavaScript 原生浮点会算错(`0.1 + 0.2 === 0.30000000000000004`)。`@wzo/calc` 用 BigInt 做严格的十进制有理运算,并提供类型友好的数字格式化。
|
|
22
|
+
|
|
23
|
+
## ✨ 特性
|
|
24
|
+
|
|
25
|
+
- 🎯 **精度算术** —— 内部以 `digits × 10^(-exp)` 表示,所有有理运算严格正确
|
|
26
|
+
- 🧮 **表达式求值** —— `calc('1 + 2 * 3')`,纯算术 + 数学函数(`abs`/`min`/`max`/`pow`/`mod`/`clamp`…),变量用模板插值
|
|
27
|
+
- 🪢 **计算 / 展示分离** —— `calc` 出错抛异常(计算用);`fmt` 是 calc 的展示版,支持运算且出错走 `_error` 兜底(模板渲染用)
|
|
28
|
+
- 🎨 **类型友好的格式化** —— 千分位、百分比、压缩、分数、科学记数、4 种舍入,全用 `IFormat` 对象配置(IDE 补全、写错即报错)
|
|
29
|
+
- 🔗 **链式 / 独立函数 / 聚合** 等多种 API 风格
|
|
30
|
+
- 📦 **0 运行时依赖**,浏览器 / Node 都能跑,完整 TypeScript 类型
|
|
31
|
+
|
|
32
|
+
## 📦 安装
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pnpm add @wzo/calc
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## 🚀 快速上手
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { calc, calcSum, chainAdd, fmt } from '@wzo/calc'
|
|
42
|
+
|
|
43
|
+
// 精度算术
|
|
44
|
+
calc('0.1 + 0.2') // "0.3"
|
|
45
|
+
|
|
46
|
+
// 模板插值代替变量 + 格式化(IFormat 对象)
|
|
47
|
+
calc('9.9 * 3', { _fmt: { decimals: 2 } }) // "29.70"
|
|
48
|
+
|
|
49
|
+
// 直接格式化
|
|
50
|
+
fmt(1234567, { decimals: 2, thousands: true }) // "1,234,567.00"
|
|
51
|
+
fmt(1234567, { compact: 'zh' }) // "123.4567万"
|
|
52
|
+
|
|
53
|
+
// 链式
|
|
54
|
+
chainAdd(10).sub(3).mul(2)() // "14"
|
|
55
|
+
|
|
56
|
+
// 聚合
|
|
57
|
+
calcSum('price', [{ price: 10 }, { price: 20 }]) // "30"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## 🧮 表达式能力
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
calc('(1 + 2) * 3') // 四则运算 + 括号
|
|
64
|
+
calc('max(3, 5) * 2') // 数学函数
|
|
65
|
+
calc(`${price} * 1.07`) // 变量用模板插值写进表达式
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
内置数学函数(精确):`abs` `sign` `floor` `ceil` `round` `trunc` `pow` `mod` `min` `max` `clamp`。
|
|
69
|
+
|
|
70
|
+
> 表达式是纯算术,不含变量 / 条件 / 比较 / 逻辑——**变量用模板插值、条件判断在 JS 外层写**:`a > 100 ? calc(\`${a} * 0.9\`) : String(a)`。
|
|
71
|
+
|
|
72
|
+
## 🎨 格式化(`IFormat` 对象)
|
|
73
|
+
|
|
74
|
+
| 字段 | 说明 |
|
|
75
|
+
| :--- | :--- |
|
|
76
|
+
| `decimals` | 小数位:`number` 固定、`{ min, max }` 区间 |
|
|
77
|
+
| `rounding` | `'truncate'`(默认) / `'halfUp'` / `'banker'` / `'ceil'`(含 JS 别名 `trunc`/`round`/`halfEven`)|
|
|
78
|
+
| `thousands` | `true` 美式 / `'eu'` / `'in'` |
|
|
79
|
+
| `compact` | `true` K/M/B/T / `'zh'`(万/亿)|
|
|
80
|
+
| `clamp` | `[min, max]` 值范围限制 |
|
|
81
|
+
| `output` | `'percent'` / `'fraction'` / `'scientific'` / `'number'`(或符号 `%%` `//` `e` `num`)|
|
|
82
|
+
| `plus` | 显示正号 |
|
|
83
|
+
| `pad` | 整数补零到 N 位 |
|
|
84
|
+
|
|
85
|
+
## 🧰 主要 API
|
|
86
|
+
|
|
87
|
+
| API | 作用 |
|
|
88
|
+
| :--- | :--- |
|
|
89
|
+
| `calc(expr, options?)` | 表达式求值 + 可选格式化(出错抛) |
|
|
90
|
+
| `fmt(value, options?)` | calc 的展示版:支持运算 + 格式化(出错兜底) |
|
|
91
|
+
| `chainAdd/chainSub/chainMul/chainDiv` | 链式运算 |
|
|
92
|
+
| `add/sub/mul/div`(→ number)、`addStr/subStr/mulStr/divStr`(→ string) | 独立运算 |
|
|
93
|
+
| `calcSum/calcAvg/calcMax/calcMin` | 数组 / 对象数组聚合 |
|
|
94
|
+
| `setConfig/resetConfig/getConfig` | 全局配置(错误兜底、默认格式、精度)|
|
|
95
|
+
|
|
96
|
+
> `div` / `divStr` / `chainDiv` / `calcAvg` 末尾可传 `{ _precision }` 单次指定除法精度,不污染全局。
|
|
97
|
+
|
|
98
|
+
更多细节见 [完整文档](https://nowo.github.io/calc)。
|
|
99
|
+
|
|
100
|
+
## 🌐 运行环境
|
|
101
|
+
|
|
102
|
+
构建产物语法层级为 **ES2022**,内部使用 **BigInt** 与 `Array.prototype.at`,运行环境需满足:
|
|
103
|
+
|
|
104
|
+
| 环境 | 最低版本 |
|
|
105
|
+
| :--- | :--- |
|
|
106
|
+
| Chrome / Edge | 92 |
|
|
107
|
+
| Firefox | 90 |
|
|
108
|
+
| Safari(macOS / iOS)| 15.4 |
|
|
109
|
+
| Node.js | 16.6 |
|
|
110
|
+
|
|
111
|
+
即覆盖 2022 年 3 月起的主流浏览器。其中 BigInt 是无法 polyfill 的硬性下限。
|
|
112
|
+
|
|
113
|
+
## 📄 License
|
|
114
|
+
|
|
115
|
+
[MIT](./LICENSE) © nowo
|