Tips for Dealing with Data
Cheat sheet for Makers 💁♂️
Don't forget to turn on ✅ Evaluating as JS expression option
🔢 Dealing with numbers and decimals
Getting a random number [1–100]
Changing the number of digits to appear after the decimal point
🔤 Dealing with strings
Converting multiline text to an array of lines
For instance, if you receive a {{text}}
field (type: string) from a Telegram bot in multiple lines and want to process each line separately, here's how you can do it:
Getting md5 hash
Generating a random string (password)
🛒 Dealing with arrays
Directual stores array and arrayLink types as strings
Converting a string (Directual array or arrayLink) into a JS-Array value
Saving a JS array to a Directual array
Counting elements of an array
Getting an element from an array with number N
Getting a random element from an array
Adding an element into an array while preventing duplicates
$D.concat
is a specific Directual JS-function
Removing an element from an array
$D.splice
is a specific Directual JS-function
Applying an array iterator map()
map()
JS iterators are more applicable in JS SDK step. However, .map
iterator can be useful in regular steps as well. This method adits all the elements of a given array.
Applying an array iterator reduce()
for calculating average
reduce()
for calculating averageJS iterators are more applicable in JS SDK step. However, .reduce
iterator can be useful in regular steps as well.
Arrays intersection (using lodash
)
lodash
)Lodash documentation on _.intersection
📅 Dealing with dates
Getting current time
The method (new Date()).toISOString()
returns a JS-date-object with current time. This method is more useful than now
, because we can compose complex JS-expressions in a one step using (new Date()).toISOString()
Converting date format
Learn more about date formatting
$D.date.format returns string data, so you can't save the result into a field type of date.
Converting into timestamp
Unix timestamp
format is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC.
Parsing date
You can compose an expression using both $D.date parse and $D.date.format to convert a format date from one to another:
Incrementing date
If you want to add minutes/hours/days/weeks to the date, here is the way
You can also use MomentJS for that
Creating a unique object for each day
You may need to aggregate statistics for the day into a single object (e.g., counting the quantity of orders or users). A straightforward approach is to create an object with an ID in the format of the date of the day. Here's an example expression for generating such a day-unique ID:
Don't forget to Save a link (write a link in the field) to the new object in order to be able to edit it. Note, that if you create an object with ID while there is an object with such an ID, nothing will be broken. You'll just create a link to that existing object.
Calculating the number of days between two dates
To solve this, use the getTime()
JS-function, which operates on the Date JS-object and returns Milliseconds since Epoch time. Here are the steps:
Getting a Date JS-object applying
new Date('{{date}}')
to the field{{date}}
type of dateGetting the value in milliseconds since Epoch time applying
.getTime()
Calculate the difference between two dates in milliseconds
Convert milliseconds into days, dividing the result to
(1000*60*60*24)
—1000 milliseconds in a one, second; 60 seconds in a minute; 60 minutes in an hour; and 24 hours in a day
N.B Here is a nice article referring to understanding Date and Time in JavaScript
Applying MomentJS
You can also use MomentJS library. Check out MomentJS documentation.
Example of using MomentJS:
🥷 Regular expressions
Directual JS-engine supports Regex-functions. Here are some widespread examples.
Tip: use https://regex101.com/ for composing a correct Regex-expression
Replace
Extract
Last updated