1.1.1 • Published 6 years ago

formula-machine v1.1.1

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

Formula machine

A formula machine for JS

example

var formula = require('formula-machine');

var src = {
  operator: '+',
  operands: [2, {
    operator: 'sqrt',
    operands: [
      { number: 4 }, // like a just number
      {
        operator: '/',
        operands: [2, 2]
      }
    ]
  }]
};

// will convert to
(2 + Math.sqrt(4 * (2/2)))

console.log(formula.toFunction(src)()); // 4

With variables

var formula = require('formula-machine');

var src = {
  operator: '+',
  operands: [2, { variable: 'name' }]
};

var fn = formula.toFunction(src);

console.log(fn({ name: 4 })); // 2 + 4 = 6

Multiple operands

var formula = require('formula-machine');

var src = {
  operator: '+',
  operands: [2, 3, 4, 5, { operator: '+', operands: [6, 7] }]
};

var fn = formula.toFunction(src);

console.log(fn()); // 2 + 3 + 4 + 5 + (6 + 7) = 27

operators

Formula machine has some binary operators:

  • 1 + 2: simple "plus" (+)
  • 1 - 2: simple "minus" (-)
  • 1 * 2: simple "multiply" (*)
  • 1 / 2: simple "division" (/)
  • Math.pow(1, 2): exponentiation (pow)

Formula machine has some unary operators:

  • Math.sqrt(4): calculating the root (sqrt)

install

With npm do:

npm install formula-machine

test

With npm do:

npm test