0.4.0 • Published 2 years ago

@blastz/formula v0.4.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

Formula

Parse text expression to calculation result.

Installation

npm install @blastz/formula

Examples

List some simple examples for quick start.

prase text expression

import { formula } from "@blastz/formula";

formula("1 + 2"); // 3

get 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 }); // 3

All Functions And Operators

NameTypeDescription
!=Logical operatorCheck if one value is not equal to another value.
=Logical operatorCompare if one value is equal to another value.
>=Logical operatorCompare if one value is greater than, or equal to, another value.
>Logical operatorCompare if one value is greater than another value
<=Logical operatorCompare if one value is less than, or equal to, another value.
<Logical operatorCompare if one value is less than another value.
+Numeric operatorAdd together two values.
-Numeric operatorSubtract two values.
*Numeric operatorMultiply two values.
/Numeric operatorDivide two values.
ABS()Numeric functionReturns the absolute value.
ROUND()Numeric functionRounds the value to the number of decimal places given by precision.
ROUNDUP()Numeric functionRounds the value to the number of decimal places given by precision, always rounding up.
ROUNDDOWN()Numeric functionRounds the value to the number of decimal places given by precision, always rounding down.
INT()Numeric functionReturns the greatest integer that is less than or equal to the specified value.
MAX()Numeric functionReturns the largest of the given numbers.
MIN()Numeric functionReturns the smallest of the given numbers.
{}Param operatorreference to a param.

函数名映射

函数支持以中文方式调用

英文函数名中文函数名
ABS()绝对值()
ROUND()四舍五入()
ROUNDUP()向上四舍五入()
ROUNDDOWN()向下四舍五入()
INT()取整
MAX()最大值
MIN()最小值
0.3.0

2 years ago

0.3.2

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.3

2 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago