Comparison Helper

Helper Description
opr Compare two values using operators.
eq Strict equality ===.
eqw Equality ==.
neq Strict inequality !==.
neqw Inequality !=.
lt Less than <.
gt Greater than >.
lte Less than or equal <=.
gte Greater than or equal >=.
ifx Imitates conditional operator ?:.
not Not !.
empty Check if it is empty.
notEmpty Check that it is not empty.
count Length of an array.
and Logical AND operation.
or Logical OR operation.
coalesce Returns first non-falsy value from list of parameters.
includes Check for a value inside an array.
regexMatch Returns true if the given str matches the given regex.

opr

Compare two values using operators.
Parameters:
  • val1: any
    First value to be compared with second.
  • opr: '=='|'==='|'!='|'!=='|'<'|'>'|'<='|'>='
    The operator to use. Operators must be enclosed in quotes: `">"`, `"="`, `"<="`, and so on.
  • val2: any
    Second value to be compared with first.
          // results in: false
hbs.compile("{{opr a '===' b}}")({"a":"3","b":3});

// results in: false
hbs.compile("{{#if (opr a '===' b)}}true{{else}}false{{/if}}")({"a":"3","b":3});

// results in: true
hbs.compile("{{opr a '==' b}}")({"a":"3","b":3});

// results in: true
hbs.compile("{{#if (opr a '==' b)}}true{{else}}false{{/if}}")({"a":"3","b":3});

// results in: true
hbs.compile("{{opr a '!==' b}}")({"a":4,"b":3});

// results in: true
hbs.compile("{{#if (opr a '!==' b)}}true{{else}}false{{/if}}")({"a":4,"b":3});

// results in: false
hbs.compile("{{opr a '!=' b}}")({"a":"3","b":3});

// results in: false
hbs.compile("{{#if (opr a '!=' b)}}true{{else}}false{{/if}}")({"a":"3","b":3});

// results in: true
hbs.compile("{{opr a '<' b}}")({"a":2,"b":3});

// results in: true
hbs.compile("{{#if (opr a '<' b)}}true{{else}}false{{/if}}")({"a":2,"b":3});

// results in: false
hbs.compile("{{opr a '>' b}}")({"a":2,"b":3});

// results in: false
hbs.compile("{{#if (opr a '>' b)}}true{{else}}false{{/if}}")({"a":2,"b":3});

// results in: true
hbs.compile("{{opr a '<=' b}}")({"a":2,"b":3});

// results in: true
hbs.compile("{{#if (opr a '<=' b)}}true{{else}}false{{/if}}")({"a":2,"b":3});

// results in: true
hbs.compile("{{opr a '>=' b}}")({"a":3,"b":3});

// results in: true
hbs.compile("{{#if (opr a '>=' b)}}true{{else}}false{{/if}}")({"a":3,"b":3});
        

eq

Determine whether or not two values are equal (===).
Parameters:
  • val1: any
    First value to be compared with second.
  • val2: any
    Second value to be compared with first.
          // results in: false
hbs.compile("{{eq a b}}")({"a":"3","b":3});

// results in: false
hbs.compile("{{#if (eq a b)}}true{{else}}false{{/if}}")({"a":"3","b":3});
        

eqw

Determine whether or not two values are equal (==) i.e weak checking.
Parameters:
  • val1: any
    First value to be compared with second.
  • val2: any
    Second value to be compared with first.
          // results in: true
hbs.compile("{{eqw a b}}")({"a":"3","b":3});

// results in: true
hbs.compile("{{#if (eqw a b)}}true{{else}}false{{/if}}")({"a":"3","b":3});
        

neq

Determine whether or not two values are not equal (!==).
Parameters:
  • val1: any
    First value to be compared with second.
  • val2: any
    Second value to be compared with first.
          // results in: true
hbs.compile("{{neq a b}}")({"a":4,"b":3});

// results in: true
hbs.compile("{{#if (neq a b)}}true{{else}}false{{/if}}")({"a":4,"b":3});
        

neqw

Determine whether or not two values are not equal (!=) weak checking.
Parameters:
  • val1: any
    First value to be compared with second.
  • val2: any
    Second value to be compared with first.
          // results in: false
