2.0.0 • Published 2 years ago

calculation-engine v2.0.0

Weekly downloads
7
License
ISC
Repository
gitlab
Last release
2 years ago

Calculation Engine

Recursively perform calcuatations from a predefined list in order to fill in missing attribute values of an object.

Calculation definitions are selected and executed only if all of their input values are defined, and if all of their outputs are null.

The order in which the calculations are defined determines the precedence of execution.

Usage

const calcEngine = require('calculation-engine');

let ce = new calcEngine([
    {
        inupts: ['var_1'],
        outputs: ['var_2'],
        fn: function (v) {
            return { var_2: v.var_1 * 2 };
        },
    },
    {
        inputs: ['var_2'],
        outputs: ['var_3'],
        excludes: ['var_4'],
        fn: function (v) {
            return { var_3: v.var_2 - 0.2 };
        },
    },
    {
        inputs: ['var_2'],
        outputs: ['var_3'],
        fn: function (v) {
            return { var_3: v.var_2 - 0.1 };
        },
    },
]);

expect(ce.calculate({
    var_1: 2,
    var_2: null,
    var_3: null,
    var_4: null,
})).to.deep.equal({
    var_1: 2,
    var_2: 4,
    var_3: 3.9,
    var_4: null,
});

Constructor

@param {Array.<Object>} definitions
@param {Array.<String>} definitions[].inputs
@param {Array.<String>} definitions[].outputs
@param {!Array.<String>} definitions[].excludes
@param {Function}       definitions[].fn 

Provide an array of objects. Each object must contain a list of input keys and a list of output keys, as well as a function to execute when the definition is selected. Optionally, attribute names listed in 'excludes' will prevent the calculation from being used if any of those attributes exist on the incoming object. Missing or invalid attributes, duplicate input/output definitions, and circular references will throw an error.

Methods

calculate

@param {Object} values
@returns {Object}

getInputKeys

@returns {Array.<String>}

getOutputKeys

@param {!Array.<String>} inputs - optional list of input keys; outputs are filtered to only those that
                                  can be satisified with the given inputs
@returns {Array.<String>}
2.0.0

2 years ago

1.0.5

3 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago