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 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:
'{{text}}'.replace(/\n/g,',') // this is string with lines, separated by commas (you may use any other character)// in this way arrays in the platform are stored// e.g.// {{text}} = // Good fortune? The fact is// The more that you practise// The harder you sweat// The luckier you get// // result will be:// Good fortune? The fact is,The more that you practise,The harder you sweat,The luckier you get'{{text}}'.replace(/\n/g,',').split(',') // this returns js-array'{{text}}'.replace(/\n/g,',').split(',')[0] // this returns the first line'{{text}}'.replace(/\n/g,',').split(',')[1] // this returns the second line
Directual stores array and arrayLink types as strings
Converting a string (Directual array or arrayLink) into a JS-Array value
'{{array}}'.split(',')// {{array}} is a field with Directual type array// // e.g. {{array}} = 1,-2,a// '{{array}}'.split(',') returns [1,-2,a]
Saving a JS array to a Directual array
constarray= ["a","b","c"] // standard JS array// if you want to save it to Directual array, you need:array.join(",")// as soon as Directual stores array and arrayLink types as strings
Counting elements of an array
// basically, the expression is'{{array}}'.split(',').length// {{array}} is a field with Directual type array// BUT! if {{array}} is empty, the expression above returns 1,// so the expression should be the following:'{{array}}'?'{{array}}'.split(',').length:0
Getting an element from an 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 a random element from an 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 an array while preventing duplicates
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'}).join(',')// let {{array}} was "Jhon,Ivan,Anna"// the expression will return// Jhon is the best,Ivan is the best,Anna is the Best
Applying an 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.
"{{array}}".split(",").reduce(function(acc, curr) {returnparseInt(acc) +parseInt(curr)}) /"{{array}}".split(",").length// e.g. for {{array}} == 1,2,5,0,-4,12 // that expression returns 2.666667
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()
(newDate()).toISOString()// returns current Coordinated Universal Time (UTC)// (new Date()).toISOString() === now === moment().toISOString()
$D.date.format returns string data, so you can't save the result into a field type of date.
$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//$D.date.format($D.date.new(),'dd/MM/YYYY')// returns today in a certain format e.g. 07.01.2021
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.
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:
$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
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 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() - (newDate('{{date}}')).getTime()))/(1000*60*60*24))// Calculating the number of days between the current moment and {{date}}Math.floor((((newDate('{{date_1}}')).getTime() - (newDate('{{date_2}}')).getTime()))/(1000*60*60*24))// Calculating the number of days between {{date_1}} and {{date_2}}
"{{text}}".replace(/one/gm,"two") // repplaces all the words "one" in the text to "two"
Extract
/\+\d{11}/gm.exec("{{text}}") // returns an array of phone numbers found in text/\+\d{11}/gm.exec("{{text}}")[0] // the first element of an array/\+\d{11}/gm.exec("{{text}}").join(",") // converts JS-array into Directual-array