Skip to content

Commit

Permalink
Allow multiple binary operator in where_exp filter (#8047)
Browse files Browse the repository at this point in the history
Merge pull request 8047
  • Loading branch information
ashmaroli committed Mar 6, 2020
1 parent ab6ef0b commit 0f4c8d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/jekyll/filters.rb
Expand Up @@ -432,10 +432,14 @@ def parse_condition(exp)
#
# Returns an instance of Liquid::Condition
def parse_binary_comparison(parser)
parse_comparison(parser).tap do |condition|
binary_operator = parser.id?("and") || parser.id?("or")
condition.send(binary_operator, parse_comparison(parser)) if binary_operator
condition = parse_comparison(parser)
first_condition = condition
while (binary_operator = parser.id?("and") || parser.id?("or"))
child_condition = parse_comparison(parser)
condition.send(binary_operator, child_condition)
condition = child_condition
end
first_condition
end

# Generates a Liquid::Condition object from a Liquid::Parser object based on whether the parsed
Expand Down
17 changes: 17 additions & 0 deletions test/test_filters.rb
Expand Up @@ -997,6 +997,23 @@ def to_liquid
)
end

should "filter objects across multiple conditions" do
sample = [
{ "color" => "teal", "size" => "large", "type" => "variable" },
{ "color" => "red", "size" => "large", "type" => "fixed" },
{ "color" => "red", "size" => "medium", "type" => "variable" },
{ "color" => "blue", "size" => "medium", "type" => "fixed" },
]
assert_equal(
[
{ "color" => "red", "size" => "large", "type" => "fixed" },
],
@filter.where_exp(
sample, "item", "item.type == 'fixed' and item.color == 'red' or item.color == 'teal'"
)
)
end

should "stringify during comparison for compatibility with liquid parsing" do
hash = {
"The Words" => { "rating" => 1.2, "featured" => false },
Expand Down

0 comments on commit 0f4c8d2

Please sign in to comment.