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 🙏

© 2025 – Pkg Stats / Ryan Hefner

formula-es5

v1.0.7

Published

A math formula Class.

Readme

formula-es5

NPM version Build Status Clean Code Dependency Status devDependency Status License

Browser support

Chrome, Safari, Firefox, Opera, IE9+

Installation

    npm install formula-es5

    or 

    jspm install npm:formula-es5

Usage

Some examples :


    var Formula = require('formula-es5');


    /**
    * A `resolver` is to determine how to resolve
    * a formula. It is the formula consumer.
    * 
    * It has to implement two methods :
    *   1. canResolveSymbol() - check whether a symbol in formula expression
    *                           is valid or not.
    *   2. resolveSymbol()    - resolve a symbol and return its value.
    */
    var resolver: {
        _allowedSymNames: new RegExp('^[a-zA-Z]+$'),
        
        canResolveSymbol: function (sym) {
            return this._allowedSymNames.test(sym);
        },
        
        /**
        * A self-resolvable method.
        * 
        * In the real world, resolveSymbol() usually 
        * get symbol value from somewhere else, e.g.
        * get an equity's real-time price from stock market.
        */
        resolveSymbol: function (sym) {
            var val = 0;

            for (var i = 0; i < sym.length; i++)
                val += sym.charCodeAt(i);

            return val;
        }
    };

    var newFormula = function (resolver) {
        var f = new Formula();
        f.addResolver(resolver);    // Add resolver.
        return f;
    };


    var f = newFormula(resolver);
    /**
    * Inside setExpression() it will call resolver.canResolveSymbol(symbol)
    * to check symbols `a` and `b` 's validity.
    */
    f.setExpression('a + b');   
    f.isValid();                // true
    f.error();                  // null
    /**
    * Inside evaluate() it will call resolver.resolveSymbol(symbol)
    * to get each symbol's value and evaluate the formula.
    */
    f.evaluate();               // 195 ( a(97) + b(98) )


    var f = newFormula(resolver);
    /**
    * Inside `Formula` class, `min` is the abbreviation of `Math.min`
    */
    f.setExpression('min(a,b,c)');
    f.isValid();                // true
    f.evaluate();               // 97 ( a(97) )


    var f = newFormula(resolver);
    f.setExpression('(3*4) - 2');
    f.isValid();                // true
    f.evaluate();               // 10


    var f = newFormula(resolver);
    f.setExpression('2 + (1,3)');
    f.isValid();                // false


    /**
    * For more examples, you can see file `test/test.js`.
    */

Public properties and methods :


    constructor: Formula


    /**
    * This is not public property,
    * just to list all abbreviations.
    */
    _fnAliases: {
        'power':    'Math.pow',
        'pow':      'Math.pow',
        'absolute': 'Math.abs',
        'abs':      'Math.abs',
        'log':      'Math.log',
        'min':      'Math.min',
        'minimum':  'Math.min',
        'max':      'Math.max',
        'maximum':  'Math.max'
        'pi':       'Math.PI'
    }


    /**
    * A `symResolver` is to determine how to resolve
    * a formula. It is the formula consumer.
    */
    addResolver: function (symResolver)


    getExpression: function ()


    setExpression: function (expr)


    /**
     * Resolves all symbols and aliases, 
     * evaluates the expression.
     *
     * @return A numeric value, or NaN.
     * @throws An exception if the expression is invalid.
     */
    evaluate: function ()


    /**
     * Returns whether this formula has any unresolved symbol(s).
     * @return {Boolean}
     */
    hasUnresolvedSymbols: function ()


    /**
    * Get all symbols.
    * @return {Array}
    */
    symbols: function ()


    /**
    * Get all resolved symbols.
    * @return {Array}
    */
    resolvedSymbols: function ()


    /**
    * Get all unresolved symbols.
    * @return {Array}
    */
    unresolvedSymbols: function ()


    /**
    * Set resolver for an existing symbol.
    */
    setResolver: function (symbol, resolver)


    /**
    * Whether formula expression is valid.
    * @return {Boolean}
    */
    isValid: function ()



    /**
    * Whether formula expression has error.
    * 
    *   1. If has error it will return error text.
    *   2. If no error but has unresolved symbols, it returns the 
    *      first unresolved symbol.
    *   3. If no error and all symbols are resolved, return Null.
    *
    * @return {String or Null}
    */
    error: function ()

Tests

    npm test