1.2.0 • Published 5 months ago

hyoka v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

hyoka

Version License: MIT GitHub release (latest by date) Build Status Commitizen friendly

hyoka is a simple math expression parser and evaluator for JavaScript and TypeScript. it uses decimal.js to ensure precision of floating point calculations.

Features

  • Supports implicit multiplication and parenthesis grouping
  • Binary and unary operators are supported
  • Supports mathematical functions
  • Precision and rounding mode can be configured

Installation

This library can be used in both node.js and in the browser.

Using npm:

npm install hyoka

Using yarn:

yarn add hyoka

in the browser:

  • UMD
<script src="https://cdn.jsdelivr.net/npm/hyoka@latest/dist/umd/hyoka.js"></script>
  • ESM
<script src="https://cdn.jsdelivr.net/npm/hyoka@latest/dist/esm/hyoka.mjs" type="module"></script>

Usage

// using ES6 import
import {Expression} from 'hyoka';
// or using require
const { Expression } = require('hyoka');
new Expression('0.1 + 0.2').evaluate(); // 0.3
new Expression('2 * 6 / 3').evaluate(); // 4

// using unary prefix operators
new Expression('- 1 + 2').evaluate(); // 1
new Expression('+ 1 - - 2').evaluate(); // 3

// implicit multiplication
new Expression('2(6 / 3)').evaluate(); // 4

//Trig Functions
new Expression('sin(π)').evaluate(); // 0
new Expression('cos(pi / 2)').evaluate(); // -0.5

// Even more complex expressions
new Expression('2(4(6 / 3 + 2) + 2^3 / - 2(2.5!))').evaluate(); //5.413192236417259652

Configuration

hyoka configuration extends that of decimal.js. this means that all configuration options of decimal.js are available. the following configuration options are available:

  • precision: the number of digits of precision to use
  • rounding: the rounding mode to use
  • modulo: the modulo to use
  • toExpNeg: the exponent of 10 to use for negative zero
  • toExpPos: the exponent of 10 to use for positive zero
  • minE: the minimum exponent value
  • maxE: the maximum exponent value
  • crypto: whether to use the crypto module for random number generation
  • angles: the unit of angles to use for trig functions

The config options can be set using the config method on the Expression class:

import {Expression} from 'hyoka';
Expression.config({
  precision: 20,
  rounding: 4,
  angles: 'degrees'
});

new Expression('sin(30)').evaluate(); // 0.5

Supported Operators

Supported Functions

1.2.0

5 months ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago