Skip to content

Commit

Permalink
Merge pull request #2 from brexhq/array-operators
Browse files Browse the repository at this point in the history
Add array comparison operators
  • Loading branch information
peburrows authored Oct 22, 2024
2 parents adbafff + 08575f8 commit a548fa1
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/statsig_ex/evaluator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,29 @@ defmodule StatsigEx.Evaluator do
end
end

# all array comparisons require a list for both value & target
defp compare(val, target, <<"array_contains_", _type::binary>>)
when not is_list(val) or not is_list(target),
do: false

defp compare(val, target, "array_contains_any") do
val_m = MapSet.new(val)
target_m = MapSet.new(target)
MapSet.intersection(val_m, target_m) |> MapSet.size() > 0
end

defp compare(val, target, "array_contains_none"),
do: !compare(val, target, "array_contains_any")

defp compare(val, target, "array_contains_all") do
val_m = MapSet.new(val)
target_m = MapSet.new(target)
MapSet.subset?(val_m, target_m)
end

defp compare(val, target, "not_array_contains_all"),
do: !compare(val, target, "array_contains_all")

defp compare(val, target, "eq"), do: val == target
defp compare(val, target, "neq"), do: val != target

Expand Down

0 comments on commit a548fa1

Please sign in to comment.