npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-to-equation

v1.0.3

Published

A TypeScript-based library for evaluating nested arithmetic expressions in JSON format, utilizing high-precision Big.js calculations.

Downloads

32

Readme

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

| Symbol | Definition | Accessor | |--------------|-----------------------------------------------|---------------------------| | C | Speed of light in vacuum (m/s) | Constants.Physics.C | | G | Newton's Universal Gravitation Constant (m³ kg⁻¹ s⁻²) | Constants.Physics.G | | h | Planck's constant (J s) | Constants.Physics.h | | e | Elementary charge (C) | Constants.Physics.e | | ε0 | Vacuum permittivity (F/m) | Constants.Physics.epsilon_0 | | α | Fine-structure constant (dimensionless) | Constants.Physics.alpha |

Maths

| Symbol | Definition | Accessor | |-------------|-----------------------------------|---------------------------| | π | Pi | Constants.Math.PI | | e | Euler's number | Constants.Math.e | | φ | Golden ratio | Constants.Math.phi | | √2 | Square root of 2 | Constants.Math.sqrt2 | | п | Natural logarithm of 2 | Constants.Math.ln2 | | н10 | Natural logarithm of 10 | Constants.Math.ln10 |

Chemistry

| Symbol | Definition | Accessor | |--------------|-------------------------------------------------|-----------------------------| | NA | Avogadro's number (1/mol) | Constants.Chemistry.N_A | | R | Gas constant (J/(mol·K)) | Constants.Chemistry.R | | k | Boltzmann constant (J/K) | Constants.Chemistry.k | | F | Faraday constant (C/mol) | Constants.Chemistry.F | | Vm | Molar volume of an ideal gas at STP (m³/mol) | Constants.Chemistry.V_m | | ∆Hf | Standard enthalpy of formation for water (kJ/mol) | Constants.Chemistry.deltaH_f |

License

This project is licensed under the MIT License.