Filters
Filters are used to both extract data from "selector" TO_TYPEs and transform values. Some work only on strings, and others only on jQuery elements.
"filters": [
{
"type": "text"
},
{
"type": "trim"
},
]
This can be written as a shorthand as follows:
"filters": ["text", "trim"]
Or simply as:
"filters": "text,trim"
Note if you supply a filter as a string, it will be automatically converted as "type": "[string]", so you can mix simple and more complex filters in the array, but not if supplied as a comma separated string.
Filters can also be set on LOOKUP shorthand using a pipe as follows. Note that any space before or after the pipe is ignored.
{
"to": "$('#productName') | text,trim"
}
jQuery elements
- text: $el.text() - text of an element
- first: $el[0] - first HTML element
- src: $el.attr('src') - SRC of an element
- href: $el.attr('href') - HREF of an element
- class: $el.attr('class') - CLASS(es) of an element
- content: $el.attr('content') - CONTENT attribute of an element
- value: $el.val() - value of an input
- display: $el.css('display') - the CSS "display" property ie "block" or "none"
jQuery element Filters: Complex
Attr
- attr: $el.attr(params.attr)
"filters": {
"type": "attr",
"params": {
"attr": "src"
}
}
String Filters
- trim: trim whitespace from string
- lowercase: lowercase string
- uppercase: uppercase string
- simple: get a string with only letters A through Z, numbers, hyphens and underscores.
- number: converts a string to a number,
- currency: converts a string to a number, taking into account the config.currencyDecimalDefault
- decode: decodes a string using encodeURIComponent
String Filters: Complex
Match
- match: string.match(/([0-9])/)
Returns a string using regular expressions that matches a part of another string. For example, the below filter is used to get a product id where window.location.href would be something like:
http://mystore.com/products/pid-0349328.aspx
"filters": {
"type": "match",
"params": {
"match": "\/pid-([0-9]+)\.aspx"
}
}
Concat
- concat: add a prefix and/or postfix to a string
"filters": {
"type": "concat",
"params": {
"prefix": LOOKUP, // optional
"postfix": LOOKUP // optional
}
}
Note that prefix and postfix are optional. You can supply one or both.
In this example, if our value starts as "HELLO" we end up with "START_HELLO_END".
"filters": {
"type": "concat",
"params": {
"prefix": "START_",
"postfix": "_END"
}
}
Split
- split: splits a string. If you specify an element that element is returned from the array, otherwise the whole array is returned.
"filters": {
"type": "split",
"params": {
"on": ":",
"element": 0 // OPTIONAL
}
}
If you do not specify any parameters, then it is split on an empty string, and the first element is returned.
Other Filters
Length
- length: gets an object's length. Works for strings and jQuery elements.
Op
- op: divide, multiply, plus, minus for numbers
Allows you to run operators as filters, including taking values from lookUps.
"filters": {
"type": "op",
"params": {
"op": OPERATOR,
"lookAt": LOOKUP
}
}
Valid operators:
- / : divide value by filter value
- * : multiply value by filter value
- + : add filter value to value
- - : minus filter value from value
In this example we set a product price using a line item lookup, divided by the quantity of that line item.
"filters": ["text", "number", {
"type": "op",
"params": {
"op": "/",
"lookAt": {
"type": "var",
"value": "$self.quantity"
}
}]
}
Note the use of "$self" here. It can be used only in a loop-item to refer to the current object that hold our scraped information. Please see Lookups.
abs
- abs: returns the absolute value of a number. That is, if it is negative it returns the positive value, otherwise the value itself. If it isn't a number it will simply return the value.
stash_get_product_id
- stash_get_product_id: Uses the current value to look up a previously saved product ID given the simple filtered product name. This relationship is saved if the config.stashProductIds is true.