HORPYNA

DESCRIPTION
This module is for better organizing chain processes with multiple branches.
It allows to design process flow by creating branches and setting conditions when
they should be called.
Main use case is to create flow template and reuse it with setting concrete functions instead of template ones.
Main unit is a Branch. Each branch accepts condition function and action function.
Also each branch can accept other branches.
API
Horpyna(options: Object): branch
or
new Horpyna(options: Object): branch
Creates new branch.
options
{ name: String
condition: Function,
action: Function,
branches: Array<Branch>
}
- name - branch name
- condition - function with condition to test
- action - function to call if condition pass
- branches - array of sub branches
example
import Horpyna from "Horpyna";
const mainBranch = Horpyna({
name: "mainBranch",
condition: value => value > 10,
action: value => value + 1,
branches: [
Horpyna({
name: "maxBranch",
condition: value => value >= 15,
action: value => 15
})
]
});
mainBranch(10)
.then(console.log)
mainBranch(11)
.then(console.log)
mainBranch(15)
.then(console.log)
branch.changeCondition(condition: (value: any) => result: any): branch
Changes branch condition. Returns branch instance.
example
import Horpyna from "Horpyna";
const mainBranch = Horpyna({
name: "mainBranch",
condition: value => value > 10,
action: value => value + 1,
});
mainBranch(11)
.then(console.log)
mainBranch.changeCondition(value => value > 11);
mainBranch(11)
.then(console.log)
branch.changeAction(action: (value: any) => result: any): branch
Changes branch action. Returns branch instance.
example
import Horpyna from "Horpyna";
const mainBranch = Horpyna({
name: "mainBranch",
condition: value => value > 10,
action: value => value + 1,
});
mainBranch(11)
.then(console.log)
mainBranch.changeAction(value => value + 2);
newMainBranch(11)
.then(console.log)
branch.addBranch(branchName: String, subBranch: branch): branch
Adds additional branch to existing one
example
import Horpyna from "Horpyna";
const mainBranch = Horpyna({
name: "mainBranch",
condition: value => value > 10,
action: value => value + 1,
branches: [
Horpyna({
name: "maxBranch",
condition: value => value >= 15,
action: value => 15
})
]
});
mainBranch(10)
.then(console.log)
mainBranch.addBranch(Horpyna({
name: "minBranch",
condition: value => value < 15,
action: value => value
}));
mainBranch(10)
.then(console.log)
branch,getBranch(branchName: String): branch
Returns first branch by name. If branch doesn't exist it will return null.
example
import Horpyna from "Horpyna";
const mainBranch = Horpyna({
name: "mainBranch",
condition: () => true,
action: () => true,
branches: [
Horpyna({
name: "maxBranch",
condition: () => true,
action: () => true
})
]
});
const maxBranch = mainBranch.getBranch("maxBranch");
branch,findBranch(branchName: String): branch
It will search in all branch tree beginning from current branch. If there is no such a branch it will return null.
example
import Horpyna from "Horpyna";
const mainBranch = Horpyna({
name: "mainBranch",
condition: () => true,
action: () => true,
branches: [
Horpyna({
name: "maxBranch",
condition: () => true,
action: () => true,
branches: [
Horpyna({
name: "someDeepBranch",
condition: () => true,
action: () => true
})
]
})
]
});
const someDeepBranch = mainBranch.findBranch("someDeepBranch");
branch,getAction(): function
Returns branch action function
branch,getName(): string
Returns branch name
branch(input): promise
Returns promise resolvable to calculated output.
example
import Horpyna from "Horpyna";
const mainBranch = Horpyna({
name: "mainBranch",
condition: value => value > 10,
action: value => value + 1,
});
mainBranch(11)
.then(console.log)
LICENSE
MIT