Module string_template_plus::transformers
source · Expand description
Transformers for the template
To apply a tranformer to a variable provide it after VAR_TRANSFORM_SEP_CHAR (currently “:”) to a variable template.
There are a few transformers available:
| Transformer | Arguments | Function | Example |
|---|---|---|---|
f [format_float] | [.]N | only N number of decimal | {“1.12”:f(.1)} ⇒ 1.1 |
case string_case | up | UPCASE a string | {“na”:case(up)} ⇒ NA |
case string_case | down | downcase a string | {“nA”:case(down)} ⇒ na |
case string_case | proper | Upcase the first letter | {“nA”:case(proper)} ⇒ Na |
case string_case | title | Title Case the string | {“na”:case(title)} ⇒ Na |
| calc | [+-*/^]N | Airthmatic calculation | {“1”:calc(+1*2^2)} ⇒ 16 |
| calc | [+-*/^]N | Airthmatic calculation | {“1”:calc(+1,-1)} ⇒ 2,0 |
| count | str | count str occurance | {“nata”:count(a)} ⇒ 2 |
repl replace | str1,str2 | replace str1 by str2 | {“nata”:rep(a,o)} ⇒ noto |
q quote | [str1] | quote with str1, or “” | {“nata”:q()} ⇒ “noto” |
| take | str,N | take Nth group sep by str | {“nata”:take(a,2)} ⇒ “t” |
| trim | str | trim the string with str | {“nata”:trim(a)} ⇒ “nat” |
You can chain transformers ones after another for combined actions. For example, count( ):calc(+1) will give you total number of words in a sentence.
Examples are in individual functions.
Functions
- Applies any tranformations to the variable, you can chain the transformers Called whenever you use
VAR_TRANSFORM_SEP_CHARto provide a transformer in the template. - Gets the bound of a rust range object
- Airthmatic calculations, the value needs to be float. e.g.
{val:calc(+1)}will add 1 to the value. The order of calculation is left to right. - Count the number of occurances of a pattern in the string. You can chain it with
calcto get the number of word like:{val:count( ):calc(+1)} - format the float (numbers). For example with
val=1.123,{val:f(2)}or{val:f(.2)}gives1.12 - Quote the text with given strings or
"" - Replace text in the string, by another text
- Format the string. Supports
up=> UPCASE,down=> downcase,proper=> first character UPCASE all others downcase,title=> title case according totitlecase::titlecase. e.g.{var:case(up)}. - Split the text with given separator and then take the Nth group
- Trim the given string with given patterns one after another