The comparison operator Expression

Hyperscript provides a rich set of comparison operators, including the usual symbolic ones (<, >, ==) along with English-language alternatives that make your scripts read more naturally.

In addition to the standard numeric and equality checks, hyperscript includes match, contain/include, starts with, ends with, is between, precedes/follows, and exists. Here's what each does:

Any comparison can be made case-insensitive by appending ignoring case:

  if name is "admin" ignoring case ...
  if title contains "hello" ignoring case ...
  if input matches "yes" ignoring case ...

Note that all comparison operators have the same precedence, but if multiple distinct operators are used the expression must be parenthesized to avoid ambiguity.

Because I is an alias for me, comparisons like I match .active or I contain #child work naturally as regular comparisons with I on the left side.

Examples

<div
  _="on click if <button/>.length > 1
                   log 'found multiple buttons!'"
>
  Find buttons
</div>

<div
  _="on click if I match .active
                   log 'I'm active!'"
>
  Check if active
</div>

<input _="on keyup
            if my value is 'quit' ignoring case
              put 'Goodbye!' into the next <output/>"/>

<input _="on keyup
            if my value starts with 'http'
              add .valid-url to me
            else
              remove .valid-url from me"/>

<input type="number" _="on change
            if my value as Int is between 1 and 100
              remove .error from me
            else
              add .error to me"/>

<div _="on click
         if I precede #footer
           log 'I am above the footer'">
  Check Position
</div>

<button _="on click
             get my value
             if it is a String!
               log 'got a non-null string'">
  Check Type
</button>

<!-- "is" as a boolean property test -->
<input type="checkbox" id="agree"/>
<button _="on click
             if #agree is not checked
               alert('Please agree to the terms')">
  Submit
</button>

Syntax

<expression> (< | <= | > | >= | == | ===) <expression>
<expression> is [not] [really] [equal to] <expression>
<expression> (equals | really equals) <expression>
<expression> is [not] empty
<expression> (matches | does not match) <expression>
<expression> (contains | does not contain) <expression>
<expression> (includes | does not include) <expression>
<expression> is [not] in <expression>
<expression> (starts with | does not start with) <expression>
<expression> (ends with | does not end with) <expression>
<expression> is [not] between <expression> and <expression>
<expression> (precedes | does not precede) <expression>
<expression> (follows | does not follow) <expression>
<expression> is [not] (a | an) <type-name>[!]
<expression> (exists | does not exist)
<comparison> ignoring case