marina-tree
An extensible logical tree evaluator.
npm i marina-tree
Usage
Single Fact
The same fact will be evaluated in all functions in the tree.
const Marina = ; // Functions definitionconst myFuncs = { return param instanceof Array; } { return typeof param === 'number'; } { return param % 2 === 1; }; // Initialize with my functions and a string (wildcard) that will indicate nesting.const marina = myFuncs '*'; // Build my conditions treeconst myTree = funcs: 'isAnArray' '*' facts: null // Padding funcs: 'isANumber' 'isOdd' operator: 'and' operator: 'or'; console; // falseconsole; // trueconsole; // falseconsole; // true
const Marina = ; // Functions definitionconst myFuncs = { return param instanceof Array; } { return typeof param === 'number'; } { return param >= 10; } { return param <= 100; } { return typeof param === 'function'; }; // Initialize with my functions and a string (wildcard) that will indicate nesting.const marina = myFuncs '*'; // Build my conditions treeconst myTree = funcs: '*' '*' facts: funcs: 'isANumber' 'isGte10' 'isLte100' operator: 'and' funcs: 'isANumber' '*' facts: null // Padding funcs: 'isANumber' operator: 'not' operator: 'or' operator: 'or'; console; // falseconsole; // falseconsole; // trueconsole; // trueconsole; // falseconsole; // trueconsole; // trueconsole; // falseconsole; // true
Single Fact + Nested Args
Each function will receive the same fact + a specific argument.
const Marina = ; // Definition of more generic and reusable functionsconst myFuncs = { return singleFactspecificArgfield >= specificArgvalue; } { return singleFactspecificArgfield <= specificArgvalue; } { return specificArgvalue > -1; }; // Initialize with my functions and a string (wildcard) that will indicate nesting.const marina = myFuncs '*'; // Build my conditions treeconst myTree = funcs: '*' '*' '*' facts: funcs: 'gte' 'lte' operator: 'and' funcs: '*' 'in' facts: funcs: 'in' 'gte' operator: 'and' null // Padding operator: 'or' funcs: 'in' operator: 'not' operator: 'and'; const nestedArgs = field: 'age' value: 20 field: 'age' value: 80 field: 'lang' value: 'java' 'c++' field: 'exp' value: 5 field: 'lang' value: 'cobol' field: 'lang' value: 'php' ; const persons = age: 20 lang: 'c++' exp: 2 age: 25 lang: 'java' exp: 5 age: 60 lang: 'cobol' exp: 30 age: 30 lang: 'php' exp: 10 ; console; // falseconsole; // trueconsole; // trueconsole; // false
Operators
- and
- nand
- or
- xor
- not (unary)
- none (unary)
- ... (alias for none) (unary)
Marina
License
MIT