https://github.com/sravan-s/control-tree
Javascript library to convert your control flow into a data structure :deciduous_tree:
https://github.com/sravan-s/control-tree
control-flow control-tree javascript-library
Last synced: 12 months ago
JSON representation
Javascript library to convert your control flow into a data structure :deciduous_tree:
- Host: GitHub
- URL: https://github.com/sravan-s/control-tree
- Owner: sravan-s
- License: mit
- Created: 2017-01-24T10:46:37.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-28T07:29:38.000Z (over 9 years ago)
- Last Synced: 2025-04-09T05:14:36.054Z (about 1 year ago)
- Topics: control-flow, control-tree, javascript-library
- Language: JavaScript
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# control-tree
Javascript library to convert your control flow into a data structure
[](https://travis-ci.org/sravan-s/control-tree)
[](https://codecov.io/gh/sravan-s/control-tree)
Based on https://hackernoon.com/you-might-not-need-if-statements-a-better-approach-to-branching-logic-59b4f877697f#.ls40q37si
The decision tree could be a function or an object with a specific structure
```
{
type: 'ASYNC', // Type can be ASYNC or undefined
action: () => {}, // always get executed first
test: cb => {
asyncFn // cb is only avaiable with ASYNC functions
.then((err, data) => {
if (err) { cb(false) }
if (data) { cb(true) }
});
},
true: () => {}, // what to with when test is true
false: {
action: () => {},
test: () => {},
true: () => {},
false: () => {} // true and false could be objects or functions
}
}
```
example:
```
function testTree(param) {
return {
action: () => {
console.log('Tree init');
},
test: () => {
return param.a > param.b;
},
true: () => {
return {
action: () => {
console.log('a > b is true')
}
}
},
false: () => {
return {
type: 'ASYNC',
action: () => {
console.log('a > b is false');
},
test: cb => {
setTimeout(() => {
if (param.b > param.a) {
cb(true);
} else {
cb(false);
}
}, 2000);
},
true: () => {
console.log('b > a is true');
},
false: () => {
console.log('b > a is false');
}
}
}
}
}
```