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

fcal

v0.4.3

Published

Math expression evaluator

Downloads

26

Readme

iamg

FOSSA Status Version License: MIT Hits Downloads

🏠 Homepage

Fcaljs is an extensive math expression evaluator library for JavaScript and Node.js. Using fcal, you can perform basic arithmetic, percentage operations with precision. It features a flexible expression parser with a large set of built-in units, functions and constants. Inspired from Numi

Fcal gui

Features

  • Units
  • Variables
  • Functions

Playground Fcal GUI, Codepen , CLI

Example

const fcal = new Fcal();
// New variable
fcal.evaluate('radius : 23 m'); // 23 Meters

fcal.evaluate('PI * radius ^ 2'); // 1661.9025137490006231 Meters

fcal.evaluate('PI2 * radius'); // 144.51326206514 Meters

// Predefined functions
fcal.evaluate('log(23)'); // 1.3617278360175928789

// Percentage
fcal.evaluate('23 % of 1023'); // 235.29

fcal.evaluate('200 sec + 120 %'); // 440 Seconds

// Unit conversion
fcal.evaluate('20 minutes + 34 day in sec'); // 2938800 Seconds

fcal.evaluate('sin(PI)'); // -1.6167204971158028306e-24

// Constants
fcal.evaluate('E'); // 2.718281828459045235360287

// Predefined units
fcal.evaluate('speed = 20 kph'); // 20 km/h

fcal.evaluate('speed in mps'); // 5.5555555555555555556 m/s

fcal.evaluate('23 C add 123 F'); // 196.40000000000004 °F

fcal.evaluate('1.2 nsec + 3 week in sec'); // 1814400.0000000012 Seconds

// Various number system
fcal.evaluate('0b1010011 day + 45'); // 128 Days

fcal.evaluate('23.44 kmh in oct'); //  0o27.341217270243656051 km/h

Install

Browser

the library is the single JavaScript file fcal.js (or minified, fcal.min.js).

<script src="https://cdn.jsdelivr.net/npm/fcal/dist/fcal.js"></script>

With NPM

$ npm install --save fcal
const { Fcal } = require('fcal');

Use

const { Fcal } = require('fcal');

const fcal = new Fcal();
var value = fcal.evaluate('102 day in minutes + abs(-34 day) in sec');

console.log(value); // 11750400 Seconds

Percentage

var value = fcal.evaluate('27% of 230 cm');
console.log(value); // 62.1 Centimeters

You can perform general percentage operation with + , -

var value = fcal.evaluate('1024 m + 6.1%');
console.log(value); // 1086.464 Meters

If type of left and right hand side of of is same, then operation will return percentage

var value = fcal.evaluate('10 of 10.100');
console.log(value); // % 99.009900990099009901

Scales

You can use Thousand k, million M and billion B scales.

var value = Fcal.eval('-0x14 M');
console.log(value); //-20000000

Equality and comparison

console.log(Fcal.eval('100 == 1230')); // false
console.log(Fcal.eval('20 cm < 1 m')); // true
console.log(Fcal.eval('100 cm != 100 m')); // true

You can use === to compare irrespective type of value

console.log(Fcal.eval('100 C === 100 F')); // true

Ternary operator

var value = Fcal.eval('234 cm > 1 m and true ? 34: 100');
console.log(value); // 34

Syntax errors

Fcal will throw exception if there is error with expression

For more error context, use info method in FcalError

try {
  var value = Fcal.eval('343 + 23.45E+*34');
} catch (e) {
  if (e instanceof FcalError) {
    console.log(e.info());
  }
}

/*
err: Expecting number after + but got '*'
| 343 + 23.45E+*34
| ......^^^^^^^
*/

Strict mode

By default, fcal will not throw exception if you try to use operations between different types or different units

But with strict mode

const fcal = new Fcal();
fcal.setStrict(true)
try {
  var value = fcal.evaluate('23% + 34 cm + 1');
} catch (e) {
  if (e instanceof FcalError) {
    console.log(e.info());
  }

/*
err: Unexpected '+' operation between different types (unit, number)
| 23% + 34 cm + 1
| ^^^^^^^^^^^^^^^
*/

Using expression

You can change state of expression , re evaluate it

const { Fcal } = require('fcal');

const exp = new Fcal().expression('PI * radius cm ^ 2');

exp.setValues({ radius: 8 });

console.log(exp.evaluate()); // 201.06192982974676726 Centimeters

exp.setValues({ radius: 10 });

console.log(exp.evaluate()); // 314.15926535897932385 Centimeters

exp.setValues({ radius: Infinity });

console.log(exp.evaluate()); // Infinity Centimeters

🤝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

To contribute

$ git clone https://github.com/5anthosh/fcal
$ npm install

Run tests

$ npm test

Author

👤 Santhosh Kumar

📝 License

Copyright © 2019 Santhosh Kumar. This project is MIT licensed.

FOSSA Status