-
Notifications
You must be signed in to change notification settings - Fork 1
Conditional Expressions
fire.js supports @if
and @unless
conditional expressions that process the input only if the condition is met. The operand used to as the condition can can be a variable path in the hint or the last result of expression block.
Conditional operands are evaluated following the Javascript evaluation rules for booleans.
@if
executes and returns the input only if the operand is evaluated as true.
The following example uses the last result as the operand, since the operand is evaluated as true the input is executed and returned as result.
{
"name": "Examples.IfLastResult",
"json": {
"@return": true,
"@if": "Totally true"
}
}
Download examples/Examples.IfLastResult.fjson and execute to see the results:
$ firejs Examples.IfLastResult.fjson
"Totally true"
@if
also works with variables paths:
{
"name": "Examples.IfVariable",
"json": {
"@set(chuckIsHere)": true,
"@if(chuckIsHere)": "Chuck is here, we are in trouble!"
}
}
Download examples/Examples.IfVariable.fjson and execute to see the results:
$ firejs Examples.IfVariable.fjson
"Chuck is here, we are in trouble!"
If the condition is not met, @if
will bypass the result.
{
"name": "Examples.IfBypass",
"json": {
"@return": "This is the First and Last Result of the Program",
"@if(thisVariableDoesntEvenExists)": "This input is never used"
}
}
Download examples/Examples.IfBypass.fjson and execute to see the results:
$ firejs Examples.IfBypass.fjson
"This is the First and Last Result of the Program"
@unless
work in the same way than @if
except that it executes and returns the input only if the operand is evaluated as false.
{
"name": "Examples.UnlessLastResult",
"json": {
"@set(chuckIsHere)": false,
"@unless(chuckIsHere)": "Chuck Norris is not here, we are Safe!"
}
}
Download examples/Examples.UnlessLastResult.fjson and execute to see the results:
$ firejs Examples.UnlessLastResult.fjson
"Chuck Norris is not here, we are Safe!"
@unless
can also use the last result as operand if no hint is given.
Due the flexibility of the Javascript object-system, fire.js behaves in certain ways when comparison expressions like @equals and @notEquals requires to compare the values of a given input(mostly known as operand). Internally fire.js will try to convert anything to an array of comparable values before execute any comparison and it follows some simple rules:
- the comparable values of any array it's the array itself
- the comparable values of null is an empty array
- the comparable values of undefined is an empty array
- the comparable values of an empty object is an empty array
- the comparable values of any object are all the values of the first-level properties
- the comparable values of any boolean is an empty array
- the comparable values of any string are all the chars in the String
Returns true if all the comparable values are equal, otherwise returns false.
{
"name": "Examples.Equals",
"json": {
"@equals": [10,"10"]
}
}
Download examples/Examples.Equals.fjson and execute to see the results:
$ firejs Examples.Equals.fjson
true
A hint with 'strict' can enforce the expression to compare types too, by default this is turned off. In the following example you will see how using strict
10 as a number and 10 as a string are no longer equals since the type comparison is taking place:
{
"name": "Examples.EqualsStrict",
"json": {
"@equals(strict)": [10,"10"]
}
}
Download examples/Examples.EqualsStrict.fjson and execute to see the results:
$ firejs Examples.EqualsStrict.fjson
false
If the operand doesn't have at least two comparable values it will return undefined
.
@notEquals
is the totally opposite to @equals
, it returns true if all the comparable values are different.
{
"name": "Examples.NotEquals",
"json": {
"@notEquals": [10,13]
}
}
Download examples/Examples.NotEquals.fjson and execute to see the results:
$ firejs Examples.NotEquals.fjson
true
Just like @equals
a hint with 'strict' can enforce the expression to compare types too, by default this is turned off. Also If the operand doesn't have at least two comparable values it will return undefined
.
{
"name": "Examples.TheFruitsJoy",
"json": {
"@set(fruits)": ["Orange", "Orange"],
"@equals": {
"@get(fruits)": null
},
"@if": "Yay, We have different fruits!",
"@unless": "Boring, Same fruits..."
}
}
Download examples/Examples.TheFruitsJoy.fjson and execute to see the results:
$ firejs Examples.TheFruitsJoy.fjson
"Yay, We have different fruits!"
Thanks for Reading. Please take your time to read other tutorials.
- Johan(author) - [email protected]
If you find any error in this tutorial(misspelling errors, syntax errors, etc) please send me an email with your corrections, I'll be more than happy to add you in this list.