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 the random number [1–100]

1
Math.floor(Math.random()*100)
Copied!

Changing the number of digits to appear after the decimal point

1
parseFloat('{{decimal_value}}').toFixed(2) // 2 digits after the point
2
//
3
// e.g.
4
// parseFloat('12.438324').toFixed(2) returns 12.43
Copied!

πŸ”€ Dealing with strings

Converting multiline text to an array of lines

For instance, you get {{text}} field (type of string) from a Telegram bot in several lines, and you want to process the lines separately. Here is the way:
1
'{{text}}'.replace(/\n/g, ',')
2
// this is string with lines, separated by commas (you may use any other character)
3
// in this way arrays in the platform are stored
4
// e.g.
5
// {{text}} =
6
// Good fortune? The fact is
7
// The more that you practise
8
// The harder you sweat
9
// The luckier you get
10
//
11
// result will be:
12
// Good fortune? The fact is,The more that you practise,The harder you sweat,The luckier you get
13
​
14
'{{text}}'.replace(/\n/g, ',').split(',') // this returns js-array
15
​
16
'{{text}}'.replace(/\n/g, ',').split(',')[0] // this returns the first line
17
'{{text}}'.replace(/\n/g, ',').split(',')[1] // this returns the second line
Copied!

Getting md5 hash

1
$D.md5('{{string}}')
2
//$D.md5('12345') returns 827ccb0eea8a706c4c34a16891f84e7b
Copied!

πŸ›’ Dealing with arrays

Directual stores array and arrayLink types as strings
1
'{{array}}'.split(',')
2
// {{array}} is a field with Directual type array
3
//
4
// e.g. {{array}} = 1,-2,a
5
// '{{array}}'.split(',') returns [1,-2,a]
Copied!

Counting the elements of an array

1
// basically, the expression is
2
'{{array}}'.split(',').length
3
// {{array}} is a field with Directual type array
4
​
5
// BUT! if {{array}} is empty, the expression above returns 1,
6
// so the expression should be the following:
7
'{{array}}' ? '{{array}}'.split(',').length : 0
Copied!

Getting the element from array with number N

1
'{{array}}'.split(',')[N]
2
// {{array}} is a field with type Array.
3
//
4
// e.g. {{a}} = a,b,c
5
// '{{a}}'.split(',')[1] returns b
Copied!

Getting random element from array

1
'{{array}}'.split(',')[Math.floor(Math.random()*'{{array}}'.split(',').length)]
2
// here we counts N = number of elements in array,
3
// get the random number [0–N] and pick the random element
Copied!

Adding an element into array, having avoided duplication

$D.concat is a specific Directual JS-function​
1
$D.concat('{{array}}', 'new element')
2
// or
3
$D.concat('{{array}}', '{{other_field}}')
4
// even or
5
$D.concat('{{array}}', '{{other_field_1}},{{other_field_2}}')
6
//
7
// e.g.
8
// $D.concat('', '3') returns 3
9
// $D.concat('1,2', '1,2,3') returns 1,2,3
10
// $D.concat('1,2,3', '3,4') returns 1,2,3,4
11
// $D.concat('1,2,3', '3') returns 1,2,3
Copied!
Example of applying $D.concat in 'Edit object' scenario step

Removing an element from array

$D.splice is a specific Directual JS-function​
1
$D.splice('{{array}}', 'removed element')
2
// or
3
$D.splice('{{array}}', '{{other_field}}')
4
// even or
5
$D.splice('{{array}}', '{{other_field_1}},{{other_field_2}}')
6
//
7
// e.g.
8
// $D.splice('', '3') returns ''
9
// $D.splice('1,2', '1,2,3') returns 3
10
// $D.splice('1,2,3', '3,4') returns 1,2
11
// $D.splice('1,2,3', '3') returns 1,2
Copied!

Applying array iterator 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.
1
// e.g. we want to add ' is the best' to every element of {{array}}.
2
// Here is the expression for that:
3
'{{array}}'.split(',').map(function(element)
4
{return element + ' is the best'}).join(',')
5
6
// let {{array}} was "Jhon,Ivan,Anna"
7
// the expression will return
8
// Jhon is the best,Ivan is the best,Anna is the Best
Copied!

Applying array iterator reduce() for calculating average

