Skip to content

Commit

Permalink
Implement most functionality of NCalc
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasHambach committed Jan 18, 2023
1 parent 8b01966 commit 9d5d9fc
Show file tree
Hide file tree
Showing 31 changed files with 18,795 additions and 3 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Build
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]
jobs:
sonarcloud:
name: SonarCloud
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Install dependencies
run: npm install
- name: Run Tests
run: npm run test
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
20 changes: 20 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Publish Package to npmjs
on:
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v3
with:
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm run prepare
- run: npm run build
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src/Grammar/.antlr
dist
node_modules
coverage
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
src
jest.config.js
__tests__
sonar-project.properties
.github
.vscode
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"mike-lischke.vscode-antlr4"
]
}
114 changes: 111 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,111 @@
# NcalcJS

Ncalc but in Javascript.
# NCalcJS

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=ThomasHambach_NcalcJS&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=ThomasHambach_NcalcJS) [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=ThomasHambach_NcalcJS&metric=coverage)](https://sonarcloud.io/summary/new_code?id=ThomasHambach_NcalcJS) [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) [![GitHub contributors](https://img.shields.io/github/contributors/thomashambach/NCalcJS.svg)](https://github.com/thomashambach/NCalcJS/graphs/contributors)

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.

## Project Description

### Installation

Get the package from npm.org by running

npm i --s ncalcjs

or with yarn

yarn ncalcjs

### Basic Usage

```typescript
import { Expression } from "ncalcjs"
const e = new Expression("2 + 3 * 5");
console.log(e); // 17
```

### Status

- [x] ANTLR4 Grammar
- [x] Basic math
- [ ] Math Functions
- [x] Abs
- [x] Acos
- [x] Asin
- [x] Atan
- [ ] Atan2
- [x] Ceiling
- [x] Cos
- [x] Exp
- [x] Floor
- [ ] IEEEERemainder (Not in JS standard, needs custom code)
- [x] Ln
- [x] Log10
- [x] Pow
- [ ] Round (Incorrect implementation)
- [x] Sign
- [x] Sqrt
- [x] Tan
- [x] Truncate
- [ ] Standard Functions
- [x] In
- [x] If
- [ ] Min
- [ ] Max
- [ ] Custom functions
- [x] Implementation
- [ ] Document different API from regular NCalc
- [x] Custom parameters
- [x] Implementation (partially done)
- [ ] Document different API from regular NCalc
- [ ] Unit tests (partial)
- [ ] Documentation
- [ ] Usage examples
- [ ] Support older Node/JS versions
- [ ] Confirm browser support
- [ ] Resolve circular dependencies so we do not need 1 massive file. See `NCalc/Domain/index.ts`
- [ ] Improve ANTLR error handling

### Known Issues

* Not specifying a custom parameter when evaluating the expression will cause a crash.
* `Round` does not return the correct value
* Errors are not properly handled and reported.

## Building

Install 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

### ANTLR & Grammar

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

npm run grammar

## Related projects

### [NCalc](https://github.com/ncalc/ncalc/)

NCalc C# implementation.

### [NCalc-Async](https://github.com/ncalc/ncalc-async/)

Pure asynchronous C# implementation of NCalc by [Peter Liljenberg](https://github.com/petli).

### [PanoramicData.NCalcExtensions](https://github.com/panoramicdata/PanoramicData.NCalcExtensions)

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](https://github.com/panoramicdata).
Loading

0 comments on commit 9d5d9fc

Please sign in to comment.