Skip to content

Commit

Permalink
Fix linting and type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jdorn committed Oct 30, 2023
1 parent aed534f commit 67126b1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
6 changes: 3 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ Metrics/BlockLength:
Max: 40

Metrics/CyclomaticComplexity:
Max: 25
Max: 40

Metrics/PerceivedComplexity:
Max: 25
Max: 40

Metrics/ParameterLists:
Max: 10
Expand All @@ -29,7 +29,7 @@ Metrics/ClassLength:
Max: 250

Metrics/MethodLength:
Max: 60
Max: 100

Layout/LineLength:
Max: 200
Expand Down
51 changes: 39 additions & 12 deletions lib/growthbook/conditions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,15 @@ def self.elem_match(condition, attribute_value)
end

def self.compare(val1, val2)
if val1.is_a?(Numeric) or val2.is_a?(Numeric)
if val1.is_a?(Numeric) || val2.is_a?(Numeric)
val1 = val1.is_a?(Numeric) ? val1 : val1.to_f
val2 = val2.is_a?(Numeric) ? val2 : val2.to_f
end

return 1 if val1 > val2
return 0 if val1 == val2
return -1 if val1 < val2

0
end

def self.eval_operator_condition(operator, attribute_value, condition_value)
Expand All @@ -130,17 +131,41 @@ def self.eval_operator_condition(operator, attribute_value, condition_value)
when '$vlte'
padded_version_string(attribute_value) <= padded_version_string(condition_value)
when '$eq'
compare(attribute_value, condition_value) == 0 rescue false
begin
compare(attribute_value, condition_value).zero?
rescue StandardError
false
end
when '$ne'
compare(attribute_value, condition_value) != 0 rescue false
begin
compare(attribute_value, condition_value) != 0
rescue StandardError
false
end
when '$lt'
compare(attribute_value, condition_value) < 0 rescue false
begin
compare(attribute_value, condition_value).negative?
rescue StandardError
false
end
when '$lte'
compare(attribute_value, condition_value) <= 0 rescue false
begin
compare(attribute_value, condition_value) <= 0
rescue StandardError
false
end
when '$gt'
compare(attribute_value, condition_value) > 0 rescue false
begin
compare(attribute_value, condition_value).positive?
rescue StandardError
false
end
when '$gte'
compare(attribute_value, condition_value) >= 0 rescue false
begin
compare(attribute_value, condition_value) >= 0
rescue StandardError
false
end
when '$regex'
silence_warnings do
re = Regexp.new(condition_value)
Expand All @@ -150,9 +175,11 @@ def self.eval_operator_condition(operator, attribute_value, condition_value)
end
when '$in'
return false unless condition_value.is_a?(Array)

in?(attribute_value, condition_value)
when '$nin'
return false unless condition_value.is_a?(Array)

!in?(attribute_value, condition_value)
when '$elemMatch'
elem_match(condition_value, attribute_value)
Expand Down Expand Up @@ -191,17 +218,17 @@ def self.padded_version_string(input)
# Remove build info and leading `v` if any
# Split version into parts (both core version numbers and pre-release tags)
# "v1.2.3-rc.1+build123" -> ["1","2","3","rc","1"]
parts = input.gsub(/(^v|\+.*$)/, "").split(/[-.]/)
parts = input.gsub(/(^v|\+.*$)/, '').split(/[-.]/)

# If it's SemVer without a pre-release, add `~` to the end
# ["1","0","0"] -> ["1","0","0","~"]
# "~" is the largest ASCII character, so this will make "1.0.0" greater than "1.0.0-beta" for example
parts << "~" if(parts.length == 3)
parts << '~' if parts.length == 3

# Left pad each numeric part with spaces so string comparisons will work ("9">"10", but " 9"<"10")
parts.map do |part|
part.match(/^[0-9]+$/) ? part.rjust(5, " ") : part
end.join("-")
/^[0-9]+$/.match?(part) ? part.rjust(5, ' ') : part
end.join('-')
end

def self.in?(actual, expected)
Expand Down

0 comments on commit 67126b1

Please sign in to comment.