The mabl String template can be used to create strings from constant values, random values, variables, and/or mathematical expressions. mabl supports certain macros in the string templates to generate random strings and also supports embedding variable values into the string template. The macros and variables are delimited by double curly braces.
Variables in string templates are also prepended by @
. Here are some examples of valid expressions using variables that can be used for string templates:
{{@variable}}
Viewed {{@two}} elements
Macros are followed by a colon and a number, where the number indicates the number of random characters to be created. Here is the list of supported macros that can be used for string templates:
alpha
- upper and lower case lettersalphaLower
- lower case letters (a-z)alphaUpper
- upper case letters (A-Z)digit
- numbers (0-9)alnum
- alphanumeric (i.e. alpha + digit)
Here are some examples of valid expressions using macros:
{{alpha:3}}
abc{{@alphaLower:4}}
ABC{{@alphaUpper:5}}
123-{{@digit:4}}
random{{alnum:6}}
mabl can also generate realistic random data using the string template. The random data expressions have to be enclosed in double curly braces and have to be prepended by "fake.". The syntax for this is in the form {{fake.type.subtype}}
, e.g. {{fake.name.lastName}}
. Below are some examples of how to use the string template to create random data.
Random monetary values can also be generated as well:
Even random titles can be created:
Below are some of the more commonly used expressions for creating random data:
{{fake.name.firstName}}
Random first name
"Garfield"
{{fake.name.lastName}}
Random last name
"Larson"
{{fake.address.streetAddress}}
Random street address
"840 Roberts Burg"
{{fake.address.city}}
Random city name
"Manteton"
{{fake.address.state}}
Random US state name
"North Carolina"
{{fake.address.zipCode}}
Random US zip code
"02160"
{{fake.address.countryCode}}
Random 2-letter country code
"TN"
{{fake.company.companyName}}
Random company name
"Sawayn Group"
{{fake.phone.phoneNumber}}
Random phone number in one of multiple formats
"1-968-628-9831 x738"
{{fake.phone.phoneNumberFormat}}
Random phone number in the format ###-###-####
"776-705-5652"
{{fake.lorem.text}}
Random filler text
"consequatur omnis neque"
{{fake.random.uuid}}
Random UUID
"71bcc5d3-7f24-4b08-bcf8-f9b8e0d6d913"
{{fake.finance.amount}}
Random financial amount
"17.78"
For the complete list of valid types and subtypes, visit the faker.js documentation.
Note: only the English locale is supported.
How to use faker.js documentation to build expression
faker.js documentation lists the valid types and subtypes in the form type.subtype
. mabl expects the expression to be in the form {{fake.type.subtype}}
.
mabl can perform basic math operations within the string template and save the results to a new variable or overwrite an existing variable. Formulating the expression is important for the correct results. The mathematical expressions must be enclosed by the double curly braces to be evaluated. Here are examples of how to create valid math expressions in the string template:
- Below is an example of using a
digit
macro within a math calculation. This will evaluate the macro (9) and add 100 to the macro.
- Math expressions will be evaluated under the PEMDAS rules.
Example:{{5 + 3 / 2}}
will evaluate to6.5
- Use parenthesis to evaluate math operations in the specified order.
Example:{{(5 + 3) / 2}}
will evaluate to4
- Concatenate strings such as
$
or%
to create a string value from the results with units.
Example:${{(5 + 3 ) / 2}}
will evaluate to$4
mabl also supports certain math functions to format the numerical result from a math expression. Below are the functions that are supported:
round(number, numberOfDecialPlaces)
- Rounds the result of a math calculation tonumberOfDecialPlaces
number of decimal places.
Javascript Floating Point Precision
Due to rounding errors stemming from how floating point numbers are stored, arithmetic expressions like 0.1 + 0.2 === 0.3
will return false
. To resolve this, the result needs to be rounded, e.g. round(0.1 + 0.2, 1)
or (0.1 + 0.2).toFixed(1)
.
abs(number)
- Returns the absolute value of a math calculation
Example:{{round(1 / 3, 2)}}
will evaluate to0.33
Updated 2 months ago