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

formula-resolver

v0.7.6

Published

**(+,-,*,/,^,=,>=,<=,<>)**. **Numeric** and **true/false** operands are supported. ### Also, **parentheses “()”** in the formulas are supported in the current version.

Downloads

29

Readme

FORMULA RESOLVER npm version npm

This is a simple formula parser which support excel functions and mathematical operators.

The library has been written by TypeScript.

(+,-,*,/,^,=,>=,<=,<>). Numeric and true/false operands are supported.

Also, parentheses “()” in the formulas are supported in the current version.

Supported functions are as below:

Logical functions:

  • IF
  • AND
  • OR

Most of mathematical Excel functions from formulajs have been ported or rewritten and have added to the library since version 0.7.0:

| SUM | ROUND | POWER | ABS | TRUNC | FLOOR | FACT | | ------------|:-------------:| ---------------:|-----------:|-------------:|------------:|---------------:| | ADD | MINUS | MULTIPLY | GTE | LT | LTE | GT | | ROUNDUP | ROUNDDOWN | RANDBETWEEN | DIVIDE | QUOTIENT | PRODUCT | MULTINOMIAL| | ACOT | ACOTH | ACOS | ACOSH | COMBINA | COMBIN | CEILING | | RAND | RADIANS | MROUND | MOD | NE | EQ | GCD | | BASE | LCM | ATAN2 | LOG10 | LOG | TANH | TAN | | SQRTPI | SQRT | SINH | SIGN | SECH | SEC | ODD | | LN | INT | FACTDOUBLE | ATANH | EVEN | DEGREES | DECIMAL | | CSCH | CSC | COTH | COSH | ASINH | ATAN | COS | | ASIN |

Other functions will be added to package in coming versions.

You can add any custom methods in addition to built-in ones

Formula should be field by the specific values.

Variables like cell addresses are not supported.

Example of correct formula:

125+IF(OR(false,-100,20),500,SUM(1,15,8))+10+4*5/ABS(-100)+14

API

Resolver

You can use "resolve" for resolving your normal formula:

import and usage example:

import { Resolver } form 'node_modules/formula-resolver/dist/formula-resolver'

const expression = 'IF(true=true,SUM(10,20,5),1000/0)';
const resolver = new Resolver();
const result = resolver.resolve(expression).result;

Also, you can add your custom functions on Resolver with register method: The input parameter of the register method is from FunctionInfo type. passive and source properties of FunctionInfo class are not used in the current version but the functionality will be added to the next versions.

Example of register method usage:

import { Resolver, FunctionInfo } form 'node_modules/formula-resolver/dist/formula-resolver'

const resolver = new Resolver();
const functionInfo = new CustomFunctionInfo(
        (params: string, source: any) => {
            console.log(params);
            return params;
        },
        'PRINT'
    )
resolver.register(functionInfo);

const expression = 'IF(10<0,PRINT("Passed!"),PRINT("Rejected!"))';
resolver.resolve(expression).result;

DynamicResolver

The resolve method of this class has more complex algorithm than normal resolver class so it is slower than normal resolver in most cases. This class is especially useful when you want to prevent the execution of your custom functions inside the conditional functions like IF or inside your costum conditional functions when the condition is not matched.

import { DynamicResolver } from "node_modules/formula-resolver/dist/formula-resolver";

let resolver: DynamicResolver = new DynamicResolver();
class TestResolver implements CustomResolver {

    DO = function(this: DynamicResolver, param: string[][]) {
        let strParam = this.resolvePreProcessedParameter(param[0]);
        console.log('Method DO Called With Param ' + strParam);
        return strParam + '';
    }
    resolveFunction(functionContext: DynamicResolver, fn: FunctionInfo) {
        switch (fn.fnName) {
            case 'DO':
                return this.DO.bind(functionContext)(fn.params);
            default:
                return '';
        }
    }
}
let testResolver = new TestResolver();
const expression = 'IF(true=true,DO(1),DO(2))';
resolver.register(new TestResolver());
resolver.resolve(expression).result

Version 0.7.6 changes:

Comare operaor some bug fixes

Version 0.7.5 changes:

Dynamic If bug fixed. Few other bug fixes.

Version 0.7.4 changes:

Consumer build bug fixed. Some Math functions bug fixed. DynamicResolver for custom resolver method improved.

Version 0.7.1 changes:

Optional extraParams parameter with type any has been added to DynamicResolver resolve method and it would be passed to your custom methods.

Version 0.7.0 changes:

  • Parenthesis bug in formula has been fixed.
  • String result of the function now has been moved to an object for preventing future breaking change when extra fields like debugging info would be added to the result.
  • Most of Excel supported Math formula now have been supported.
  • DynamicResolver class has been added to the package. The resolve method of this class prevents unwanted execution of methods or operations inside of the methods like If, Or, And or other custom and conditional functions. This class can help to preventing unwanted execution of custom functions which interact with user interface or server, inside the conditional blocks.

Version 0.2.0 changes:

  • register method has been added to the resolver module for defining custom functions.
  • Operator regex bugs have been fixed.