عکس heokhe
Small, fast, zero-dependency library/CLI to help you parse and calculate basic math expressionsJavaScript
موضوع‌ها
۱
فورک‌ها
۰
ستاره‌ها
۹
تاریخ ایجاد
۲۳ آبان ۱۳۹۸
آخرین بروزرسانی
۵ ماه قبل
لایسنس
MIT License

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