hbs.compile("{{neqw a b}}")({"a":"3","b":3});

// results in: false
hbs.compile("{{#if (neqw a b)}}true{{else}}false{{/if}}")({"a":"3","b":3});
        

lt

Check for less than condition (a < b).
Parameters:
  • val1: any
    First value to be compared with second.
  • val2: any
    Second value to be compared with first.
          // results in: true
hbs.compile("{{lt a b}}")({"a":2,"b":3});

// results in: true
hbs.compile("{{#if (lt a b)}}true{{else}}false{{/if}}")({"a":2,"b":3});
        

gt

Check for greater than condition (a > b).
Parameters:
  • val1: any
    First value to be compared with second.
  • val2: any
    Second value to be compared with first.
          // results in: false
hbs.compile("{{gt a b}}")({"a":2,"b":3});

// results in: false
hbs.compile("{{#if (gt a b)}}true{{else}}false{{/if}}")({"a":2,"b":3});
        

lte

Check for less than or equals condition (a <= b).
Parameters:
  • val1: any
    First value to be compared with second.
  • val2: any
    Second value to be compared with first.
          // results in: false
hbs.compile("{{lte a '<=' b}}")({"a":2,"b":3});

// results in: false
hbs.compile("{{#if (lte a '<=' b)}}true{{else}}false{{/if}}")({"a":2,"b":3});
        

gte

Check for greater than or equals condition (a >= b).
Parameters:
  • val1: any
    First value to be compared with second.
  • val2: any
    Second value to be compared with first.
          // results in: false
hbs.compile("{{gte a '>=' b}}")({"a":3,"b":3});

// results in: false
hbs.compile("{{#if (gte a '>=' b)}}true{{else}}false{{/if}}")({"a":3,"b":3});
        

ifx

Helper to imitate the ternary '?:' conditional operator.
Parameters:
  • condition: boolean
    Satisfying condition for getting first value. Either true of false. (Required).
  • val1: any
    Value to return when the condition holds true.
  • val2: any
    Value to return when the condition is false (Optional).
          // results in: foo
hbs.compile("{{ifx true a b}}")({"a":"foo","b":"bar"});

// results in: bar
hbs.compile("{{ifx false a b}}")({"a":"foo","b":"bar"});
        

not

Logical NOT of any expression.
Parameters:
  • expression: any
    Any expression.
          // results in: false
hbs.compile("{{not true}}")();

// results in: false
hbs.compile("{{#if (not true)}}true{{else}}false{{/if}}")();

// results in: true
hbs.compile("{{not false}}")();

// results in: true
hbs.compile("{{#if (not false)}}true{{else}}false{{/if}}")();
        

empty

Check if it is empty.
If the value is an array, returns true if there are no elements.
If the value is a string, the leading and trailing spaces are trimmed and then checked.
Parameters:
  • val: any
    Character strings, arrays, objects, etc. to be checked.
          // If the value is an array.
// results in: true
hbs.compile("{{empty val}}")({"val":[]});

// results in: true
hbs.compile("{{#if (empty val)}}true{{else}}false{{/if}}")({"val":[]});

// results in: false
hbs.compile("{{empty val}}")({"val":["foo"]});

// If the value is a string.
// results in: false
hbs.compile("{{empty val}}")({"val":"Hello"});

// results in: true
hbs.compile("{{empty val}}")({"val":""});

// results in: true
hbs.compile("{{empty val}}")({"val":" "});
        

notEmpty

Check that it is not empty.
If the value is an array, returns true if there is an element.
If the value is a string, the leading and trailing spaces are trimmed and then checked.
Parameters:
  • val: any
    Character strings, arrays, objects, etc. to be checked.
          // If the value is an array.
// results in: false
hbs.compile("{{notEmpty val}}")({"val":[]});

// results in: false
hbs.compile("{{#if (notEmpty val)}}true{{else}}false{{/if}}")({"val":[]});

// results in: true
hbs.compile("{{notEmpty val}}")({"val":["foo"]});

// If the value is a string.
// results in: true
hbs.compile("{{notEmpty val}}")({"val":"Hello"});

