Expressions are the backbone of Plywood. The basic flow is to construct an expression and then hand it to Plywood for evaluation. Expressions can be expressed as JSON or composed via the provided operands.
There are many ways of creating expressions to account for all the different ways in which Plywood can be used.
# $(name)
Will create a reference expression that refers to the given name.
Writing $('x')
is the same as parsing $x
var ex = $('x');
ex.compute({ x: 10 }).then(console.log); // => 10
# r(value)
Will create a literal value expression out of the given value.
var ex = r('Hello World');
ex.compute().then(console.log); // => 'Hello World'
There are a number of basic literal expressions that are provided as static members on the Expression
class.
Expression.NULL.equals(r(null));
Expression.ZERO.equals(r(0));
Expression.ONE.equals(r(1));
Expression.FALSE.equals(r(false));
Expression.TRUE.equals(r(true));
Expression.EMPTY_STRING.equals(r(''));
Expressions are designed to be composed by method chaining. All of the functions below operate on an expression and produce an expression in turn.
# operand.performAction(action: Expression)
Perform a specific action on an expression. This method is useful if you are creating actions as action objects. All the methods below create the appropriate action and then call this method.
# operand.add(...exs: any[])
Adds the arguments to the operand.
Writing $('x').add(1)
is the same as parsing $x + 1
var ex = $('x').add('$y', 1);
ex.compute({ x: 10, y: 2 }).then(console.log); // => 13
# operand.subtract(...exs: any[])
Subtracts the arguments from the operand.
Writing $('x').subtract(1)
is the same as parsing $x - 1
var ex = $('x').subtract('$y', 1);
ex.compute({ x: 10, y: 2 }).then(console.log); // => 7
# operand.negate()
Negates the operand.
Writing $('x').negate()
is the same as parsing -$x
var ex = $('x').negate();
ex.compute({ x: 10 }).then(console.log); // => -10
# operand.multiply(...exs: any[])
Multiplies the operator by the arguments.
Writing $('x').multiply(3)
is the same as parsing $x * 3
var ex = $('x').multiply('$y', 3);
ex.compute({ x: 10, y: 2 }).then(console.log); // => 60
# operand.divide(...exs: any[])
Divides the operator by the arguments.
Writing $('x').divide(3)
is the same as parsing $x / 3
var ex = $('x').divide('$y');
ex.compute({ x: 10, y: 2 }).then(console.log); // => 5
# operand.reciprocate()
Reciprocates the operand.
Writing $('x').reciprocate()
is the same as parsing 1 / $x
var ex = $('x').reciprocate();
ex.compute({ x: 10 }).then(console.log); // => 0.1
# operand.absolute()
Applies absolute value to the operand.
var ex = $('x').absolute();
ex.compute({ x: -10 }).then(console.log); // => 10
# operand.power(ex: number)
Raises the operand to the power of the expression value.
var ex = $('x').power(0.5);
ex.compute({ x: 4 }).then(console.log); // => 2
# operand.is(ex: any)
Checks that the operand and the given expression are equal.
Writing $('x').is(5)
is the same as parsing $x == 5
var ex = $('x').is(5);
ex.compute({ x: 10 }).then(console.log); // => false
# operand.isnt(ex: any)
Checks that the operand and the given expression are not equal.
Writing $('x').isnt(5)
is the same as parsing $x != 5
var ex = $('x').isnt(5);
ex.compute({ x: 10 }).then(console.log); // => true
# operand.lessThan(ex: any)
Checks that the operand is less than the given expression.
Writing $('x').lessThan(5)
is the same as parsing $x < 5
var ex = $('x').lessThan(5);
ex.compute({ x: 10 }).then(console.log); // => false
# operand.lessThanOrEqual(ex: any)
Checks that the operand is less than or equal the given expression.
Writing $('x').lessThanOrEqual(5)
is the same as parsing $x <= 5
var ex = $('x').lessThanOrEqual(5);
ex.compute({ x: 10 }).then(console.log); // => false
# operand.greaterThan(ex: any)
Checks that the operand is greater than the given expression.
Writing $('x').greaterThan(5)
is the same as parsing $x > 5
var ex = $('x').greaterThan(5);
ex.compute({ x: 10 }).then(console.log); // => true
# operand.greaterThanOrEqual(ex: any)
Checks that the operand is greater than the given expression.
Writing $('x').greaterThanOrEqual(5)
is the same as parsing $x >= 5
var ex = $('x').greaterThanOrEqual(5);
ex.compute({ x: 10 }).then(console.log); // => true
# operand.contains(ex: any, compare?: string)
Checks whether the operand contains the given expression. An optional argument specifying weather the check should be case sensitive is provided.
var ex = $('str').contains('ello');
ex.compute({ str: 'Hello World' }).then(console.log); // => true
# operand.match(re: string)
Checks whether the operand matches the given RegExp that is provided as a string.
var ex = $('str').match('^Hell.*d