The way to access numerical properties had slightly changed. While in version 1.5.X and older, a property like : PP(obj.1.1) would return obj["1.1"], since version 1.6.x the same property will return obj[1][1].
In my humble opinion I think this way is more intuitive an more predictable, that is why I didn't made a new mayor version of the library, trying to make everyone getting this important change.
Install
npm
gt; npm install @nytramr/executor
yarn
gt; yarn add @nytramr/executor
API
constructor
const engine = new Engine();
compile
Syntax
compile(_textGraph_);
Parameters
name
description
textGraph
The textGraph is a graph like string that represents the code to be "compiled" into an executor.
It allows the introduction of new operators into the compiler parser, in order to extend the functionality to meet special needs.
Syntax
define(_operator_, _executer_);
Parameters
name
description
operator
The operator is a string to be used by the compiler to identify the new functionality.
executer
The executer is a special function to be used in order to compile the operator. The function must return another function that will receive the context as a parameter
Return value
undefined
Example
const engine = new Engine();
// let's define an IF operator, that depending on the boolean result of pred, will execute and return the execution of trueResult or falseResult
engine.define('IF', (pred, trueResult, falseResult) => (context) =>
pred(context) ? trueResult(context) : falseResult(context),
);
// let's create an operator that prints in the console the value got by `valueGetter`
engine.define('CL', (valueGetter) => (context) => console.log(valueGetter(context)));
var executor = engine.compile('IF(PP(value), CL(CT("true")), CL(CT("false")))');
executor({ value: true }); // prints "true"
executor({ value: 'hello' }); // prints "true"
executor({ value: 0 }); // prints "false"
executor({}); // prints "false"
Recipes
Please consider be exception free, if your new component can throw and exception, try to avoid it as much as possible.
var executor = engine.compile('FN(PP("singers"), EQ(SL(PP("name")), CT("John")))');
filter
const engine = new Engine();
engine.define('FT', (arrayGetter, string) => (context) => {
const array = arrayGetter(context);
if (Array.isArray(array)) return array.filter((element) => predicate(context, subContext, element));
return []; // you may choice to return undefined instead.
});
var executor = engine.compile('FT(PP("singers"), EQ(SL(PP("band")), CT("The Beatles")))');
getVariable
Syntax
getVariable(_name_);
Parameters
name
description
name
The name of the value to be retrieved.
Return value
undefined
Example
const engine = new Engine();
const value = engine.getVariable('variableName');
setVariable
Syntax
setVariable(_name_, _value_);
Parameters
name
description
name
The name under the value is going to be stored.
value
The value to be stored.
Return value
undefined
Example
const engine = new Engine();
engine.setVariable('variableName', 'value of the variable');
Language
PP(path)
It will return the part of the context object according to the given path. If at any point of the path a value cannot be resolved, it returns undefined.
path
Overall
The path is a divided by dots ('.') string like property and can be expressed the same way that any object is accessed programmatically.
Special Chars
There is also the possibility to use a path-like string between quotes to access to a property which contains non allowed chars like ., -, etc.
Dynamic Access
The use of squarebrackets allows using literals or other values of the same context as part of the path.
It returns a new array with every element from the array that the predicate executes in true. If none element meets the predicate, it returns an empty array ([])
example
const engine = new Engine();
const executor = engine.compile('FLT(PP("myArray"), EQ(SL(PP("band")), PP("myBand")))');
executor({
myArray: [
{ name: 'John', band: 'The Beatles' },
{ name: 'Paul', band: 'The Beatles' },
{ name: 'Ringo', band: 'The Beatles' },
{ name: 'George', band: 'The Beatles' },
{ name: 'Yoko', band: 'None' },
],
myBand: 'The Beatles',
});
/* returns [
{ name: 'John', band: 'The Beatles' },
{ name: 'Paul', band: 'The Beatles' },
{ name: 'Ringo', band: 'The Beatles' },
{ name: 'George', band: 'The Beatles' },
{ name: 'Yoko', band: 'None' },
]
*/
FND(array, predicate)
It returns the first element from the array that the predicate executes in true. It returns undefined if none element meets the predicate.
It will return the stored value under the name valueName.
example
const engine = new Engine();
const executor = engine.compile('GET(CT("someText"))');
executor({}); // returns any previously stored valued under the name "someText"
GT(value1, value2)
It will return true when the first value is greater than the second value, returns false otherwise.
const engine = new Engine();
const executor = engine.compile('SET(PP(name), CT("artistName"))');
executor({ name: 'John' }); // will store "John" under the key "artistName"
Dev Setup
Prerequisites
In order to checkout project and build and run tests locally you need to meet the following requirements:
Node.js version >= 13.14.0, you can get the latest version of Node.js from http://nodejs.org/,