lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb in eco-helpers-2.1.4 vs lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb in eco-helpers-2.1.5
- old
+ new
@@ -2,32 +2,74 @@
module API
class UseCases
class OozeSamples
module Helpers
module Shortcuts
+ # Basic simplification pattern (matches all but a-z and blank space)
+ def non_letters_regex
+ @non_letters_regex ||= /[^a-z ]+/
+ end
- # Offers multiple ways to compare two strings
- def same_string?(value1, value2, exact: false, mild: false)
- case
- when value1.is_a?(String) && value2.is_a?(String)
- if exact
- value1 == value2
- else
- v1 = value1.to_s.strip.downcase
- v2 = value2.to_s.strip.downcase
+ # Matches anything between two consecutive (), inclusive
+ def bracked_regex
+ @bracked_regex ||= /(?<bracked>\([^\)]+?\))/
+ end
- if mild
- v1 = v1.gsub(/[^a-z ]+/, ' ').gsub(/\s+/, ' ').strip
- v2 = v2.gsub(/[^a-z ]+/, ' ').gsub(/\s+/, ' ').strip
- end
- v1 == v2
+ # It always downcase, trim and remove double spaces.
+ # @param ignore Boolean, Regexp] ingored when `exact` is `true`
+ # * when `false`: it does not do anything additional
+ # * when `Regex`: it removes all characters that match the expression.
+ # * when `String`: each character listed is removed.
+ # * when `Array`: reduces `str` processing `ignore` in order.
+ # * when `true` (or otherwise): it removes all non a-zA-Z characters but blank spaces.
+ def simplify_string(str, ignore: false)
+ str = str.to_s.strip.downcase.gsub(/\s+/, ' ')
+ return str unless ignore
+ sub = non_letters_regex
+ case ignore
+ when Regexp; sub = ignore
+ when String; sub = /[#{ignore}]+/
+ when Array
+ return ignore.reduce(str) do |out, sub|
+ simplify_string(out, ignore: sub)
end
- when value1.is_a?(Regexp) && value2.is_a?(String)
- value2 =~ value1
- when value1.is_a?(String) && value2.is_a?(Regexp)
- value1 =~ value2
+ end
+ str.gsub(sub, '').gsub(/\s+/, ' ').strip
+ end
+
+ # Offers multiple simplification methods to compare two strings
+ # @note only one of the values can be a Regexp
+ # @param value1 [String, Regexp, Nil]
+ # @param value2 [String, Regexp, Nil]
+ # @param exact [Boolean]
+ # * when `true`: requiring the values to be exactly the same
+ # * otherwise (`false`): compares in downcase, with no extra spaces
+ # @param mild [Boolean] only a-z comparison
+ # @param ignore [see @simplify_string]
+ def same_string?(value1, value2, exact: false, mild: false, ignore: false)
+ return true if value1.to_s.strip.empty? && value2.to_s.strip.empty?
+ return false if value1.to_s.strip.empty? || value2.to_s.strip.empty?
+ val1, val2 = value1, value2
+ unless exact
+ if val1.is_a?(String)
+ val1 = simplify_string(val1, ignore: ignore) if ignore
+ val1 = simplify_string(val1, ignore: non_letters_regex) if mild
+ end
+ if val2.is_a?(String)
+ val2 = simplify_string(val2, ignore: ignore) if ignore
+ val2 = simplify_string(val2, ignore: non_letters_regex) if mild
+ end
+ end
+ case
+ when val1.is_a?(String) && val2.is_a?(String)
+ val1 == val2
+ when val1.is_a?(Regexp) && val2.is_a?(String)
+ val2 =~ val1
+ when val1.is_a?(String) && val2.is_a?(Regexp)
+ val1 =~ val2
else
- value1 == value2
+ #val1 == val2
+ raise "Expected at least one String, and either a String or Regex. Given: (1: #{val1.class}) and (2: #{val2.class})"
end
end
def titleize(str)
return nil unless str