JS iterators are more applicable in JS SDK step. However, .reduce iterator can be useful in regular steps as well.
1
"{{array}}".split(",")
2
.reduce(function(acc, curr)
3
{return parseInt(acc) + parseInt(curr)})
4
/ "{{array}}".split(",").length
5
6
// e.g. for {{array}} == 1,2,5,0,-4,12
7
// that expression returns 2.666667
Copied!

πŸ“… Dealing with dates

In Advanced section of App settings you can choose your Time zone β€” if affects your operations with date functions like today or now
Setting up a time zone for your app

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()
1
(new Date()).toISOString()
2
// returns current Coordinated Universal Time (UTC)
3
// (new Date()).toISOString() === now === moment().toISOString()
Copied!

Converting date format

Learn more about date formatting​
$D.date.format returns string data, this you can't save the result into a field type of date
1
$D.date.format('{{date_1}}','MMMM dd, YYYY')
2
//
3
// e.g. $D.date.format('2020-05-20T18:14:11.000Z','MMMM dd, YYYY')
4
// returns May 20, 2020
5
//
6
$D.date.format($D.date.new(),'dd/MM/YYYY')
7
// returns today in a certain format e.g. 07.01.2021
Copied!

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.
1
((new Date('{{date}}')).getTime()/1000).toString()
2
//
3
// e.g {{date}} == 2020-11-12T00:00:00
4
// ((new Date('{{date}}')).getTime()/1000).toString() returns 1605139200 seconds
Copied!

Parsing date

1
$D.date.parse('{{formatted_date}}',dateFormat)
2
//
3
// e.g. $D.date.parse('10-03-2020','dd-MM-YYY') returns 2020-03-10T00:00:00.000Z
Copied!
You can compose an expression using both $D.date parse and $D.date.format to convert a format date from one to another:
1
$D.date.format($D.date.parse('10-03-2020','dd-MM-YYY'),'MMMM dd, YYYY')
2
// here we've converted 10-03-2020 into March 10, 2020
Copied!

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
1
//adding {{minutes}} minutes
2
(new Date((new Date('{{date}}')).getTime() + ('{{minutes}}' * 60 * 1000))).toISOString()
3
​
4
//adding {{hours}} hours
5
(new Date((new Date('{{date}}')).getTime() + ('{{hours}}' * 60 * 60 * 1000))).toISOString()
6
​
7
//adding {{days}} days
8
(new Date((new Date('{{date}}')).getTime() + ('{{days}}' * 60 * 60 * 24 * 1000))).toISOString()
9
​
10
//adding {{weeks}} weeks
11
(new Date((new Date('{{date}}')).getTime() + ('{{weeks}}' * 7 * 60 * 60 * 24 * 1000))).toISOString()
Copied!

Creating a unique object for each day

You may need to aggregate some statistics during the day in a single object (for example, count the quantity of orders or users). The simple way to have such an object is to create it with ID = date of the day in some format. Here is the example of an expression for generating such day-unique ID:
1
$D.date.format($D.date.new(),'dd-MM-YYYY')
2
// for todays object
3
​
4
$D.date.format('{{today_date}}','dd-MM-YYYY')
5
// where {{today_date}} is a type of date field
Copied!
An example of 'Create object' step for creating a 'daily-unique' object
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

For solving that we have to apply getTime() JS-function. It deals with Date JS-object and returns Milliseconds since Epoch time. The steps are the following:
    Getting a Date JS-object applying new Date('{{date}}') to the field {{date}} type of date
    Getting 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.
1
Math.floor((($D.date.new().getTime() -
2
(new Date('{{date}}')).getTime()))/(1000*60*60*24))
3
// Calculating the number of days between the current moment and {{date}}
4
​
5
Math.floor((((new Date('{{date_1}}')).getTime() -
6
(new Date('{{date_2}}')).getTime()))/(1000*60*60*24))
7
// Calculating the number of days between {{date_1}} and {{date_2}}
Copied!
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:
1
moment("{{date}}").add(10, 'days').format('DD-MM-YYYY')
2
​
3
// e.g {{date}} == 2020-11-12T00:00:00
4
// expression will return 22-11-2020
Copied!

πŸ₯· 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

1
"{{text}}".replace(/one/gm, "two")
2
// repplaces all the words "one" in the text to "two"
Copied!

Extract

1
/\+\d{11}/gm.exec("{{text}}") // returns an array of phone numbers found in text
2
/\+\d{11}/gm.exec("{{text}}")[0] // the first element of an array
3
/\+\d{11}/gm.exec("{{text}}").join(",") // converts JS-array into Directual-array
Copied!
Last modified 14d ago