Conditionals
CONDITIONALs are used for either deciding whether to set a variable, as well as some flow control such as whether to track quick cart.
{
"lookAt": LOOKUP,
"tests": TESTS
}
LOOKUP
This gets a value to test against. Please see Lookups section.
TESTS
A list of tests to pass for the conditional to pass.
This can include complex statements such as "IF TestA and TestB or TestX and TestY". This is accomplished using nested arrays.
"tests": [
[ TestA, TestB ],
[ TestX, TestY ]
]
Each test is formed as follows:
{
"type": TEST_TYPE,
"op": TEST_OPERATOR,
"rhs": TEST_RHS,
"params": [] // optional
}
A simple test that checks if the LOOKAT is equal to "/":
{
"type": "op",
"op": "=",
"rhs": "/"
}
More comlex tests requrie parameters. These are passed through as an array.
{
"type": "indexOf",
"params": ["/category/"]
"op": ">",
"rhs": 0,
}
Shorthand Conditionals
We can simplify the tests by using a string instead of an object. The magic "$value" is used in the string to represent the LOOKAT value. For the above examples:
"tests": "$value = '/'"
And:
"tests": "$value.indexOf('/category/') > 0"
We can also do use "and" and "or" in shorthand like this:
"tests": "TestA && TestB || TestX && TestY"
Complex Example
"tests": [
[
{
"type": "op",
"op": "=",
"rhs": "/"
}
],
[
{
"type": "indexOf",
"params": ["/category/"]
"op": ">",
"rhs": 0,
}
]
]
Shorthand:
"tests": "$value = '/' || $value.indexOf('/category') > 0"
TEST_TYPE
- op : Simple test for equality
- indexOf : Calling indexOf on strings or arrays
- length : Get the length property of strings or arrays
TEST_OPERATOR
- = : Equal to
- != : Not equal to
- > : Larger than
- >= : Larger than or equal to
- < : Smaller than
- <= : Smaller than or equal to
TEST_RHS
Either a number, or a string enclosed in single quotes.
Multi-lookup conditionals
Generally conditionals can only use a single lookup, with multiple tests. If we need to test over multiple lookups it's a little more complex, and is usable in a few different ways.
Single array: [ test AND tests ]
You can supply a single dimensional array which will run all tests where all must be true; an AND test.
[
{
"lookAt": "$('#productHeader') | text,trim",
"tests": "$value.length > 0"
},
{
"lookAt": "$('#sm_price')",
"tests": "$value.length > 0"
}
]
The above test will be true if both a product header AND a product price element exist on the page
Multi array: [ [ test AND test ] OR [ test AND test ] ]
If you supply a multidimensional array
[
{
"lookAt": "$('meta[property=\"og:type\"]') | content,trim",
"tests": "$value == 'page'"
}
],
[
{
"lookAt": "$('#productHeader') | text,trim",
"tests": "$value.length > 0"
},
{
"lookAt": "$('#sm_price')",
"tests": "$value.length > 0"
}
]
The above will be true if:
- The page has a meta property 'og:type' set to 'page'
OR
- Both a product header AND a product price element exist on the page
Reversing Logic
Sometimes you may need a test that is [ [ test OR test ] AND [ test OR test ] ]. To get behavior you will need to supply an object with "reverseLogic" and "ifs" properties.
- reverseLogic: A Boolean that if true will reverse the logic.
- ifs: The same as single or multi dimensional array lookup conditionals above
"reverseLogic": true,
"ifs": [ [], [] ]
}