- Notifications
You must be signed in to change notification settings - Fork2
ThomasHambach/NcalcJS
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
NCalc is a mathematical expressions evaluator in JavaScript/TypeScript. NCalc can parse anyexpression and evaluate the result, including static or dynamic parameters and custom functions. Youmay also want to look at the example running on CloudFlare workershttps://github.com/ThomasHambach/expression.worker
In theory, all browsers supporting ECMAScript 5.1.
In practice, this target has been extensively tested against:
- Firefox 34.0.5
- Safari 8.0.2
- Chrome 39.0.2171
- Explorer 11.0.3
The package has been extensively tested against Node.js 14 LTS.
Get the package from npm.org (https://www.npmjs.com/package/ncalcjs) by running
npm i --s ncalcjs
or with yarn
yarn ncalcjs
For documentation here is the table of content:
- description: overall concepts, usage and extensibility points
- operators: available standard operators and structures
- functions: list of already implemented functions
- parameters: on how to use parameters expressions
import{Expression}from'ncalcjs';conste=newExpression('2 + 3 * 5');console.log(e.Evaluate());// 17
The API compared to C# NCalc is a little different. In NCalcJS you define custom functions in thefollowing way. Each function is expected to be of the typeEvaluateFunctionHandler
.
conste=newExpression('SecretOperation(3, 6)');e.EvaluateFunction['SecretOperation']=(args:FunctionArgs)=>{args.Result=args.Parameters[0].Evaluate()+args.Parameters[1].Evaluate();};console.log(e.Evaluate());// 9
You can use the methodExpression.HasErrors()
to check for any errors that are present in yourexpression. The errors details are stored inExpression.errors
.
import{Expression}from'ncalcjs';conste=newExpression('2 + 3 * 5');if(e.HasErrors()){console.error(e.errors);}
Round
does not return the correct value
Install all dependencies
npm install
Before we can run our build, we need to installts-patch
that will change ourpaths
configuredintsconfig.json
. Note that you have to run this every time after runningnpm install
.
npm run prepare
Build the distribution version
npm run build
Note that the files, except forNCalc.g4
in/src/Grammar
are automatically generated. Anychanges you wish to make there are to be made inNCalc.g4
. You will need Java runtime installed onyour system to generate these files.
To update the generated files, run this commond inside/src/Grammar
.
antlr4 -Dlanguage=TypeScript NCalc.g4
Special thanks to:
- Thaina for creating the basis of this package's ANTLR4 grammarfile. Original C# version available at:https://github.com/Thaina/NCalc2/blob/master/grammer/NCalc.g
- robmarti12 for implementing the static
Compile
as in original NCalc.
NCalc C# implementation.
Pure asynchronous C# implementation of NCalc byPeter Liljenberg.
Extension functions for NCalc in C# to handle many general functions,
including string functions, switch, if, in, typeOf, cast etc.
Developed by David, Dan and all atPanoramic Data.
Starter kit to run NCalcJS in a cloudflare worker.
About
NCalc is a mathematical expressions evaluator in JavaScript/TypeScript. NCalc can parse any expression and evaluate the result, including static or dynamic parameters and custom functions.