2.0.0 • Published 10 months ago

@nfq/decision-tree v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

@nfq/decision-tree

Table of Contents

Description: License: MIT

An DecisionTree class to simplify the generation of decision trees. For example branching surveys or everything that needs to decide on an data object.

Installation:

To install the package run

npm install @nfq/decision-tree

if you are in yarn

yarn add @nfq/decision-tree

or on pnpm

pnpm install @nfq/decision-tree


Usage:

import DecisionTree, {hasSomeData} from '@nfq/decision-tree';

const tree = new DecisionTree(firstStepDefaultData)
    .addBranch(
        hasSomeData,
        new DecisionTree(secondStepDefaultData)
            .addBranch(hasSomeData, returnData)
    )
    .addBranch(
        data => data.text === 'bla',
        returnDataForBlaChoosed
    );

const dataToDecideOn = [{text: 'bla'}];

tree.evaluate(dataToDecideOn); // returns returnDataForBlaChoosed

const dataToDecideOn2 = [{text: 'nope'}];

tree.evaluate(dataToDecideOn); // returns secondStepDefaultData


Methods:

DecisionTree

  • Contructor(data): | Param | type | Description | | ----- | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | | data | object, function(data, depth) | Data to give back if no other branch is matching. If you provide an function you will get the actual depth and the corresponding data from the evaluation array. |
  • addBranch(condition, action): | Param | type | Description | | ---------- | ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | | contition | boolean, function(data) | An boolean or an function that evaluates to an boolean. Gets the actual data for thism step. | | action | object, function(data, depth), DecisionTree | The action taken if condition is true. Can be another DecisionTree (for branching) or an function that gets the actual step data and the depth |

hasSomeData

  • Shortcut method for an condition that only checks if something is in the data object for this step.


Contributions:

.NFQ | Christoph Kruppe



License:

The licence used is: MIT Click on licence badge for licence details: License: MIT



Questions:

If you have any furter questions please contact the following email address: email: c.kruppe@nfq.de

2.0.0

10 months ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.3

4 years ago

1.0.0

4 years ago