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

calculy

v4.0.4

Published

Lightweight, zero dependency calculator engine

Downloads

24

Readme

pipeline status

Calculy

Lightweight, zero dependency calculator engine.

Only ≈2kB min + gzipped! ✨

Installation

npm install calculy

Usage

import { calculate } from "calculy";

calculate("1+2(3!)^2"); // 73
calculate("ans*2", { constants: { ans: 3 } }); // 6
calculate("ans^x", { constants: { ans: 3, x: 2 } }); // 9
calculate("cos(90)"); // -0.44807361612
calculate("cos(90)", { deg: true }); // 0

You can also directly include ./dist/calculy.min.js from the npm package. This will make all calculy functions available under the global variable Calculy.

Syntax

Calculy supports most basic algebra that you would need for regular calculations.

Addition & Substraction

2+2 = 4

2-2 = 0

Multiplication & Division

2*2 = 4

1/2 = 0.5

Exponents

2^2 = 4

Grouping

2^(2+1) = 8

4*5-2^2-3 = 1

Constants

e ≈ 2.718281828459045

π or pi ≈ 3.141592653589793

φ or phi ≈ 1.618033988749895

τ or tau ≈ 6.283185307179586

Custom constants

ans*2 = 3 (if the provided constant ans is 3) x^2 = 16 (if the provided constant x is 4)

Shorthand multiplication

2pi ≈ 6.283185307179586

(4-2)(3-4) = -2

3(4-2)^2 = 12

Percentages

50% = 0.5

-200% = -2

Big number notation

2.5e+6 = 2500000

2.5e-3 = 0.0025

Absolute

|-3| = 3

|(2*2-6)| = 2

Factorials

5! = 5

0.5! ≈ 0.8862269254527586

Roots

√(9) or √(9, 2) or sqrt(9) = 3

√(27, 3) or cbrt(27) = 3

√(81, 4) = 3

Logarithms

log(100) or log(100, 10) = 2

log(8, 2) = 3

ln(e) or log(e, e) = 3

Trigonometry

sin(pi/2) = 1

cos(2pi) = 1

tan(pi/4) ≈ 1

asin(0.5) ≈ 0.5235987755982989

acos(0.5) ≈ 1.0471975511965979

atan(0.5) ≈ 0.4636476090008061

Other functions

rand() = random number between 0 and 1

rand(2) = random number between 0 and 2

rand(2, 5) = random number between 2 and 5

round(2.6) = 3

floor(2.6) = 2

ceil(2.2) = 3

ceil(2.2) = 3

API

calculate(code: string, [options: Object]) => number

Will execute the math and give the answer. Can throw a SyntaxError.

Options

  • constants: Custom constants (Object.<number>), will override default constants with same name. (default: {})
  • deg: Whether to use degrees instead instead of radians for trigonometry functions. (default: false)

tokenize(code: string) => string[]

Will return a list of the tokens that calculy parses. Can come in handy for input validation/formatting.

parse(tokens: string[], [options: Object]) => Expression

Will return the raw AST, useful for custom evaluation. Can throw a SyntaxError.

evaluate(expression: Expression, [options: Object]) => number

Will evaluate a calculation expression;