Below is a list of Atomic functions you can use in script expressions when using MathJS. These are in addition to the functions that are available in the MathJS library.

If you are new to Atomic we highly recommend you use JavaScript and the corresponding Javascript cheat sheet. Support for MathJS is deprecated and planned to be phased out later in 2018.

Tip: If the parameter you are passing into a function is text (a string) wrap it in double quotation marks. E.g. goto("Page 2"). Numbers, variables and other data types do not need to be wrapped in quotation marks.

 

Navigating between pages

goto(pageName)

Sets the current page of the prototype to pageName, with no transition.

 

goto(pageName, delay)

Sets the current page of the prototype to pageName after delay milliseconds.
 

goto(pageName, motion)

Sets the current page of the prototype to pageName using motion with ease-in-out and 300ms duration.
  

goto(pageName, motion, easing)

Sets the current page of the prototype to pageName using motion with easing and 300ms duration.
  

goto(pageName, motion, easing, duration, delay)

Sets the current page of the prototype to pageName after delay milliseconds using the provided animation settings at the bottom of this page.
 

Examples:

goto("Profile screen")

goto("Home", "slide-right", "ease-out", 200, 50)

  

Getting and setting a container’s scroll position

scrollTo([pageName, elementName], positionX, positionY)

Sets the current scroll position of elementName. This works for scrolling Container elements. Note: negative values have no effect; the scroll position starts at 0 and increases as content is scrolled.
 

scrollTo([pageName, elementName], positionX, positionY, duration)

Same as above but animates the scroll position from the current position to the position set over duration in milliseconds. The animation uses an ease-in-out quadratic tween.

Example:

scrollTo(["Page 1", "Container"], 100, 300)

  

scrollPos([pageName, elementName])

Gets the current scroll position of elementName. Use this to find out if a scroll container has been scrolled and how far is has been scrolled. This works for scrolling Container elements. Note: if the element is not found, a -1  is returned.

Returns: number
 

resetScroll([pageName, elementName])

Sets the current scroll position of elementName to 0,0. This works for scrolling Container elements.

 

Accessing event information

An event context variable is available in script expressions for the following events: blur , focus , and keypress; which are available on text-based input elements. The variable is automatically available as ctx. This represents the event context, or in other words, a set of values specific to the element and event which triggered the script expression.

ctx.value

This is the current value of the text-based input element. Available on blur, focus, and keypress.

ctx.key

This is the string key code of the key that was pressed in a keypressevent.

Tip: Use log(ctx) in your script expression, then open the JavaScript console to see the output when triggering events.

  

Accessing dataset data

data(dataSetName)

Gets the data from dataSetName, which is the name of a dataset you have imported into your project. The returned array contains an entry for each row in the data set; each array entry contains an object having properties named after the data set columns. Arrays begin with an index of 1.
Returns: an array of objects

Example:

myArray = data("Contacts list spreadsheet")

Find out more about using data in your prototypes

  

Utility functions

contains(array, value)

Determines if value exists in array.
Returns: boolean
 

contains(string, value)

Determines if value exists in string.
Returns: boolean
 

contains(object, property)

Determines if property exists in object.
Returns: boolean

 

push(array, value)

Appends value to the end of array and returns a new arrray.
Returns: array

Example:

myNewArray = push(myArray, "Jane")

  

pop(array)

Removes the last value in array and returns a new array. Use last()  to get the last item in the array prior to calling pop()  if required.
Returns: array
 

last(array)

Returns the last value in array.
Returns: any
 

unshift(array, value)

Prepends value to beginning of array and returns a new array.
Returns: array
 

shift(array)

Removes first value in array and returns a new array. Use myArr[1] to get the first value in the array prior to calling shift() if required.
Returns: array
 

sort(array)

Sorts array items using their natural sort order and returns a new array.
Returns: array
 

sort(array, property)

Sorts array items, which are objects, using the value of property within those objects and returns a new array.
Returns: array
 

reverse(array)

Reverses the items in the array and returns a new array.
Returns: array
 

match(string, regex)

Retrieves the matches when matching string against regex.
Returns: array containing the entire match result and any parentheses-captured matched results; null if there were no matches.
 

splice(array, start, num)

Removes num items from array starting at index start and returns a new array.
Returns: array
  

splice(array, start, num, array)

Removes num items from array starting at index start and replaces those items with the items in the 4th array parameter and returns a new array.
Returns: array
 

limit(value, lower, upper)

Limits value to the lower and upper values (inclusive).
Returns: number
 

split(value, delimiter)

Splits the string value into an array using the given delimiter.
Returns: array

Example:

split("red,green,blue", ",")

//returns ["red","green","blue"]

  

join(array, value)

Creates a string by joining each item in the array using the provided string value.
Returns: string

Example:

join(["red","green","blue"], " & ")

// returns "red & green & blue"

  

sprintf(format, ...arguments)

vsprintf(format, [arguments])

sprintf and vsprintf let you format strings in powerful and versatile ways. See the documentation for more details: https://github.com/alexei/sprintf.js

 

Logging information to the JavaScript console

log(message)

log([msg1,msg2,msg3,...])

Prints a message to the JavaScript console in your browser, useful for tracking the value of variables over time and debugging complex scripts.

Tip: Even if you don't log messages you can still use of the JavaScript console to spot errors in your code, as any error encountered while previewing your prototype will be logged there.

 

Animation values

motion

  • "custom"
  • "slide-left"
  • "slide-right"
  • "slide-down"
  • "slide-up"
  • "push-left"
  • "push-right"
  • "float-left"
  • "float-right"
  • "fade"
  • "flip"

easing

  • "linear"
  • "ease-in"
  • "ease-out"
  • "ease-in-out"
  • "back"
  • "bounce"

duration

specified in milliseconds; a value of 0 prevents animation

delay

specified in milliseconds; default is 0

 

Handy Math.js functions

The Math.js library contains many useful functions for manipulating numbers, strings, arrays and other data types. Here's a few we find particularly handy: 

forEach(array, callback)

Iterates over all elements of an array, and executes the given callback function.
 

filter(array, test)

Filters the items in an array to only contain those that meet the test condition. 

 

Format a number as currency or date

Some JavaScript formatting functions are supported in script expressions. The most common example is currency. To convert a numeric value such as 25 in currency you could use the following to return: $25.00 :

var1 = 25
var1 = var1.toLocaleString("en-US", {style: "currency", currency: "USD", minimumFractionDigits: 2})

You could also store that function inside an advanced variable then call it from within a script expression. For example, with the date function above, you could create an advanced variable like this:

The value of the advanced variable formatCurrency in this example is:

formatCurrency(n) = n.toLocaleString("en-US", {style: "currency", currency: "USD", minimumFractionDigits: 2}) 

Now back in your prototype could you simply pass any numeric variable to the formatCurrency function and it would return the nicely formatted result. The expression you could use to do this is to return $25.00:

var1 = formatCurrency(var1)

 

Related articles:

Did this answer your question?