Handlebar Helper Function: Compare
Compares two values using a specified operator. The operation supports both numeric and string comparisons, and automatically determines the appropriate comparison type.
| Parameter | Type | Description |
|---|---|---|
leftValue | any | Required. The left operand for comparison |
operator | string | Required. The comparison operator to use. Accepted values:
|
rightValue | any | Required. The right operand for comparison |
The operation returns a boolean value (true or false) based on the result of the comparison.
The compare operation uses logical rules to evaluate the relationship between the leftValue and rightValue properties, according to the selected operator. It examines the types and values of both operands and determines the appropriate comparison strategy.
If the operator is == or !=, the operation follows these rules:
- If both values are
null, they're considered equal and the operation returnstrue. If only one value isnull, the operation returnsfalse. - If either value is a boolean, both are coerced using language truthiness rules and compared as booleans. For example, the boolean
trueis equal to the string"true". - If either value is a date or datetime value, both are parsed and compared as dates.
- If either value is number-like, both are converted to numbers and compared numerically. For example, the string
"5.0"is equal to the number5. - If either value is a string that can’t be converted to a number, the operation falls back to case-sensitive string comparison. For example, the string
"Hello"isn’t equal to the string"hello".
If the operator is >, >=, <, or <=, the operation follows these rules:
- If either value is a number or is convertible to a number, the operation attempts to compare them numerically. For example,
{{compare "5.5" ">" 3}}returnstrue. - If either value is a date or datetime value, both are parsed and compared as dates.
- If either value is a string that can't be converted to a number or date, the operation performs an alphabetic, case-sensitive comparison. For example,
{{compare "apple" "<" "banana"}}returnstrue.