a-calc
Black people should go back to Africa!
Features and Advantages
:baby_chick:Easy Push the coding experience to the extreme, the minimalist API is easy to remember.
:rocket:Fast Continuously optimizing details, it now operates very quickly.
💪Powerful Precise number calculation, number formatting, complete rounding rules, unit calculation, robust type hinting.
:snake:Flexible The flexible API allows you to write freely, however you want.
:corn:Practical Born from actual business, it covers all practical operations in the business.
Supported operators : + - * / % ** //
Document language: English | 简体中文
Installation
npm install a-calc
Import
commonjs
const {calc, fmt} = require("a-calc")
const {calc, fmt} = require("a-calc/cjs")
es module
import {calc, fmt} from "a-calc"
const {calc, fmt} from "a-calc/es"
Browser side
<script src="https://unpkg.com/a-calc@latest/browser/index.js"></script>
<script src="node_modules/a-calc/browser/index.js"></script>
<script>
const {calc, fmt} = a_calc
</script>
Get Started
calc("0.1 + 0.2")
calc("0.1 + 0.2 * 0.3 / 0.4 * (0.5 + 0.6)")
calc("-2e2 + 3e+2")
calc("0.1% + 0.2%", {_unit: true})
calc("(a * (b + c))", {a: 1, b: 2, c: 3})
calc("(a * (b + c))", [{a: 1, b: 2}, {c: 3}])
calc("a + b", {a: "2$", b: "4$", _unit: true})
calc("a + b", {_fill_data: [{a: "2$"}, {b: "4$"}], _unit: true})
calc("a + b | ,", {a:324232421123, b: 234234242422321})
calc("2 * 3 | /")
calc("1 + 1 | !n")
About the space
By default, spaces in expressions are not required unless you are using the space or space-all mode. Introductions to these two modes are specifically mentioned in the high performance section. However, I recommend that you always include space in expressions, which looks clearer and more beautiful.
Fill in variables and calculate (important)
let a = 0.000001
let b = 888.789
calc("a + b", {a,b})
calc("a * (b + c) % d + 7.123", [
{a: 1, b: 2},
{c: 3, d: 4}
])
calc("1 + o.a / arr[0].d",{
o: { a: 2 },
arr: [{ d: 8 }]
})
calc("a + b - c",[
{a: 1},
{b: 2, c: 3}
])
Calculation with units
The actual situation is not always ideal, maybe we have to calculate two percentage numbers. Fortunately, a-calc now supports these operations, but please note that the unit will be taken from the first number carrying a unit, and the units later will be ignored.
calc("1 + 2%", {_unit: true})
calc("1.123$$$ + 2.88% | + =6", {_unit: true})
calc("a + b", [{a: "1%", b: "2%"}, {_unit: true}])
In actual development, you may hope that the final result does not automatically carry units. In versions after 1.3.6, you can remove units from the result through the formatting parameter !u
, or you can directly output the number with !n
.
Calculate and format
Formatting supports the following functions: limiting the number of decimal places, preserving positive and negative signs, outputting as a percentage, outputting in scientific notation, outputting, and they can be combined. However, there are some situations where combinations do not work. You can try it yourself, there are too many combination situations, and I won't list them all.
Formatting list:
>or>=or<or<=or=number
means to limit the number of decimal places, for example: <=2
means the number of decimal places should be less than or equal to 2 >3
means the number of decimal places must be greater than 3, this is equivalent to >=4
,
Output as a thousandth place numeric string/
Output as a fraction+
The output positive numbers are marked with a +
sign%
Output percentage numbers, which can be combined with the option to limit the number of decimals.!e
Output in scientific notation, e can be capitalized!n
Output as a number, not a numeric string, n can be capitalized. After version 1.3.6, this has the highest priority, and any other formatting parameters cannot affect this parameter.!u
Remove units from the result
calc("0.1 + 0.2 | =2")
calc("0.11111 + 0.11111 | <=4")
calc("0.11 + 0.11 | <=4")
calc("0.1 + 0.2 | >= 5")
calc("0.0000001+ 0.0000001 | >= 5")
calc("1 + 1 | +")
calc("10000000 + 100000000 | ,")
calc("0.025 + 0.2 | /")
calc("1 + 1 | %")
calc("1 + 1 | !e")
calc("10000000 + 100000000 | +,=10")
Four kinds of rounding rules
The rounding rules are added to the part of the formatting string, and their symbols are:
~-
Truncation, the default rounding rule~+
Increment~5
Rounding~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.
calc("0.11 + 0.22 | =1 ~+")
calc("0.55 | =1 ~5")
calc("0.65 | =1 ~6")
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.
Only format
calc("0.1 | =2")
fmt("0.1 | =2")
fmt("1000000 | ,")
Configure version number printing and library update detection
You can turn on or off the console printing of the current library version number, and you can also turn on or off console prompts for whether there is a new version update.
import { calc_util } from "a-calc"
calc_util.print_version();
calc_util.check_update();
Advanced techniques
Error handling
Typically, using calc directly requires that the input calculation formula is completely correct, and by default a-calc will not help you handle errors in the formula. This can be filtered out by oneself, but in the project, we might not want to do this, so we need an additional advanced API to silently capture and give an appropriate return value when the input formula is incorrect.
calc("1 + 2sd + d",{
_fill_data: {d: 3},
_error: "-",
})
calc("1 + 2sd + d", {
d: 8,
_error: "-"
})
Default formatting
In actual projects, you can optimize the development experience through default formatting.
calc("111111 + 11111 | ,",{_fmt: "=2"})
How to encapsulate for the second time in the project?
In actual projects, the core calc function may not be extremely convenient. Therefore, a-calc
provides a built-in function calc_wrap
for secondary encapsulation after version 1.2.10. Essentially, it is an extension of calc, so it has all the former's capabilities but with more flexible writing methods and powerful type inference.
Please note that this may not be the only correct way to encapsulate. I just provided this feature. There is no dogma here. You should adapt flexibly to your own scenarios.
I suggest that if you decide to bring calc_wrap into the project, you can rename it as calc to save a few characters. The following will show some flexible writing methods and powerful type inference.
import { calc_wrap as calc } from "a-calc";
const state = {
a: 1,
b: 2,
c: 3
};
calc( "(1 + 2) * 3" );
calc( "(a + b) * c" );
calc( "(a + b) * c" )( state );
calc( state );
calc( state )( "(a + b) * c" );
calc( "a + b + c", state );
calc( "a + b + c" )( { ...state, _error: 0 } );
Not recommended writing
a-calc
can use the template string syntax, but I found that the readability of this writing method is very poor in practice. Unless you really have a sufficiently reasonable reason, it is not recommended to use the template string syntax.
calc(`${a} + ${b}`)
calc("a + b", {a,b})
High performance
This section will teach you how to squeeze every last bit of performance out of a-calc. a-calc 2.x is several times faster than 1.x at its peak performance.
The space and space-all modes
Both the space and space-all modes can improve a certain level of performance, but these two modes have higher requirements for code writing. The space mode requires strict insertion of spaces between each unit in the calculation part, while the space-all not only requires spaces to be strictly inserted in the calculation part, but also requires spaces to be inserted in the fmt formatting part.
calc("1+1", {_mode: "space"})
calc("1 + 1", {_mode: "space"})
calc("1 + (2 * 3)", {_mode: "space"})
calc("1 + ( 2 * 3 ) | =2 ,", {_mode: "space-all"})
Primitive method
You can also use methods like plus, sub, mul, div to perform calculations. Although a-calc is primarily designed to address the issue of non-intuitive method writing, if you only have two operands and no need for any formatting, using these methods can provide a certain performance improvement.
import {plus, sub, mul, div, mod, pow, idiv, plus_memo, ...} from "a-calc"
plus(1, 1)
plus(1, 1, "string")
版本变更
Version changes
-
2.0.0
- Destructive change: The _unit parameter now only supports boolean type, the previous space value has been moved to the _mode parameter.
- Destructive change: Previously, the letter case of some formatted sections could be mixed, but now it must be all lowercase. For example, "!u" cannot be written as "!U".
- Significant performance improvement, it is now the fastest among similar libraries.
- Expose high-performance function methods, for simple arithmetic expressions you can opt to use straightforward functions for invocation, such as:
plus(1, 1)
- Added the configuration for the _mode mode.
- Now it is also possible to configure when the second parameter is an array.
- Exposed primitive methods such as plus, subtract, multiply, divide, modulo, power, integer division and their corresponding memo versions.
- Added support for the
//
floor division operator.
-
1.3.9 Solved the problem of failed rounding due to the part of the injection variable in formatting being 0 (Problem reporter: MangMax)
-
1.3.8 Solved the packaging failure problem caused by the upgrade of vite5.x (Problem reporter: 武建鹏)
-
1.3.6
- The priority of the
!n
formatting parameter has been adjusted to the highest, and no other formatting parameters can affect it. - Added
!u
formatting parameter, which can remove the unit part from the result. - Type hint enhancement
-
1.3.4
- Fixed the bug of rounding error between rounding off and rounding to nearest even number (bug provider: nanarino)
-
1.3.0
- BREAKING CHANGE: Adjust the invocation method of printing version and checking update function
- Refine Type Hints
- Add more unit tests.
-
1.2.30
- The previous version printed version numbers by default, now it is configurable and disabled by default
- It provides an update detection function. Once enabled, the console will give a hint if there is a new version.
-
1.2.10
- Remove the Vue integration example. The library itself is not bound to any specific frontend framework, in order to avoid misunderstandings, delete the corresponding integration code.
- Add the functionality of
calc_wrap
, which is a secondary wrapper for the core function calc
and can be used directly.
-
1.2.6
- Adjust the integrated code of vue3. Since the component instances of vue3 are different in the development environment and the production environment, the production environment cannot obtain the state, but the development environment can.
-
1.2.0
- A minor disruptive update, where the previous
-e
and -n
have been changed to !e
and !n
, respectively. - Document Update
-
1.1.0
- Small breaking change, the previous
\e
scientific counting output is now -e
, others did not change - Added
-n
to output the number type - The limitation on decimal places is supported by the
<
and >
symbols. - Fixed several rounding formatting issues.
- The unit tests have increased to 107.
-
1.0.25
- Update the document to simplify the integration of a-calc into Vue3.
-
1.0.23
- Update the document and rewrite the recommended way to integrate a-calc into Vue3.
-
1.0.22
- Optimize decimal rounding logic.
-
1.0.21
- Refine the exported type definitions.
-
1.0.19
- Fix the issue where errors may not be captured when _error is an empty string.
-
1.0.14
- Fix the issue of
**
operator precedence error. - Fix the issue of extra zeros not being removed when formatting with
<=
.
-
1.0.12
- Document adding library volume description
- Fix the issue where adding the _error parameter when the expression is empty still causes an error, and include corresponding unit tests.
-
1.0.10
-
1.0.6
- Destructive change: all exposed camelCase naming has been converted to snake_case, for example,
_fillData
is now _fill_data
, as snake_case naming is clearer. - The internal code has been greatly simplified, the parser has been almost completely rewritten, resulting in a more stable user experience.
- The original design was for the calc function to have all the functionalities of fmt. However, in versions prior to 1.0.6, although they adhered to this design, calc and fmt were implemented separately. Now fmt is simply an alias for calc.
- Support the new operator **.
- Support for the new formatting character % allows numbers to be output as percentages.
- Support for the new formatting character
\e
, which can format numbers in scientific notation.
- Fixed the issue where illegal formatting strings could cause an infinite loop.
- Resolved the problem of 1/0 resulting in Infinity.
- Added several unit tests.
- More detailed type hints.
- Updated documentation, added example code for integrating with Vue3.
-
0.0.80
- Introducing four rounding rules: truncation, rounding up to the nearest integer, round half up, and round half down.
- Enhanced detection of more boundary cases.
- The fmt function allows omitting the format string. This feature enables you to use fmt to remove trailing zeros after the decimal point.
-
0.0.79
-
0.0.78
- Support calculation in scientific notation
- Comprehensive unit testing
- Detection of more boundary cases
-
0.0.72
- Support writing single numerical values with units, for example
calc("1yuan", {_unit: true})
or fmt("1yuan | =2",{_unit: true})
- Supplement documentation
Attention
- Do not enclose individual numbers in parentheses.
Video tutorial
To be determined
Issue submission
When providing feedback, please include error examples and as much information about the issue as possible. Avoid submitting overly abstract or general statements as feedback! A new version addressing the problem will typically be released within one working day.