An DecisionTree class to simplify the generation of decision trees. For example branching surveys or everything that needs to decide on an data object.
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
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
- 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
- Shortcut method for an condition that only checks if something is in the data object for this step.
.NFQ | Christoph Kruppe
The licence used is: MIT
Click on licence badge for licence details:
If you have any furter questions please contact the following email address: email: c.kruppe@nfq.de