Skip to content

Small, fast, zero-dependency library/CLI to help you parse and calculate basic math expressions

License

Notifications You must be signed in to change notification settings

heokhe/node-calc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node-calc

Build Status Coverage Status Minzipped size

A small, fast, zero-dependency library/CLI to help you parse and calculate basic math expressions. It supports +, -, *, / and ^ operators, parenthesis, and functions such as:

  • Trigonometric functions: sin, cos, tan, cot
  • sqrt, cbrt
  • Factorial: x! (or fact(x))
  • abs
  • log (base 10), ln

Functions are written in the form of f(x), fx syntax is not supported yet!

Installation

npm i @hkh12/node-calc

Examples

const { evalExpression, tokenize, Token, evalTokens } = require('@hkh12/node-calc');
evalExpression('2*2') // 4

const tokens = tokenize('2*2') // -> Token[]
evalTokens(tokens); // 4

CLI

Once installed, you can node-calc in your terminal:

# node-calc [expr...]
node-calc "2*2" "2+2" "1*3" # always wrap your expressions in quotes
node-calc # prompts you

About

Small, fast, zero-dependency library/CLI to help you parse and calculate basic math expressions

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published