
Product
Introducing Socket Fix for Safe, Automated Dependency Upgrades
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
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. You may also want to look at the example running on CloudFlare workers https://github.com/ThomasHambach/expression.worker
In theory, all browsers supporting ECMAScript 5.1.
In practice, this target has been extensively tested against:
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:
import {Expression} from 'ncalcjs';
const e = new Expression('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 the
following way. Each function is expected to be of the type EvaluateFunctionHandler
.
const e = new Expression('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 method Expression.HasErrors()
to check for any errors that are present in your
expression. The errors details are stored in Expression.errors
.
import {Expression} from 'ncalcjs';
const e = new Expression('2 + 3 * 5');
if (e.HasErrors()) {
console.error(e.errors);
}
Round
does not return the correct valueInstall all dependencies
npm install
Before we can run our build, we need to install ts-patch
that will change our paths
configured
in tsconfig.json
. Note that you have to run this every time after running npm install
.
npm run prepare
Build the distribution version
npm run build
Note that the files, except for NCalc.g4
in /src/Grammar
are automatically generated. Any
changes you wish to make there are to be made in NCalc.g4
. You will need Java runtime installed on
your system to generate these files.
To update the generated files, run this commond inside /src/Grammar
.
antlr4 -Dlanguage=TypeScript NCalc.g4
Special thanks to https://github.com/Thaina for creating the basis of this package's ANTLR4 grammar file. Original C# version available at: https://github.com/Thaina/NCalc2/blob/master/grammer/NCalc.g
NCalc C# implementation.
Pure asynchronous C# implementation of NCalc by Peter 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 at Panoramic Data.
Starter kit to run NCalcJS in a cloudflare worker.
FAQs
Typescript/Javascript implementation of NCalc
The npm package ncalcjs receives a total of 767 weekly downloads. As such, ncalcjs popularity was classified as not popular.
We found that ncalcjs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Automatically fix and test dependency updates with socket fix—a new CLI tool that turns CVE alerts into safe, automated upgrades.
Security News
CISA denies CVE funding issues amid backlash over a new CVE foundation formed by board members, raising concerns about transparency and program governance.
Product
We’re excited to announce a powerful new capability in Socket: historical data and enhanced analytics.