Understanding operators and methods

When working with data objects in programming, operators and methods are essential tools for data manipulation and transformation.

Operators are symbols or keywords used to perform operations on data. They allow you to compare values, perform arithmetic calculations, and combine logical conditions. For example, using operators, you can check if a user is active, calculate a new balance, or determine if multiple conditions are met.

Methods, on the other hand, are functions that operate on data objects to perform specific tasks. These tasks range from modifying strings, handling arrays, and performing date calculations to transforming numerical values. Methods enable you to apply more complex logic and transformations to your data, enhancing the flexibility and power of your workflows.

Understanding the distinction and proper use of operators and methods upgrades your expression writing abilities in Cargo.


Operators

Logical Operators

Cover image

The main logical operators that users are likely to encounter are the AND (&&) and OR (||) operators.

These allow for the synthesis of one or multiple conditions into a logic gate.

For instance, evaluating whether a user is both active and possesses a score exceeding a threshold involves an AND operation.

AND operation

{
  {
    userInfo.isActive && userInfo.score > 50;
  }
}

This expression demands both conditions to be true for the overall expression to return true.

Conversely, the OR operator would require just one of the conditions to be met for the expression to return true.

OR operation

{
  {
    userInfo.isVerified || userInfo.balance > 100;
  }
}

The above expression checks if the user is either verified or has a balance greater than 100. If either condition is met, the expression returns true.


Operators

Comparison Operators

These operators allow the user to compare values, which is useful for creating conditional logic in workflows.

The strict equality (===) and inequality (!==) operators check for the equivalence or divergence of values, including their types.

It is important to note that the strict equality/inequality operators are slightly different from the loose equality/inequality operators (==, !=), which do not check for type. Moreover, neither of these operators should be confused with the assignment operator (=). The assignment operator is used to assign a value to a variable, while the equality operators are used to compare values. There will always never be an instance where the assignment operator will be use inside an expression, except for writing custom javascript recipes.

Strict equality check

{
  {
    nodes.userInfo.isActive === true;
  }
}

This expression checks if the user's active status is explicitly true.

Strict inequality check

{
  {
    nodes.userInfo.balance !== 100;
  }
}

This expression checks if the user's balance is not equal to 100.


Operators

Artihmetic operators

Arithmetic operators are essential for performing mathematical operations on numerical data.

These operators allow us to manipulate and transform numerical values stored within objects or variables.

For instance, the addition + operator adds two numbers together.

See the reference section for more examples of arithmetic operators supported in Cargo.

Example of adding to a numerical value

{
  {
    nodes.userInfo.balance + 79.5;
  }
}

This expression increases the 'balance' by 79.50. If the initial balance was 120.50, the new balance would be 200.00.


Methods

String Types

Cover image

The manipulation of textual data through methods such as concat(), split(), and toLowerCase() enables a user to handle strings within workflows.

Methods for string manipulation include accessing specific characters, understanding character encoding, and seamlessly combining strings, and so on.

Concatenate first and last names

{
  {
    nodes.userInfo.firstName.concat(" ", nodes.userInfo.lastName);
  }
}

This expression concatenates the 'firstName' and 'lastName' fields to form a full name, such as 'Alex Johnson'.

Convert to lowercase

{
  {
    nodes.userInfo.firstName.toLowerCase();
  }
}

This expression converts the 'firstName' to lowercase, resulting in 'alex'.

Split string

{
  {
    nodes.userInfo.emails[0].split("@")[0];
  }
}

This expression splits the first email address at the '@' symbol and returns the part before it, such as ;john.doe'.

See the reference section for more examples of string methods supported in Cargo.


Date manipulation

Dates

Cover image

Date objects play a pivotal role in many workflows, whether for comparing two dates, calculating differences, or setting specific timestamps for events.

Ensuring that date items are in the correct format before manipulation is essential to prevent errors and ensure accurate results.

For instance, if you're working with date strings, ensure they conform to a recognized format like ISO 8601 (YYYY-MM-DD) before converting them into Date objects for further manipulation.

The expression template supports such methods.

Example userInfo data object with dates

{
  "userInfo": {
    "lastLoginDate": "2024-06-21T09:59:32.076Z",
    "registrationDate": "2024-06-21T09:59:32.076Z"
  }
}

The example above represents a JSON object containing the lastLoginDate and registrationDate of a user.

Calculate the difference in days between two dates

{
  {
    (new Date() - new Date(nodes.userInfo.lastLoginDate)) /
      (1000 * 60 * 60 * 24);
  }
}

This expression calculates the number of days since the last login by subtracting the lastLoginDate from the current date and dividing by the number of milliseconds in a day.