Skip to content

Int

The int() function parses a string number to an integer value.

If the string does not match any number, MARS will produce an error.

Basic usage

js
const parsedInt = int("42.23");
write('parsedInt', parsedInt);

Result:

json
{
   "parsedInt": 42
}

Error Example:

js
const parsedInt = int("hello42world");
write('parsedInt', parsedInt);

This will produce a MARS error if the string cannot be parsed to a number.

Str

The str() function tries to parse any argument to a string value.

Basic usage

js
const num = 42.23;
const parsedStr = str(num);
write('parsedStr', parsedStr);

Result:

json
{
   "parsedStr": "42,23"
}

Getting text content from files

You can also get text content of files loaded from another directory:

js
let myFile = fs.load('myfolder/fileName');
// mdFile.data will be a base64 string
let fileContent = str(myFile.data);
// fileContent will be actual text content of the file
write('fileContent', fileContent);

Base64

The base64 object offers encoding and decoding functions.

Method

base64.encode(value: string)

Encodes a string to Base64.

js
let someString = 'Bread';
let convertedToBase64 = base64.encode(someString);
write('result', convertedToBase64); 
// {"result": "QnJlYWQ="}

base64.decode(value: string, encoding: string)

Decodes a Base64-encoded string.

js
const someBase64 = 'QnJlYWQ=';
const someString = base64.decode(someBase64, 'UTF-8');
// returns "Bread"