Skip to content

Commit

Permalink
Handle nil expression on has_pattern function
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkhk committed Sep 11, 2024
1 parent 0701f76 commit e2d4130
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions lib/expression/callbacks/standard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1489,14 +1489,11 @@ defmodule Expression.Callbacks.Standard do
@expression_doc expression: "has_pattern(\"Buy cheese please\", \"buy (\\w+)\")", result: true
@expression_doc expression: "has_pattern(\"Sell cheese please\", \"buy (\\w+)\")", result: false
@expression_doc expression: "has_pattern(nil, \"buy (\\w+)\")", result: false
def has_pattern(nil, _expression, _pattern) do
false
end

def has_pattern(ctx, expression, pattern) do
[expression, pattern] = eval_args!([expression, pattern], ctx)

with {:ok, regex} <- Regex.compile(String.trim(pattern), "i"),
with false <- is_nil(expression),
{:ok, regex} <- Regex.compile(String.trim(pattern), "i"),
[[_first | _remainder]] <- Regex.scan(regex, String.trim(expression), capture: :all) do
# Future match result: first
true
Expand Down

0 comments on commit e2d4130

Please sign in to comment.