Skip to content

Commit

Permalink
support more comparison ops such as is equal to, `is really equal t…
Browse files Browse the repository at this point in the history
…o`, etc.
  • Loading branch information
1cg committed Jun 30, 2023
1 parent f1348d7 commit 6282add
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/_hyperscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -3818,7 +3818,15 @@
operator = "not empty";
hasRightValue = false;
} else {
operator = "!=";
if (tokens.matchToken("really")) {
operator = "!==";
} else {
operator = "!=";
}
// consume additional optional syntax
if (tokens.matchToken("equal")) {
tokens.matchToken("to");
}
}
} else if (tokens.matchToken("in")) {
operator = "in";
Expand Down Expand Up @@ -3847,8 +3855,20 @@
operator = ">";
}
} else {
operator = "==";
if (tokens.matchToken("really")) {
operator = "===";
} else {
operator = "==";
}
if (tokens.matchToken("equal")) {
tokens.matchToken("to");
}
}
} else if (tokens.matchToken("equals")) {
operator = "==";
} else if (tokens.matchToken("really")) {
tokens.requireToken("equals")
operator = "===";
} else if (tokens.matchToken("exist") || tokens.matchToken("exists")) {
operator = "exist";
hasRightValue = false;
Expand Down Expand Up @@ -3902,6 +3922,11 @@
} else if (operator === "!=") {
return lhsVal != rhsVal;
}
if (operator === "===") {
return lhsVal === rhsVal;
} else if (operator === "!==") {
return lhsVal !== rhsVal;
}
if (operator === "match") {
return lhsVal != null && sloppyMatches(lhs, lhsVal, rhsVal);
}
Expand Down
72 changes: 72 additions & 0 deletions test/expressions/comparisonOperator.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,53 @@ describe("the comparisonOperator expression", function () {
result.should.equal(true);
});

it("equals works", function () {
var result = evalHyperScript("1 equals 2");
result.should.equal(false);

var result = evalHyperScript("2 equals 1");
result.should.equal(false);

var result = evalHyperScript("2 equals 2");
result.should.equal(true);
});

it("is equal to works", function () {
var result = evalHyperScript("1 is equal to 2");
result.should.equal(false);

var result = evalHyperScript("2 is equal to 1");
result.should.equal(false);

var result = evalHyperScript("2 is equal to 2");
result.should.equal(true);
});

it("is really equal to works", function () {
var result = evalHyperScript("1 is really equal to 2");
result.should.equal(false);

var result = evalHyperScript("2 is really equal to 1");
result.should.equal(false);

var result = evalHyperScript("2 is really equal to '2'");
result.should.equal(false);

var result = evalHyperScript("2 is really equal to 2");
result.should.equal(true);
});

it("really equals works", function () {
var result = evalHyperScript("1 really equals 2");
result.should.equal(false);

var result = evalHyperScript("2 really equals 1");
result.should.equal(false);

var result = evalHyperScript("2 really equals 2");
result.should.equal(true);
});

it("is not works", function () {
var result = evalHyperScript("1 is not 2");
result.should.equal(true);
Expand All @@ -116,6 +163,31 @@ describe("the comparisonOperator expression", function () {
result.should.equal(false);
});

it("is not equal to works", function () {
var result = evalHyperScript("1 is not equal to 2");
result.should.equal(true);

var result = evalHyperScript("2 is not equal to 1");
result.should.equal(true);

var result = evalHyperScript("2 is not equal to 2");
result.should.equal(false);
});

it("is not really equal to works", function () {
var result = evalHyperScript("1 is not really equal to 2");
result.should.equal(true);

var result = evalHyperScript("2 is not really equal to 1");
result.should.equal(true);

var result = evalHyperScript("2 is not really equal to '2'");
result.should.equal(true);

var result = evalHyperScript("2 is not really equal to 2");
result.should.equal(false);
});

it("is in works", function () {
var result = evalHyperScript("1 is in [1, 2]");
result.should.equal(true);
Expand Down
8 changes: 8 additions & 0 deletions www/expressions/comparison-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ title: comparison operator - ///_hyperscript
<expr> == <expr>
<expr> === <expr>
<expr> is <expr>
<expr> is equal to <expr>
<expr> is really equal to <expr>
<expr> is not <expr>
<expr> is not equal to <expr>
<expr> is not really equal to <expr>
<expr> equals <expr>
<expr> really equals <expr>
<expr> is empty
<expr> is not empty
I match <expr>
Expand All @@ -45,6 +51,8 @@ I do not include <expr>

Many comparison operators are similar to comparison operators in javascript. In addition to the usual comparison operators, hyperscript includes the english terms `is` and `is not` for `==` and `!=` respectively.

The `really` modifier switches a comparison to use `===` or `!==`, depending on what it modifies.

Hyperscript also includes four additional operations, `match`, `contain`, `include`, `exists` and various syntaxes depending on what is being tested against. `match` will test if the left hand side matches the CSS query or Regular Expression string. `contains` will test if the left hand side contains OR includes the right hand side (invoking `contains()` or `includes()`). `includes` is identical to `contains`. `exist` test if the left hand side
is not null and, if it is a collection of elements, it contains any elements.

Expand Down

0 comments on commit 6282add

Please sign in to comment.