Skip to content

Studio Script Reference: Utils ​

context.capitalize() ​

Converts the first character of string to upper case and the remaining to lower case.

Imported from Lodash.

Type: function([string='']).

Example:

js
context.capitalize('FRED');
// => 'Fred'

context.ceil() ​

Computes number rounded up to precision.

Imported from Lodash.

Type: function(number, [precision=0])

Example:

js
context.ceil(4.006);
// => 5
 
context.ceil(6.004, 2);
// => 6.01
 
context.ceil(6040, -2);
// => 6100

context.convert() ​

A chainable unit conversion utility.

→ convert-units documentation

Example:

js
// Convert a value between units:
context.convert(1).from('l').to('ml');
// => 1000

// Find the best human-readable unit:
context.convert(12000).from('mm').toBest();
// => { val: 12, unit: 'm', singular: 'Meter', plural: 'Meters' }

// List possible conversions for a unit:
context.convert().from('kg').possibilities();
// => ['mcg', 'mg', 'g', 'kg', 'oz', 'lb', ...]

context.floor() ​

Computes number rounded down to precision.

Imported from Lodash.

Type: function(number, [precision=0])

Example:

js
context.floor(4.006);
// => 4
 
context.floor(0.046, 2);
// => 0.04
 
context.floor(4060, -2);
// => 4000

context.gsap ​

The full GSAP (GreenSock Animation Platform) library, available for use in scripts.

→ GSAP documentation

Example:

js
context.gsap.to('#my-element', { opacity: 0, duration: 0.5 });

context.get() ​

Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.

Imported from Lodash.

Type: function(object, path, [defaultValue])

Example:

js
const object = { 'a': [{ 'b': { 'c': 3 } }] };

context.get(object, 'a[0].b.c');
// => 3

context.moment() ​

The full Moment.js library made available for use in scripts.

→ Moment.js documentation

context.padStart() ​

Pads string on the left side if it's shorter than length. Padding characters are truncated if they exceed length.

Imported from Lodash.

Type: function([string=''], [length=0], [chars=' '])

Example:

js
context.padStart('abc', 6);
// => '   abc'
 
context.padStart('abc', 6, '_-');
// => '_-_abc'
 
context.padStart('abc', 3);
// => 'abc'

context.parseInt() ​

Converts string to an integer of the specified radix. If radix is undefined or 0, a radix of 10 is used unless value is a hexadecimal, in which case a radix of 16 is used.

Imported from Lodash.

Type: function(string, [radix=10])

Example:

js
context.parseInt('08');
// => 8
 
context.map(['6', '08', '10'], _.parseInt);
// => [6, 8, 10]

context.replace() ​

Replaces matches for pattern in string with replacement.

Imported from Lodash.

Type: function([string=''], pattern, replacement)

Example:

js
context.replace('Hi Fred', 'Fred', 'Barney');
// => 'Hi Barney'

context.round() ​

Computes number rounded to precision.

Imported from Lodash.

Type: function(number, [precision=0])

Example:

js
context.round(4.006);
// => 4
 
context.round(4.006, 2);
// => 4.01
 
context.round(4060, -2);
// => 4100

context.set() ​

Sets the value at path of object. If a portion of path doesn't exist, it's created. Arrays are created for missing index properties while objects are created for all other missing properties.

Imported from Lodash.

Type: function(object, path, value)

Example:

js
const object = { 'a': [{ 'b': { 'c': 3 } }] };
 
context.set(object, 'a[0].b.c', 4);
console.log(object.a[0].b.c);
// => 4
 
context.set(object, ['x', '0', 'y', 'z'], 5);
console.log(object.x[0].y.z);
// => 5

context.sleep() ​

Put the JavaScript runtime to sleep before it carries out another action.

Important

Don't use sleep too often as it will block execution of upcoming code while sleeping.

Type: function(int): Promise

Example:

Remember to use await in front of the command, or your script won't actually wait for it to finish sleeping.

js
console.log(new Date())
// => Fri Sep 29 2022 09:36:12 GMT+0200
await sleep(10000) // wait 10 seconds
console.log(new Date())
// => Fri Sep 29 2022 09:36:22 GMT+0200

context.startCase() ​

Converts string to start case.

Type: function([string=''])

Example:

js
context.startCase('--foo-bar--');
// => 'Foo Bar'
 
context.startCase('fooBar');
// => 'Foo Bar'
 
context.startCase('__FOO_BAR__');
// => 'FOO BAR'

context.toLower() ​

Converts string, as a whole, to lower case just like String#toLowerCase.

Type: ([string=''])

Example:

js
context.toLower('--Foo-Bar--');
// => '--foo-bar--'
 
context.toLower('fooBar');
// => 'foobar'
 
context.toLower('__FOO_BAR__');
// => '__foo_bar__'

context.toUpper() ​

Converts string, as a whole, to upper case just like String#toUpperCase.

Type: function([string=''])

Example:

js
context.toUpper('--foo-bar--');
// => '--FOO-BAR--'
 
context.toUpper('fooBar');
// => 'FOOBAR'
 
context.toUpper('__foo_bar__');
// => '__FOO_BAR__'

context.uuid() ​

Create UUIDs.

Type: function()

Example:

js
console.log(context.uuid());
// => '11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000'