Extensive math expression evaluator library for JavaScript and Node.js

Overview

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 Soulver

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
| ......^^^^^^^
*/

Format

Format the results

var value = Fcal.eval('10345345.3454');
console.log(value.toFormat()); // 10,345,345.3554

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

Buy Me A Coffee

πŸ“ License

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

FOSSA Status

You might also like...

Drag and drop library for two-dimensional, resizable and responsive lists

GridList Drag and drop library for a two-dimensional resizable and responsive list of items Demo: http://hootsuite.github.io/grid/ The GridList librar

Dec 14, 2022

A high-performance, dependency-free library for animated filtering, sorting, insertion, removal and more

MixItUp 3 MixItUp is a high-performance, dependency-free library for animated DOM manipulation, giving you the power to filter, sort, add and remove D

Dec 24, 2022

Browser fingerprinting library with the highest accuracy and stability.

Browser fingerprinting library with the highest accuracy and stability.

FingerprintJS is a browser fingerprinting library that queries browser attributes and computes a hashed visitor identifier from them. Unlike cookies a

Dec 31, 2022

autoNumeric is a standalone library that provides live as-you-type formatting for international numbers and currencies.

autoNumeric is a standalone library that provides live as-you-type formatting for international numbers and currencies.

What is autoNumeric? autoNumeric is a standalone Javascript library that provides live as-you-type formatting for international numbers and currencies

Dec 16, 2022

A platform detection library.

Platform.js v1.3.6 A platform detection library that works on nearly all JavaScript platforms. Disclaimer Platform.js is for informational purposes on

Dec 31, 2022

A benchmarking library. As used on jsPerf.com.

Benchmark.js v2.1.4 A robust benchmarking library that supports high-resolution timers & returns statistically significant results. As seen on jsPerf.

Dec 28, 2022

A wrapper library for Jitsi Meet that adds audio spatialization, to be able to create virtual meeting rooms.

A wrapper library for Jitsi Meet that adds audio spatialization, to be able to create virtual meeting rooms.

A wrapper library for Jitsi Meet that adds audio spatialization, to be able to create virtual meeting rooms.

Dec 27, 2022

Solid.js library adding signaling to built-in non-primitives

Solid.js library adding signaling to built-in non-primitives

This package provides signaled versions of Javascript's built-in objects. Thanks to it, all theirs properties will be automatically tracked while using standard API.

Dec 29, 2022

πŸ“ Algorithms and data structures implemented in JavaScript with explanations and links to further readings

πŸ“ Algorithms and data structures implemented in JavaScript with explanations and links to further readings

JavaScript Algorithms and Data Structures This repository contains JavaScript based examples of many popular algorithms and data structures. Each algo

Dec 31, 2022
Comments
  • Control significant digits

    Control significant digits

    Currently, Fcal returns many digits of precision. For example, 100/3 = 33.333333333333333333. This is excessive for my use -- rounding to a few significant digits would be better. Perhaps evaluate could take an optional argument that limits the digits of precision?

    opened by lhagan 3
  • Issue with units

    Issue with units

    There is an issue with units when evaluating expressions:

    23 m / 23 m should be evaluated to 1 not 1 Meters

    Same for multiplication:

    3 m * 3 m should be evaluated to 9 Square Meters not 9 Meters

    And so forth

    bug good first issue 
    opened by iamrenaud 3
  • Add license scan report and status

    Add license scan report and status

    Your FOSSA integration was successful! Attached in this PR is a badge and license report to track scan status in your README.

    Below are docs for integrating FOSSA license checks into your CI:

    opened by fossabot 1
  • Admit :: in variable declaration

    Admit :: in variable declaration

    This allow compute and define a Dataview field you can use in other place

    • My calcs radius : 20cm Area:: PI*radius^2

    • Notes We need cover =this.area

    opened by jcangas 0
Releases(v0.4.2)
Owner
Santhosh Kumar
Backend engineer. C, Go, Rust, TypeScript, and Python
Santhosh Kumar
Math Magicians - A calculator and three subpages built with React + SASS

Math Magicians JavaScript application that contains a calculator and three sub-pages. Built with React + SASS. Built With JavaScript HTML5 SASS React.

Leonardo Albornoz 8 Mar 23, 2022
JavaScript Survey and Form Library

SurveyJS is a JavaScript Survey and Form Library. SurveyJS is a modern way to add surveys and forms to your website. It has versions for Angular, jQue

SurveyJS 3.5k Jan 1, 2023
This library was designed to be used in SPA framework wrappers for the FingerprintJS Pro Javascript Agent

Framework-agnostic SPA service wrapper. Use it to build a FingerprintJS Pro wrapper for your favorite framework.

FingerprintJS 12 Sep 3, 2022
ChelseaJS - a Javascript library for creative, generative Coding

ChelseaJS is a Javascript library for creative, generative Coding. It's simple and intuitive syntax makes it easy for everyone (including non-coders)

Prakrisht Dahiya 26 Oct 6, 2022
Estrela - a JavaScript library for building reactive web components inspired by lit

Estrela ⭐ Full Reactive Web Components Estrela is a JavaScript library for building reactive web components inspired by lit. Just like Lit, Estrela is

null 50 Oct 31, 2022
BotsApp is an optimized and easy-to-use WhatsApp UserBot written in Node.js

?? BotsApp ?? Your Personal Assisstant, on WhatsApp! BotsApp is an optimized and easy-to-use WhatsApp UserBot written in Node.js. Utilize your persona

BotsApp 5.5k Jan 1, 2023
SiJago - GraphQL Client for Browser and Node.js

SiJago is GraphQL Client for Browser and Node.js, You can write request GraphQL schema using JavaScript Object Style, Why i create this tools, Because for reducing typo when writing GraphQL schema using HTTP client like Axios, Fetch or GraphQL client using Apollo and also to simplify calling the GraphQL schema easy to understand for human.

Restu Wahyu Saputra 7 Mar 13, 2022
A simple stateless microservice in Nodejs, Built with Node.js, Express and Mocha

A Stateless Microservice in NodeJS, having three major functionalities - Authentication, JSON patching and Image Thumbnail Generation.

Christotle Agholor 3 Feb 26, 2022
The perfect library for adding search, sort, filters and flexibility to tables, lists and various HTML elements. Built to be invisible and work on existing HTML.

List.js Perfect library for adding search, sort, filters and flexibility to tables, lists and various HTML elements. Built to be invisible and work on

Jonny StrΓΆmberg 10.9k Jan 1, 2023
πŸ“ž NODE.TS - WhatsApp proxy for Discord

?? NODE.TS - WhatsApp proxy for Discord

Bruno Silva 3 Mar 2, 2022