1.0.3 • Published 9 months ago

json-to-equation v1.0.3

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

json-to-equation, a light weight library leveraging big.js to perform precise arithmetic operations using json notation.

Features

  • JSON-Based Expressions: Perform arithmetic operations using JSON notation for easy and readable mathematical expressions.
  • Expressions are tree strcutured, can be nested to any levels.
  • Support for Common Operators: Includes support for addition, subtraction, multiplication, division, and exponentiation.
  • Provides accurate values for important mathematical constants.
  • Big Number Precision: Utilizes big.js for high-precision arithmetic calculations.

Installation

npm install json-to-equation --save

Usage

Importing the Library

import { Constants, evaluate } from 'json-to-equation';

Basic Expressions

console.log(evaluate({
  $add: [15,5]
})); // Output: 15

console.log(evaluate({
    $add: [100, {
        $mul: [
            {
                $add: [1, 2]
            }, // Result : 3 
            {
                $exp : [10, 2]
            } // Result : 100 
        ] // Result : 400 
    }]
})) // Output: 400

Complex Expressions

Express complex equations in JSON notation.

Newtons Law of Gravity

Newtons law of universal gravitation can be expressed as:

import { Constants, evaluate } from 'json-to-equation';

const m1 = 10; 
const m2 = 20;
const r = 12

console.log(evaluate({
    $mul : [
        Constants.Physics.G, // Universal Gravitational Constant
        {
            $div : [
                {
                    $mul : [m1, m2]
                },
                 {
                    $mul : [r, r]
                },
            ]
        }
    ]
})) 

Expression Formats

  • Expressions should be provided as objects with a single key representing the operator, and an array of operands which can be numbers or nested expressions.
  • The operhands array can contain only two values, which can be either a value or another nested expression, so that it can always be respresented as an expression tree.

Valid Expressions

// 1 + 2 
const  addingTwoNumbers = evaluate({
    $add : [1,2] // Result =  3 
})

// 1 + ( 2 + 3 )
const  addingThreeNumbers = evaluate({
    $add : [1, {
        $add : [2,3] // Result = 5
    }] // Result = 6
})

// 10 * 6
const multiplicationExample= evaluate({
    $mul : [10, 6] // Result = 60
})

// 2 * 10^2
const multiplicationAndExponent= evaluate({
    $mul : [2, {
        $exp : [10,2]
    }]
})

Invalid Expressions

// The Wrong Way  
const  addingThreeNumbers = evaluate({
    $add : [1,2,3] // Error : Operhands array can have only two values
})

// The Correct Way
const  addingThreeNumbers = evaluate({
    $add : [1, {
        $add : [2,3]
    }] // Result = 6 , Note: Operhands array right now have two values 
})

Supported Operators

  • $add: Adds two numbers or expressions.
  • $sub: Subtracts the second number or expression from the first.
  • $mul: Multiplies two numbers or expressions.
  • $div: Divides the first number or expression by the second.
  • $exp: Raises the first number or expression to the power of the second

Constants

Accessing Constants

import { Constants } from 'json-to-equation';

console.log(Constants.Physics.C); // Output: Speed of light - 299792458
console.log(Constants.Math.PI);   // Output: Pi - 3.141592653589793
console.log(Constants.Math.PI);   // Output: Faraday constant - 96485.33212

Physics

SymbolDefinitionAccessor
CSpeed of light in vacuum (m/s)Constants.Physics.C
GNewton's Universal Gravitation Constant (m³ kg⁻¹ s⁻²)Constants.Physics.G
hPlanck's constant (J s)Constants.Physics.h
eElementary charge (C)Constants.Physics.e
ε0Vacuum permittivity (F/m)Constants.Physics.epsilon_0
αFine-structure constant (dimensionless)Constants.Physics.alpha

Maths

SymbolDefinitionAccessor
πPiConstants.Math.PI
eEuler's numberConstants.Math.e
φGolden ratioConstants.Math.phi
2Square root of 2Constants.Math.sqrt2
пNatural logarithm of 2Constants.Math.ln2
н10Natural logarithm of 10Constants.Math.ln10

Chemistry

SymbolDefinitionAccessor
NAAvogadro's number (1/mol)Constants.Chemistry.N_A
RGas constant (J/(mol·K))Constants.Chemistry.R
kBoltzmann constant (J/K)Constants.Chemistry.k
FFaraday constant (C/mol)Constants.Chemistry.F
VmMolar volume of an ideal gas at STP (m³/mol)Constants.Chemistry.V_m
HfStandard enthalpy of formation for water (kJ/mol)Constants.Chemistry.deltaH_f

License

This project is licensed under the MIT License.