# formula-es5

> A math formula Class.

Latest version **1.0.7** (published 2016-06-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install formula-es5
pnpm add formula-es5
yarn add formula-es5
bun add formula-es5
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.7 |
| Published | 2016-06-24 |
| First published | 2016-06-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Wenwu Wu |
| Maintainers | wenwu |
| Keywords | formula, math |

## Links

- npm: https://www.npmjs.com/package/formula-es5
- Repository: https://github.com/wenwuwu/formula-es5
- Homepage: https://github.com/wenwuwu/formula-es5#readme
- Issues: https://github.com/wenwuwu/formula-es5/issues
- npm.io page: https://npm.io/package/formula-es5

## Dependencies (4)

- [math-es5](https://npm.io/package/math-es5.md) ^1.0.0
- [array-es5](https://npm.io/package/array-es5.md) ^1.0.0
- [string-es5](https://npm.io/package/string-es5.md) ^1.0.0
- [function-es5](https://npm.io/package/function-es5.md) ^1.0.4

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.7 (latest) — 2016-06-24
- 1.0.6 — 2016-06-20
- 1.0.5 — 2016-06-20
- 1.0.4 — 2016-06-20
- 1.0.3 — 2016-06-18
- 1.0.2 — 2016-06-17
- 1.0.1 — 2016-06-16
- 1.0.0 — 2016-06-16

## README

formula-es5
=========

[![NPM version][icon-nv]][link-nv]
[![Build Status][icon-bs]][link-bs]
[![Clean Code][icon-cc]][link-cc]
[![Dependency Status][icon-ds]][link-ds]
[![devDependency Status][icon-dds]][link-dds]
![License][icon-li]

## Browser support

Chrome, Safari, Firefox, Opera, IE9+

## Installation 

```shell
    npm install formula-es5

    or 

    jspm install npm:formula-es5
```

## Usage

Some examples :

```js

    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 :

```js

    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

```shell
    npm test          
```


[icon-nv]: https://img.shields.io/npm/v/formula-es5.svg?style=flat
[link-nv]: https://www.npmjs.com/package/formula-es5
[icon-bs]: https://travis-ci.org/wenwuwu/formula-es5.svg?branch=master
[link-bs]: https://travis-ci.org/wenwuwu/formula-es5
[icon-ds]: https://img.shields.io/david/wenwuwu/formula-es5.svg?style=flat
[link-ds]: https://david-dm.org/wenwuwu/formula-es5
[icon-dds]: https://img.shields.io/david/dev/wenwuwu/formula-es5.svg?style=flat
[link-dds]: https://david-dm.org/wenwuwu/formula-es5#info=devDependencies
[icon-cc]: https://img.shields.io/badge/code-clean-orange.svg?style=flat
[link-cc]: https://github.com/wenwuwu/code-convention-js 
[icon-li]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat

---
_Source: https://npm.io/package/formula-es5 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
