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

basic-jsmath

v0.1.0

Published

A lightweight math parsing library to deal with some basic maths

Downloads

9

Readme

basic-jsmath

A lightweight math parsing library to deal with some basic maths

Table Of Contents

Methods

Parsing

  • parse(calculation: string) -> Operations

    Parse math syntax into operations, refer to specifications for more info

    Examples:

    // This will parse to operations which
    // can be used in execute method
    const ops = parse("1 + 2")

Calculate

  • execute(   operations: Operations,   variables: Variables = {},   strict: boolean = true ) -> number

    Execute math operations and will return the result as number

    Examples:

    // Will execute the previously parsed operations and return '3'
    const ops = parse("1 + 2")
    execute(ops)
    
    // Using variables, will return '3'
    const opsWithVars = parse("a + b")
    execute(opsWithVars, { a: 1, b: 2 })
    
    // Disable error when failed to load variables, will return '0' because no variables defined
    execute(opsWithVars, {}, false)
  • math(   calculation: string,   variables: Variables = {},   strict: boolean = true ) -> number

    Parse and execute operations and will return the result as number

    Examples:

    // Both works the same
    
    // The long path
    const ops = parse("1 + 1")
    execute(ops)
    
    // The short path
    math("1 + 1")

Function

  • wrap(   operations: Operations,   varName: string,   variables: Variables = {},   strict: boolean = true ) -> ((val: number) => number)

    Wrap the operations into a javascript function

    Examples:

    // Parse the operations of calculating sinus, we use x as the input variable
    const sinOps = parse("sin(deg(x))")
    
    // Notice how we use "x" as the varName because our input variable is "x"
    const sin = wrap(sinOps, "x")
    
    // Will return 1
    sin(90)
  • wrapMath(   calculation: string,   varName: string,   variables: Variables = {},   strict: boolean = true ) -> ((val: number) => number)

    Parse and wrap the operations into a javascript function

    Examples:

    // Both works the same
    
    // The long path
    const sinOps = parse("sin(deg(x))")
    const sin = wrap(sinOps, "x")
      
    // The short path
    const sin = wrapMath("sin(deg(x))", "x")
    
    // Both will return the same value
    sin(90)

Specifications

Operations

(Operations | Operation)[]

Operation

{ prev: Block, next: Block, operator: Operators }

Block

Number Block | Variable Block | Function Block

Number Block

{ type: 0, value: number }

Variable Block

{ type: 1, name: string }

Function Block

{ type: 2, name: string, params: Operations[] }

Operators

| Operator | Description | | - | - | | + | Summation of numbers, 1 + 2 = 3 | | - | Subtraction of numbers, 3 - 2 = 1 | | * | Multiplication of numbers, 2 * 2 = 4 | | / | Division of numbers, 10 / 2 = 5 | | % | Modulus of numbers, 7 % 2 = 1 | | ^ | Exponentiation of number, 3 ^ 3 = 27 |

Variables

Record<string, Variable>

/**
 * Variables name are case-sensitive, X ≠ x
 */

// Variable only
const vars = {
    x: 100
}

// Function only
const vars = {
    x: (x) => x * x / x ** 1 / x
}

// Variable and function
const vars = {
    x: [100, (x) => x * x / x ** 1 / x]
}

Variable

number | ((...values: number[]) -> number) | [number, (...values: number[]) -> number]

Helpers

Default Variables

| Variable | Description | | - | - | | PI | The ratio of the circumference of a circle to its diameter, 3.141592653589793 | | SQRT2 | Square root of 2, 1.4142135623730951 |

Default Functions

| Function | Description | | - | - | | round or ROUND | Round the provided number into nearest integer, round(2.99999) = 3 | | toFIxed or TOFIXED | Round into nearest fixed decimal position, toFixed(8.384444, 2) = 8.38 | | sqrt or SQRT | Square root of number, sqrt(16) = 4 | | cbrt or CBRT | Cube root of number, cbrt(125) = 5 | | nthrt or NTHRT | Nth root of number, nthrt(1296, 4) = 6 | | sin or SIN | Calculate sinus of number, sin(1.5707963267948966) = 1 | | cos or COS | Calculate cosinus of number, cos(0) = 1 | | tan or TAN | Calculate tangent of number, tan(0.7853981633974483) = 1 | | deg or DEG | Transform degree into radiant, deg(90) = 1.5707963267948966 | | rad or RAD | Transform radiant into degree, rad(1.5707963267948966) = 90 |