Penscript Logic

Logic Functions

PenScript provides a variety of logic functions to evaluate expressions, handle conditional operations, and manipulate data dynamically. Below is an explanation of each function and how to use it.


:if :then :else

This function evaluates an expression in :if. If the expression returns a truthy value (e.g., a non-empty string or non-zero number), the value in :then is returned. Otherwise, the value in :else is returned.

Example:

{
  ":if": "{data.my_var}",
  ":then": "Condition is true!",
  ":else": "Condition is false."
}

Use Case: Execute different actions based on whether a condition is met.

:case :when

This function acts like a switch statement. It evaluates a value and returns the block associated with that value.

Example:

{
    ":case": "{data.my_key}",
    ":when": [
        [
            "val1",
            "Block for val1"
        ],
        [
            "val2",
            "Block for val2"
        ],
        [
            "val3",
            "Block for val3"
        ]
    ]
}

Use Case: Simplify multiple conditional evaluations by mapping values to specific outputs.

:defined

This function checks if an expression is defined (i.e., not null or undefined) and returns true if it is.

Example:

{
  ":defined": "{data.key}"
}

Result: true if {data.key} exists; false otherwise.
Use Case: Ensure a value is present before performing operations on it.

:cmp

Compares a value with another value using a specified comparison function.

Supported Comparison Functions:

  • :eq – Equal to
  • :gt – Greater than
  • :ge – Greater than or equal to
  • :lt – Less than
  • :le – Less than or equal to
{
  ":cmp": "{data.my_var}",
  ":eq": 2
}

Result: true if {data.my_var} equals 2.
Use Case: Perform flexible comparisons in your logic.

:in

Checks if a value exists within a list.

{
  ":in": ["val1", ["val1", "val2"]]
}

Result: true because val1 is in the list ["val1", "val2"].
Use Case: Validate membership in a predefined list.

:includes

Checks if a list contains a specific value.

{
  ":includes": [["val1", "val2"], "val1"]
}

Result: true because val1 is in the list ["val1", "val2"].
Use Case: Validate if a value is included in a dataset.

:intersects

Checks if at least one element from one list exists in another list.

{
  ":intersects": [["val1", "val2"], ["val1", "val3"]]
}

Result: true because both lists share val1.
Use Case: Identify overlapping values in two datasets.

:not

Reverses the boolean value of an expression.

{
  ":not": true
}

Result: false.
Use Case: Invert logic conditions.

:true

Checks if an expression is true.

{
  ":true": "{data.my_var}"
}

Use Case: Verify truthy expressions.

:false

Checks if an expression is false.

{
  ":false": "{data.my_var}"
}

:every

Checks if all expressions in a list return a truthy value.

{
  ":every": [
    {":defined": "{data.my_var}"},
    "{data.my_other_var}"
  ]
}

Result: true if all expressions are truthy.
Use Case: Validate multiple conditions simultaneously.

:some

Checks if at least one expression in a list returns a truthy value.

{
  ":some": [
    {":defined": "{data.my_var}"},
    "{data.my_other_var}"
  ]
}

Result: true if any expression is truthy.
Use Case: Validate if one of multiple conditions is met.

:coalesce

Returns the first defined value from a list.

{
  ":coalesce": [
    null,
    "{data.some_undefined_value}",
    "default value"
  ]
}

Result: "default value" (the first non-null/undefined value).
Use Case: Provide fallback values when primary values are missing.


These logic functions are versatile tools that enable you to create highly dynamic and intelligent workflows within Penbox. Use them to evaluate conditions, handle exceptions, and design robust processes!