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]

Math.floor(Math.random()*100)

Changing the number of digits to appear after the decimal point

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

🛒 Dealing with arrays

Directual stores array and arrayLink types as strings

'{{array}}'.split(',')
// {{array}} is a field with Directual type array
//
// e.g. {{array}} = 1,-2,a
// '{{array}}'.split(',') returns [1,-2,a]

Counting the elements of an array

'{{array}}'.split(',').length
// {{array}} is a field with Directual type array

Getting the element from array with number N

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

Getting random element from array

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

Adding an element into array, having avoided duplication

$D.concat is a specific Directual JS-function

$D.concat('{{array}}', 'new element')
// or
$D.concat('{{array}}', '{{other_field}}')
// even or
$D.concat('{{array}}', '{{other_field_1}},{{other_field_2}}')
//
// e.g.
// $D.concat('', '3') returns 3
// $D.concat('1,2', '1,2,3') returns 1,2,3
// $D.concat('1,2,3', '3,4') returns 1,2,3,4
// $D.concat('1,2,3', '3') returns 1,2,3
Example of applying $D.concat in 'Edit object' scenario step

Removing an element from array

$D.splice is a specific Directual JS-function

$D.splice('{{array}}', 'removed element')
// or
$D.splice('{{array}}', '{{other_field}}')
// even or
$D.splice('{{array}}', '{{other_field_1}},{{other_field_2}}')
//
// e.g.
// $D.splice('', '3') returns ''
// $D.splice('1,2', '1,2,3') returns 3
// $D.splice('1,2,3', '3,4') returns 1,2
// $D.splice('1,2,3', '3') returns 1,2

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.

// e.g. we want to add ' is the best' to every element of {{array}}.
// Here is the expression for that:
'{{array}}'.split(',').map(function(element)
{return element + ' is the best'}).toString()

📅 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

$D.date is a specific Directual JS-function

The method $D.date.new 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 $D.date.new

$D.date.new()
// returns current Coordinated Universal Time (UTC)
// $D.date.new === new Date() === now

Converting date format

Learn more about date formatting

$D.date.format('{{date_1}}','MMMM dd, YYYY')
//
// e.g. $D.date.format('2020-05-20T18:14:11.000Z','MMMM dd, YYYY')
// returns May 20, 2020

Parsing date

$D.date.parse('{{formatted_date}}',dateFormat)
//
// e.g. $D.date.parse('10-03-2020','dd-MM-YYY') returns 2020-03-10T00:00:00.000Z

You can compose an expression using both $D.date parse and $D.date.format to convert a format date from one to another:

$D.date.format($D.date.parse('10-03-2020','dd-MM-YYY'),'MMMM dd, YYYY')
// here we've converted 10-03-2020 into March 10, 2020

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:

$D.date.format($D.date.new(),'dd-MM-YYYY')
// for todays object
$D.date.format('{{today_date}}','dd-MM-YYYY')
// where {{today_date}} is a type of date field
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.

Math.floor((($D.date.new().getTime() -
(new Date('{{date}}')).getTime()))/(1000*60*60*24))
// Calculating the number of days between the current moment and {{date}}
Math.floor((((new Date('{{date_1}}')).getTime() -
(new Date('{{date_2}}')).getTime()))/(1000*60*60*24))
// Calculating the number of days between {{date_1}} and {{date_2}}

N.B Here is a nice article referring to understanding Date and Time in JavaScript

🔡 Dealing with strings

Getting md5 hash

$D.md5('{{string}}')
//$D.md5('12345') returns 827ccb0eea8a706c4c34a16891f84e7b