GistTree.Com
Entertainment at it's peak. The news is by your side.

Show HN: Fcal – Extensive math expression evaluator library for JavaScript

0

iamg

FOSSA Status
Version
License: MIT
Hits
Downloads

🏠Homepage

Fcaljs is an in depth math expression evaluator library for JavaScript and Node.js.
Using fcal, you might want to compose identical old arithmetic, share operations with precision.
It facets a flexible expression parser with a expansive situation of built-in objects, capabilities and constants.
Impressed from Numi

Fcal gui

Aspects

  • Devices
  • Variables
  • Capabilities

Playground Fcal GUI, Codepen , CLI

Instance

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

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

fcal.review('PI2 radius'); // 144.51326206514 Meters

// Predefined capabilities
fcal.review('log(23)'); // 1.3617278360175928789

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

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

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

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

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

// Predefined objects
fcal.review('hump = 20 kph'); // 20 km/h

fcal.review('hump in mps'); // 5.5555555555555555556 m/s

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

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

// Assorted amount machine
fcal.review('0b1010011 day + 45'); // 128 Days

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

Set up

Browser

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

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

With NPM

$ npm set up --effect fcal
const { Fcal } = require('fcal');

Exhaust

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

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

console.log(cost); // 11750400 Seconds

Percentage

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

That you can compose identical old share operation with + , -

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

If form of left and actual hand aspect of of is the same, then operation will return share

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

Scales

That you can utilize Thousand k, million M and billion B scales.

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

Equality and comparability

console.log(Fcal.eval('100 == 1230')); // fraudulent
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 factual ? 34: 100');
console.log(cost); // 34

Syntax errors

Fcal will throw exception if there is error with expression

For more error context, utilize data technique in FcalError

strive {
  var cost = Fcal.eval('343 + 23.45E+*34');
} train (e) {
  if (e instanceof FcalError) {
    console.log(e.data());
  }
}

/*
err: Ready for amount after + but purchased '*'
| 343 + 23.45E+*34
| ......^^^^^^^
*/

Strict mode

By default, fcal will no longer throw exception if you occur to strive to make utilize of operations between differing kinds or different objects

But with strict mode

const fcal = new Fcal();
fcal.setStrict(factual)
strive {
  var cost = fcal.review('23% + 34 cm + 1');
} train (e) {
  if (e instanceof FcalError) {
    console.log(e.data());
  }

/*
err: Unexpected '+' operation between differing kinds (unit, amount)
| 23% + 34 cm + 1
| ^^^^^^^^^^^^^^^
*/

Using expression

That you can exchange articulate of expression , re review it

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

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

exp.setValues({ radius: 8 });

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

exp.setValues({ radius: 10 });

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

exp.setValues({ radius: Infinity });

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

🤝 Contributing

Contributions, points and characteristic requests are welcome!

Be at liberty to envision points page.

To contribute

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

Escape assessments

Author

👤Santhosh Kumar

Buy Me A Coffee

📝 License

Copyright © 2019 Santhosh Kumar.


This mission is MIT licensed.

FOSSA Status

Read More

Leave A Reply

Your email address will not be published.