a-calc 1.5.0-dev.2024042402 → 1.5.0-dev.2024042405
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 +69 -46
- package/a-calc.versions.js +2 -1
- package/browser/index.js +1 -1
- package/calc.d.ts +8 -1
- package/cjs/index.js +1 -1
- package/es/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
# a-calc
|
|
2
|
-
[](https://github.com/Autumn-one/a-calc-old) [](https://github.com/Autumn-one/a-calc-old) [](https://github.com/Autumn-one/a-calc-old) [](https://www.npmjs.com/package/a-calc)
|
|
2
|
+
[](https://www.npmjs.com/package/a-calc) [](https://github.com/Autumn-one/a-calc-old) [](https://github.com/Autumn-one/a-calc-old) [](https://github.com/Autumn-one/a-calc-old) [](https://www.npmjs.com/package/a-calc)
|
|
3
3
|
|
|
4
|
-
**
|
|
4
|
+
**Black people should go back to Africa!**
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
## Features and Advantages
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
**:baby_chick:Easy** Push the coding experience to the extreme, the minimalist API is easy to remember.
|
|
9
|
+
|
|
10
|
+
**:rocket:Fast** Continuously optimizing details, it now operates very quickly.
|
|
11
|
+
|
|
12
|
+
**💪Powerful** Precise number calculation, number formatting, complete rounding rules, unit calculation, robust type hinting.
|
|
13
|
+
|
|
14
|
+
**:snake:Flexible** The flexible API allows you to write freely, however you want.
|
|
15
|
+
|
|
16
|
+
**:corn:Practical** Born from actual business, it covers all practical operations in the business.
|
|
15
17
|
|
|
16
18
|
> Supported operators : + - * / % **
|
|
17
19
|
|
|
18
|
-
**
|
|
20
|
+
**Document language:** English | [简体中文](https://github.com/Autumn-one/a-calc-old/blob/main/README_ZH.md)
|
|
19
21
|
|
|
20
22
|
## Installation
|
|
21
23
|
|
|
@@ -30,7 +32,7 @@ npm install a-calc
|
|
|
30
32
|
```js
|
|
31
33
|
const {calc, fmt} = require("a-calc")
|
|
32
34
|
// or
|
|
33
|
-
const {calc, fmt} = require("a-calc/cjs")
|
|
35
|
+
const {calc, fmt} = require("a-calc/cjs")
|
|
34
36
|
```
|
|
35
37
|
|
|
36
38
|
**es module**
|
|
@@ -41,25 +43,42 @@ import {calc, fmt} from "a-calc"
|
|
|
41
43
|
const {calc, fmt} from "a-calc/es"
|
|
42
44
|
```
|
|
43
45
|
|
|
44
|
-
**
|
|
46
|
+
**Browser side**
|
|
45
47
|
|
|
46
48
|
```html
|
|
47
|
-
<script src="
|
|
49
|
+
<script src="https://unpkg.com/a-calc@latest/browser/index.js"></script> <!-- cdn -->
|
|
50
|
+
<script src="node_modules/a-calc/browser/index.js"></script> <!-- After installing npm, you can also import it locally. Choose either option. -->
|
|
48
51
|
<script>
|
|
49
52
|
const {calc, fmt} = a_calc
|
|
50
53
|
</script>
|
|
51
54
|
```
|
|
52
55
|
|
|
53
|
-
##
|
|
56
|
+
## Get Started
|
|
54
57
|
|
|
55
|
-
```
|
|
56
|
-
calc("0.1 + 0.2") // 0.3
|
|
58
|
+
```typescript
|
|
59
|
+
calc("0.1 + 0.2") // "0.3"
|
|
60
|
+
|
|
61
|
+
// A more complex calculation
|
|
62
|
+
calc("0.1 + 0.2 * 0.3 / 0.4 * (0.5 + 0.6)") // "0.265"
|
|
63
|
+
|
|
64
|
+
// Scientific notation calculation
|
|
65
|
+
calc("-2e2 + 3e+2") // "100"
|
|
57
66
|
|
|
58
|
-
//
|
|
59
|
-
calc("0.1 + 0.2
|
|
67
|
+
// Calculations with units
|
|
68
|
+
calc("0.1% + 0.2%", {_unit: true}) // "0.3%"
|
|
60
69
|
|
|
61
|
-
//
|
|
62
|
-
calc("
|
|
70
|
+
// Variable operation
|
|
71
|
+
calc("(a * (b + c))", {a: 1, b: 2, c: 3}) // "5"
|
|
72
|
+
calc("(a * (b + c))", [{a: 1, b: 2}, {c: 3}]) // "5"
|
|
73
|
+
calc("a + b", {a: "2$", b: "4$", _unit: true}) // "6$"
|
|
74
|
+
calc("a + b", {_fill_data: [{a: "2$"}, {b: "4$"}], _unit: true}) // "6$"
|
|
75
|
+
|
|
76
|
+
// Calculate and format: Thousands separator
|
|
77
|
+
calc("a + b | ,", {a:324232421123, b: 234234242422321}) // "234,558,474,843,444"
|
|
78
|
+
// Calculate and format: fractions
|
|
79
|
+
calc("2 * 3 | /") // "6/1"
|
|
80
|
+
// Calculate and format: output numbers.
|
|
81
|
+
calc("1 + 1 | !n") // 2
|
|
63
82
|
```
|
|
64
83
|
|
|
65
84
|
## About the space
|
|
@@ -77,23 +96,23 @@ In short, always add spaces!
|
|
|
77
96
|
```js
|
|
78
97
|
let a = 0.000001
|
|
79
98
|
let b = 888.789
|
|
80
|
-
calc("a + b", {a,b}) //
|
|
99
|
+
calc("a + b", {a,b}) // "888.789001"
|
|
81
100
|
|
|
82
101
|
calc("a * (b + c) % d + 7.123", [
|
|
83
102
|
{a: 1, b: 2},
|
|
84
103
|
{c: 3, d: 4}
|
|
85
|
-
]) // 8.123
|
|
104
|
+
]) // "8.123"
|
|
86
105
|
|
|
87
106
|
// A bit more complex
|
|
88
107
|
calc("1 + o.a / arr[0].d",{
|
|
89
108
|
o: { a: 2 },
|
|
90
109
|
arr: [{ d: 8 }]
|
|
91
|
-
}) // 1.25
|
|
110
|
+
}) // "1.25"
|
|
92
111
|
|
|
93
112
|
calc("a + b - c",[
|
|
94
113
|
{a: 1},
|
|
95
114
|
{b: 2, c: 3}
|
|
96
|
-
])
|
|
115
|
+
]) // "0"
|
|
97
116
|
```
|
|
98
117
|
|
|
99
118
|
## Calculation with units
|
|
@@ -102,9 +121,9 @@ calc("a + b - c",[
|
|
|
102
121
|
|
|
103
122
|
```js
|
|
104
123
|
// Please note that _unit is required and not enabled by default. This is because calculations with units will perform some additional operations, and in contrast, pure numerical calculations are faster.
|
|
105
|
-
calc("1 + 2%", {_unit: true}) // 3%
|
|
124
|
+
calc("1 + 2%", {_unit: true}) // "3%"
|
|
106
125
|
|
|
107
|
-
calc("1.123$$$ + 2.88% | + =6", {_unit: true}) // +4.003000$$$
|
|
126
|
+
calc("1.123$$$ + 2.88% | + =6", {_unit: true}) // "+4.003000$$$"
|
|
108
127
|
```
|
|
109
128
|
|
|
110
129
|
After version 1.0.6, calculations with units can have more parameters. The value of `_unit` can be `boolean | "on" | "off" | "auto" | "space"`. The parameters seem to be a lot but actually are similar to the previous usage. The effects of `true "on" "auto"` are the same, which means to automatically recognize the unit after the number. `false "off"` means to turn off the unit calculation. The biggest difference is the "space" value, which only treats the space as a unit separator. For example, if your unit happens to be +-, it will be recognized as an operator in normal mode, so you can use the "space" mode. But in this case, the space is necessary. You should write like this: `calc("2+- * 3")`. The final result is: `6+-`.
|
|
@@ -128,29 +147,29 @@ Formatting supports the following functions: limiting the number of decimal plac
|
|
|
128
147
|
|
|
129
148
|
```js
|
|
130
149
|
// Operate the decimal places
|
|
131
|
-
calc("0.1 + 0.2 | =2") // 0.30
|
|
132
|
-
calc("0.11111 + 0.11111 | <=4") // 0.2222
|
|
133
|
-
calc("0.11 + 0.11 | <=4") // 0.22
|
|
134
|
-
calc("0.1 + 0.2 | >= 5") // 0.30000
|
|
135
|
-
calc("0.0000001+ 0.0000001 | >= 5") // 0.0000002
|
|
150
|
+
calc("0.1 + 0.2 | =2") // "0.30"
|
|
151
|
+
calc("0.11111 + 0.11111 | <=4") // "0.2222"
|
|
152
|
+
calc("0.11 + 0.11 | <=4") // "0.22"
|
|
153
|
+
calc("0.1 + 0.2 | >= 5") // "0.30000"
|
|
154
|
+
calc("0.0000001+ 0.0000001 | >= 5") // "0.0000002"
|
|
136
155
|
|
|
137
156
|
// Preserve positive and negative signs
|
|
138
|
-
calc("1 + 1 | +") // +2
|
|
157
|
+
calc("1 + 1 | +") // "+2"
|
|
139
158
|
|
|
140
159
|
// Thousandth place
|
|
141
|
-
calc("10000000 + 100000000 | ,") // 110,000,000
|
|
160
|
+
calc("10000000 + 100000000 | ,") // "110,000,000"
|
|
142
161
|
|
|
143
162
|
// Fraction
|
|
144
|
-
calc("0.025 + 0.2 | /") // 9/40
|
|
163
|
+
calc("0.025 + 0.2 | /") // "9/40"
|
|
145
164
|
|
|
146
165
|
// Percentage
|
|
147
|
-
calc("1 + 1 | %") // 200%
|
|
166
|
+
calc("1 + 1 | %") // "200%"
|
|
148
167
|
|
|
149
168
|
// Scientific notation, note that this e can also be capitalized
|
|
150
|
-
calc("1 + 1 | !e") // 2e+0
|
|
169
|
+
calc("1 + 1 | !e") // "2e+0"
|
|
151
170
|
|
|
152
171
|
// Simultaneously specify decimals and thousandth place while preserving the positive and negative signs
|
|
153
|
-
calc("10000000 + 100000000 | +,=10") // +110,000,000.0000000000
|
|
172
|
+
calc("10000000 + 100000000 | +,=10") // "+110,000,000.0000000000"
|
|
154
173
|
```
|
|
155
174
|
|
|
156
175
|
## Four kinds of rounding rules
|
|
@@ -163,9 +182,9 @@ The rounding rules are added to the part of the formatting string, and their sym
|
|
|
163
182
|
- `~6` Round to Even, this rounding rule is more accurate than the normal rounding. The rule is different when the number after the rounding is 5. It will check the position after 5. If the number after 5 is not 0, it will increment. If the number after 5 is 0, it will check whether the number before 5 is even or not. If it is even, it will not increment. If it is not even, it will increment.
|
|
164
183
|
|
|
165
184
|
```js
|
|
166
|
-
calc("0.11 + 0.22 | =1 ~+") // 0.4 Keep one place and increment it
|
|
167
|
-
calc("0.55 | =1 ~5") // 0.6
|
|
168
|
-
calc("0.65 | =1 ~6") // 0.6
|
|
185
|
+
calc("0.11 + 0.22 | =1 ~+") // "0.4" Keep one place and increment it
|
|
186
|
+
calc("0.55 | =1 ~5") // "0.6"
|
|
187
|
+
calc("0.65 | =1 ~6") // "0.6"
|
|
169
188
|
```
|
|
170
189
|
|
|
171
190
|
This newly added rounding rule seems to make the formatting part longer, but the actual situation is not like this. Generally, the rounding rule of a project is fixed, so the formatting part of the rounding rule should be encapsulated in the default formatting parameters. When it is actually used, there is no need to write this part of the content at all. Refer to the following `default formatting` instructions.
|
|
@@ -173,11 +192,11 @@ This newly added rounding rule seems to make the formatting part longer, but the
|
|
|
173
192
|
## Only format
|
|
174
193
|
|
|
175
194
|
```js
|
|
176
|
-
calc("0.1 | =2") // 0.10
|
|
177
|
-
fmt("0.1 | =2") // 0.10
|
|
195
|
+
calc("0.1 | =2") // "0.10"
|
|
196
|
+
fmt("0.1 | =2") // "0.10"
|
|
178
197
|
// calc has the function of fmt, but fmt has better semantics
|
|
179
198
|
|
|
180
|
-
fmt("1000000 | ,") // 1,000,000
|
|
199
|
+
fmt("1000000 | ,") // "1,000,000"
|
|
181
200
|
```
|
|
182
201
|
|
|
183
202
|
## Configure version number printing and library update detection
|
|
@@ -214,7 +233,7 @@ calc("1 + 2sd + d", {
|
|
|
214
233
|
> In actual projects, you can optimize the development experience through default formatting.
|
|
215
234
|
|
|
216
235
|
```js
|
|
217
|
-
calc("111111 + 11111 | ,",{_fmt: "=2"}) // 122,222.00 Obviously , and =2 are combined, and the format string in the expression has a higher priority.
|
|
236
|
+
calc("111111 + 11111 | ,",{_fmt: "=2"}) // "122,222.00" Obviously , and =2 are combined, and the format string in the expression has a higher priority.
|
|
218
237
|
```
|
|
219
238
|
|
|
220
239
|
## How to encapsulate for the second time in the project?
|
|
@@ -266,7 +285,11 @@ calc("a + b", {a,b}) // Recommended writing, because it is clearer.
|
|
|
266
285
|
|
|
267
286
|
## Version changes
|
|
268
287
|
|
|
269
|
-
* 1.5.0
|
|
288
|
+
* 1.5.0
|
|
289
|
+
|
|
290
|
+
- Performance improvement
|
|
291
|
+
- Expose high performance function methods, for simple arithmetic expressions you can choose to use simple functions to call,for example:`plus("1", "1")`
|
|
292
|
+
- More robust update detection methods
|
|
270
293
|
* 1.3.9 Solved the problem of failed rounding due to the part of the injection variable in formatting being 0 (Problem reporter: MangMax)
|
|
271
294
|
* 1.3.8 Solved the packaging failure problem caused by the upgrade of vite5.x (Problem reporter: 武建鹏)
|
|
272
295
|
* 1.3.6
|
package/a-calc.versions.js
CHANGED
|
@@ -36,7 +36,8 @@ try{
|
|
|
36
36
|
'1.5.0-dev.2024042302', '1.5.0-dev.2024042303', '1.5.0-dev.2024042304',
|
|
37
37
|
'1.5.0-dev.2024042305', '1.5.0-dev.2024042306', '1.5.0-dev.2024042307',
|
|
38
38
|
'1.5.0-dev.2024042308', '1.5.0-dev.2024042309', '1.5.0-dev.2024042310',
|
|
39
|
-
'1.5.0-dev.2024042311', '1.5.0-dev.2024042312', '1.5.0-dev.2024042401',
|
|
39
|
+
'1.5.0-dev.2024042311', '1.5.0-dev.2024042312', '1.5.0-dev.2024042401',
|
|
40
|
+
'1.5.0-dev.2024042402', '1.5.0-dev.2024042404', '1.5.0-dev.2024042405'
|
|
40
41
|
]; a_calc_versions;
|
|
41
42
|
}
|
|
42
43
|
finally
|