0.4.0 • Published 4 years ago
@blastz/formula v0.4.0
Formula
Parse text expression to calculation result.
Installation
npm install @blastz/formulaExamples
List some simple examples for quick start.
prase text expression
import { formula } from "@blastz/formula";
formula("1 + 2"); // 3get lexer result
import { tokenize } from "@blastz/formula";
tokenize("1 + 2");
// [
// { image: "1", tokenType: { name: "Number" } },
// { image: "+", tokenType: { name: "Plus" } },
// { image: "2", tokenType: { name: "Number" } },
// ];Get more token detail from Interface IToken
use params
import { formula } from "@blastz/formula";
formula("{Number1} + {Number2}", { Number1: 1, Number2: 2 }); // 3All Functions And Operators
| Name | Type | Description |
|---|---|---|
| != | Logical operator | Check if one value is not equal to another value. |
| = | Logical operator | Compare if one value is equal to another value. |
| >= | Logical operator | Compare if one value is greater than, or equal to, another value. |
| > | Logical operator | Compare if one value is greater than another value |
| <= | Logical operator | Compare if one value is less than, or equal to, another value. |
| < | Logical operator | Compare if one value is less than another value. |
| + | Numeric operator | Add together two values. |
| - | Numeric operator | Subtract two values. |
| * | Numeric operator | Multiply two values. |
| / | Numeric operator | Divide two values. |
| ABS() | Numeric function | Returns the absolute value. |
| ROUND() | Numeric function | Rounds the value to the number of decimal places given by precision. |
| ROUNDUP() | Numeric function | Rounds the value to the number of decimal places given by precision, always rounding up. |
| ROUNDDOWN() | Numeric function | Rounds the value to the number of decimal places given by precision, always rounding down. |
| INT() | Numeric function | Returns the greatest integer that is less than or equal to the specified value. |
| MAX() | Numeric function | Returns the largest of the given numbers. |
| MIN() | Numeric function | Returns the smallest of the given numbers. |
| {} | Param operator | reference to a param. |
函数名映射
函数支持以中文方式调用
| 英文函数名 | 中文函数名 |
|---|---|
| ABS() | 绝对值() |
| ROUND() | 四舍五入() |
| ROUNDUP() | 向上四舍五入() |
| ROUNDDOWN() | 向下四舍五入() |
| INT() | 取整 |
| MAX() | 最大值 |
| MIN() | 最小值 |