npm package: @nfq/decision-tree
Description: A simple class to make statefull decision trees
Author: .NFQ | Christoph Kruppe
Latest version: 2.0.0


# @nfq/decision-tree

# Table of Contents
- [Description](#description)
- [Installation](#installation)
- [Usage](#usage)
- [Contributions](#contributions)
- [Licence](#licence)
- [Questions](#questions)

## Description: [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/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
```sh
npm install @nfq/decision-tree
```
if you are in yarn
```sh
yarn add @nfq/decision-tree
```
or on pnpm
```sh
pnpm install @nfq/decision-tree
```
---
---
## Usage:
```javascript
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](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

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