The manuals explain how to use the different types of inline operations in Pen'script.
Pen'script now supports inline operations within curly brackets {}
. This guide provides a detailed explanation of the new features, including usage examples.
This manual will discuss the following operators:
- Pipe Operator
- Ternary Operator
- Concatenation
- Subtraction
- Multiplication and division
- Comparisons
- Operator Priorities
- Parentheses
- Test Case Examples
Pipe Operator
The pipe operator |
allows chaining multiple methods together. The syntax is:
This is equivalent to:
Examples:
Supported Methods:
- lowercase
- uppercase
- capitalize
- number
Note: The pipe operator cannot be used with methods that require additional parameters.
Ternary Operator
The ternary operator allows inline conditional logic. The syntax is:
If condition
is true, is_true
is returned; otherwise, else
is returned. If :
and else
are omitted, the default value is an empty string ''
.
Examples:
Concatenation
Use the +
operator to concatenate strings or add numbers. If both operands are numbers, addition is performed; if one or both are strings, concatenation is performed.
Examples:
Subtraction
Use the -
operator to subtract numbers. Subtraction is only allowed when both operands are numbers. If one or both are strings, the result is undefined
.
Examples:
Multiplication and division
Use the *
operator to multiply numbers and the /
operator to divide numbers.
Examples:
Comparisons
Comparisons can be done using >
, ≥
, <
, ≤
, ==
, or !=
.
Operator Priorities
Operators have the following priority order: ? > | > + > -
Parentheses
Use parentheses to change the order of operations.
Test Cases Examples
Here are some test cases to help you understand how these operations work:
Test Case
|
Expected Output
|
{season}
|
summer
|
{ season summer }
|
undefined
|
{season} cold {season}
|
summer cold summer
|
{'winter'}
|
winter
|
{season|uppercase}
|
SUMMER
|
{season|uppercase} hello
|
SUMMER hihi summer WINTER
|
{'winter'|uppercase}
|
WINTER
|
{'season is summer'|uppercase|lowercase}
|
season is summer
|
{'season is summer'|uppercase|lowercase|capitalize}
|
Season Is Summer
|
{'season IS SummER'|uppercase|lowercase|uppercase}
|
SEASON IS SUMMER
|
{'Spring'|uppercase}
|
SPRING
|
{'spring | uppercase'}
|
spring | uppercase
|
{"spring | uppercase"}
|
spring | uppercase
|
{test_function}
|
testing | uppercase
|
{test_function|uppercase}
|
TESTING | UPPERCASE
|
{number_str|number}
|
172
|
{number|number}
|
27
|
{'string'|number}
|
NaN
|
{ user.birth_date ? 1 : ‘oops’}
|
oops
|
{ user.birth_date ? 1}
|
|
{ true ? true ? 1 : 2 : 3 }
|
1
|
{ true ? false ? 1 : 2 : 3 }
|
2
|
{ false ? false ? 1 : 2 : 3 }
|
3
|
{'123' + '2132'}
|
1232132
|
{'? | 123 + 3232 | ? 2 2132'}
|
? | 123 + 3232 | ? 2 2132
|
{ 14 + 16 }
|
30
|
{ number + 28 }
|
55
|
{ number - 28 }
|
-1
|
{ 2 + 'hello' + 2 }
|
2hello2
|
{ 'hello' - 2 }
|
|
{'john ' + 'doe'|uppercase}
|
john DOE
|
{ user.family_name ? ‘yes’ + 'sure?'|uppercase : 'nope'}
|
yesSURE
|
{ user.birth_date ? ‘oops’ + 'sure'|uppercase : 'nope'}
|
nope
|
{ 100|number + '99'|number }
|
199
|