Expressions allow you to work with your data in expressive ways. In this article we collect expression formatting and solutions that have been created by platform users and validated by the Betty Blocks services department.
Format phone number
Format a phone number to a certain format, in this format the first 3 letters are separated (the location code) and added in brackets ().
Input
Value:
function formatPhoneNumber() { // Return empty text if there is no phone number if (!"{{ office_phone }}") return ""; // Only get numbers from text const onlyNumbers = "{{ office_phone }}".replace(/[^0-9]/g, ""); // Format phone number like "(123) 456-7890" return "(" + onlyNumbers.substring(0, 3) +") " + onlyNumbers.substring(3,6) +"-" + onlyNumbers.substring(6, 10); }();
Variables:
{{ office phone }}
Output:
Output type: Text
Value: “(123) 456-7890”
Example:
Monday of week
Retrieve the date of a Monday from a week and year you provide.
Input
Value:
function getMondayOfWeek() { const date = new Date({{ year }}, 0, (1 + ({{ week_number }} - 1) * 7)); const offset = date.getTimezoneOffset(); return new Date(date.getTime() - (offset * 60 * 1000)).toISOString().split('T')[0]; }();
Variables:
{ year: 2024, week_number: 25 }
Output:
Output type: Text
Value: “2024-06-17”
This value can be saved in a date field and be used further in your application.
Get array element
Get a specific element from an array given an index (number).
Important to know that the index count starts from 0, therefore index 1 will return the second value from your array.
Input:
Value:
“{{ array }}“.split(“,”)[{{ index }}]
Variables:
{ array: [“Element 1", “Element 2”, “Element 3"], index: 1 }
Output:
Output type: Text
Value: “Element 2”
Filter record from collection
This filter expression is used to filter an element from a (custom) collection. In this example the output type array is used.
The expression itself is a good example of how you can deal with collections in the Expression step.
Input:
Value:
[{{ collection.map(item => item.id) }}].filter(item => item !== {{ record_id }})
Variables:
{ collection: [{ id: 1, title: “Element 1” }, { id: 2, title: “Element 2" }, { id: 3, title: “Element 3” }], record_id: 2 }
Output:
Output type: Array
Value: [{ id: 1, title: “Element 1" }, { id: 3, title: “Element 3” }]
Some custom data formatting steps have already been turned into a block, have a look at this list to see what steps are already available in the block store.
If you have any expression formatting you would like to report and add to the list, reach out to us via email: odin@bettyblocks.com