// results in: false
hbs.compile("{{notEmpty val}}")({"val":""});

// results in: false
hbs.compile("{{notEmpty val}}")({"val":" "});
        

count

Determine the length of an array.
Parameters:
  • collection: any[]
    Array whose elements to be counted.
          // results in: 2
hbs.compile("{{count collection}}")({"collection":["foo","bar"]});
        

and

Returns the boolean AND of two or more parameters passed i.e it is true iff all the parameters are true.
Parameters:
  • args: ...any
    Any number of boolean parameters.
          // results in: true
hbs.compile("{{and a b}}")({"a":true,"b":true});

// results in: true
hbs.compile("{{#if (and a b)}}true{{else}}false{{/if}}")({"a":true,"b":true});

// results in: false
hbs.compile("{{and a b}}")({"a":false,"b":true});

// results in: false
hbs.compile("{{#if (and a b)}}true{{else}}false{{/if}}")({"a":false,"b":true});
        

or

Returns the boolean OR of two or more parameters passed i.e it is true if any of the parameters is true.
Parameters:
  • args: ...any
    Any number of boolean parameters.
          // results in: true
hbs.compile("{{or a b}}")({"a":true,"b":false});

// results in: true
hbs.compile("{{#if (or a b)}}true{{else}}false{{/if}}")({"a":true,"b":false});

// results in: false
hbs.compile("{{or a b}}")({"a":false,"b":false});

// results in: false
hbs.compile("{{#if (or a b)}}true{{else}}false{{/if}}")({"a":false,"b":false});
        

coalesce

Returns the first non-falsy value from the parameter list.
Checks for the first non-false parameter.
Parameters:
  • args: ...any
    Any number of parameters.
          // results in: foo
hbs.compile("{{coalesce a b c}}")({"a":"foo","b":"bar","c":"baz"});

// results in: bar
hbs.compile("{{coalesce a b c}}")({"a":"","b":"bar","c":"baz"});
        

includes

Returns true if the array contains a particular value, false if it does not.
Parameters:
  • collection: any[]
    The array.
  • val: any
    The value to search for.
  • strict: boolean
    FALSE for non-strict checking. TRUE by default.
          // results in: true
hbs.compile("{{includes collection a}}")({"collection":[1,2,3,4],"a":2});

// results in: true
hbs.compile("{{#if (includes collection a)}}true{{else}}false{{/if}}")({"collection":[1,2,3,4],"a":2});

// results in: false
hbs.compile("{{includes collection a}}")({"collection":[1,2,3,4],"a":10});

// results in: false
hbs.compile("{{#if (includes collection a)}}true{{else}}false{{/if}}")({"collection":[1,2,3,4],"a":10});

// results in: false
hbs.compile("{{includes collection a}}")({"collection":[1,2,3,4],"a":"3"});

// results in: false
hbs.compile("{{#if (includes collection a)}}true{{else}}false{{/if}}")({"collection":[1,2,3,4],"a":"3"});

// results in: true
hbs.compile("{{includes collection a strict}}")({"collection":[1,2,3,4],"a":"3","strict":false});

// results in: true
hbs.compile("{{#if (includes collection a strict)}}true{{else}}false{{/if}}")({"collection":[1,2,3,4],"a":"3","strict":false});
        

regexMatch

Returns true if the given str matches the given regex.
Parameters:
  • val: string
    The string against which to match the regular expression.
  • pattern: string
    The text of the regular expression.
  • flags?: string
    Regular expression flags, such as global and case-insensitive searches. The default is none (undefined).
          // results in: true
hbs.compile("{{regexMatch 'foobar' 'foo'}}")();

// results in: false
hbs.compile("{{regexMatch 'bar' 'foo'}}")();

// results in: false
hbs.compile("{{regexMatch 'foobar' '^foo$'}}")();

// results in: true
hbs.compile("{{regexMatch 'Visit Here' 'here' 'i'}}")();

// results in: false
hbs.compile("{{regexMatch 'Visit Here' 'here'}}")();

// results in: Match
hbs.compile("{{#if (regexMatch 'foobar' 'foo')}}Match{{/if}}")();