Class: Growthbook::Util
- Inherits:
-
Object
- Object
- Growthbook::Util
- Defined in:
- lib/growthbook/util.rb
Overview
internal use only
Class Method Summary collapse
- .check_rule(actual, op, desired) ⇒ Object
-
.choose_variation(num, ranges) ⇒ Object
Chose a variation based on a hash and range.
-
.format_js_digits(digits, point) ⇒ Object
Lays out digits d1..dk with x = 0.d1..dk * 10**point, as ECMAScript Number::toString does.
-
.get_bucket_ranges(num_variations, coverage, weights) ⇒ Object
Determine bucket ranges for experiment variations.
- .get_equal_weights(num_variations) ⇒ Object
-
.get_hash(seed:, value:, version:) ⇒ Float?
Hash, or nil if the hash version is invalid.
-
.get_query_string_override(id, url, num_variations) ⇒ Object
Get an override variation from a url querystring e.g.
- .in_namespace?(hash_value, namespace) ⇒ Boolean
- .in_range?(num, range) ⇒ Boolean
-
.js_number_to_s(number) ⇒ String
Formats a number like JavaScript's String(number) (ECMAScript Number::toString), e.g.
Class Method Details
.check_rule(actual, op, desired) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/growthbook/util.rb', line 10 def self.check_rule(actual, op, desired) # Check if both strings are numeric so we can do natural ordering # for greater than / less than operators numeric = begin (!Float(actual).nil? && !Float(desired).nil?) rescue StandardError false end case op when '=' numeric ? Float(actual).to_d == Float(desired).to_d : actual == desired when '!=' numeric ? Float(actual).to_d != Float(desired).to_d : actual != desired when '>' numeric ? Float(actual) > Float(desired) : actual > desired when '<' numeric ? Float(actual) < Float(desired) : actual < desired when '~' begin !!(actual =~ Regexp.new(desired)) rescue StandardError false end when '!~' begin actual !~ Regexp.new(desired) rescue StandardError false end else true end end |
.choose_variation(num, ranges) ⇒ Object
Chose a variation based on a hash and range
99 100 101 102 103 104 |
# File 'lib/growthbook/util.rb', line 99 def self.choose_variation(num, ranges) ranges.each_with_index do |range, i| return i if num >= range[0] && num < range[1] end -1 end |
.format_js_digits(digits, point) ⇒ Object
Lays out digits d1..dk with x = 0.d1..dk * 10**point, as ECMAScript Number::toString does
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/growthbook/util.rb', line 170 def self.format_js_digits(digits, point) k = digits.length return digits + ('0' * (point - k)) if k <= point && point <= 21 return "#{digits[0, point]}.#{digits[point..-1]}" if point.positive? && point <= 21 return "0.#{'0' * -point}#{digits}" if point > -6 && point <= 0 e = point - 1 fraction = k > 1 ? ".#{digits[1..-1]}" : '' "#{digits[0]}#{fraction}e#{e.negative? ? '-' : '+'}#{e.abs}" end |
.get_bucket_ranges(num_variations, coverage, weights) ⇒ Object
Determine bucket ranges for experiment variations
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/growthbook/util.rb', line 73 def self.get_bucket_ranges(num_variations, coverage, weights) # Make sure coverage is within bounds coverage = 1.0 if coverage.nil? coverage = 0.0 if coverage.negative? coverage = 1.0 if coverage > 1 # Default to equal weights weights = get_equal_weights(num_variations) if !weights || weights.length != num_variations # If weights don't add up to 1 (or close to it), default to equal weights total = weights.sum weights = get_equal_weights(num_variations) if total < 0.99 || total > 1.01 # Convert weights to ranges cumulative = 0.0 ranges = [] weights.each do |w| start = cumulative cumulative += w ranges << [start, start + (coverage * w)] end ranges end |
.get_equal_weights(num_variations) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/growthbook/util.rb', line 62 def self.get_equal_weights(num_variations) return [] if num_variations < 1 weights = [] (1..num_variations).each do |_i| weights << (1.0 / num_variations) end weights end |
.get_hash(seed:, value:, version:) ⇒ Float?
Returns Hash, or nil if the hash version is invalid.
46 47 48 49 50 51 |
# File 'lib/growthbook/util.rb', line 46 def self.get_hash(seed:, value:, version:) return (FNV.new.fnv1a_32(value + seed) % 1000) / 1000.0 if version == 1 return (FNV.new.fnv1a_32(FNV.new.fnv1a_32(seed + value).to_s) % 10_000) / 10_000.0 if version == 2 nil end |
.get_query_string_override(id, url, num_variations) ⇒ Object
Get an override variation from a url querystring
e.g. http://localhost?my-test=1 will return 1 for id my-test
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/growthbook/util.rb', line 108 def self.get_query_string_override(id, url, num_variations) # Skip if url is empty return nil if url == '' || id.nil? # Parse out the query string parsed = URI(url) parsed_query = parsed.query return nil if parsed_query.nil? qs = URI.decode_www_form(parsed_query) # Look for `id` in the querystring and get the value vals = qs.assoc(id) return nil unless vals val = vals.last return nil unless val # Parse the value as an integer n = begin Integer(val) rescue StandardError nil end # Make sure the integer is within range return nil if n.nil? return nil if n.negative? return nil if n >= num_variations n end |
.in_namespace?(hash_value, namespace) ⇒ Boolean
53 54 55 56 57 58 59 60 |
# File 'lib/growthbook/util.rb', line 53 def self.in_namespace?(hash_value, namespace) return false if namespace.nil? n = get_hash(seed: "__#{namespace[0]}", value: hash_value, version: 1) return false if n.nil? n >= namespace[1] && n < namespace[2] end |
.in_range?(num, range) ⇒ Boolean
141 142 143 |
# File 'lib/growthbook/util.rb', line 141 def self.in_range?(num, range) num >= range[0] && num < range[1] end |
.js_number_to_s(number) ⇒ String
Formats a number like JavaScript's String(number) (ECMAScript Number::toString), e.g. 2.0 -> "2", 0.000002 -> "0.000002", 1e21 -> "1e+21".
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/growthbook/util.rb', line 150 def self.js_number_to_s(number) x = number.to_f return 'NaN' if x.nan? return x.positive? ? 'Infinity' : '-Infinity' if x.infinite? return '0' if x.zero? return "-#{js_number_to_s(-x)}" if x.negative? # Float#to_s gives the same shortest round-trip digits as JavaScript, but # switches to scientific notation at different thresholds mantissa, exponent = x.to_s.split('e') whole, fraction = mantissa.to_s.split('.') all_digits = "#{whole}#{fraction}" digits = all_digits.sub(/\A0+/, '') # x = 0.digits * 10**point point = whole.to_s.length + exponent.to_i - (all_digits.length - digits.length) digits = digits.sub(/0+\z/, '') format_js_digits(digits, point) end |