Module: WolfCore::StringUtils
- Included in:
- FileUtils, FriendlyModelId, LambdaFunctionOperations
- Defined in:
- lib/wolf_core/utils/string_utils.rb
Instance Method Summary collapse
- #base64_encoded?(str) ⇒ Boolean
- #camelcase_to_spaces(str) ⇒ Object
- #clean_phone_number(phone) ⇒ Object
- #deep_parse_json(input) ⇒ Object
- #hash_str_to_json(hash_str) ⇒ Object
- #jaro_winkler_similarity(string1, string2) ⇒ Object
- #remove_blank_spaces(str) ⇒ Object
- #remove_non_alphanumeric_chars(str, exceptions: nil) ⇒ Object
- #split_address(address_string) ⇒ Object
- #split_name(full_name, name_range: nil, lastname_range: nil, if_one_word: nil) ⇒ Object
- #to_kebab_case(str) ⇒ Object
- #to_snake_case(str) ⇒ Object
- #valid_json?(str) ⇒ Boolean
- #valid_url?(url) ⇒ Boolean
Instance Method Details
#base64_encoded?(str) ⇒ Boolean
71 72 73 74 75 76 |
# File 'lib/wolf_core/utils/string_utils.rb', line 71 def base64_encoded?(str) return false unless str.is_a?(String) && str.length % 4 == 0 base64_regex = %r{^[A-Za-z0-9+/]+={0,2}$} str.match?(base64_regex) end |
#camelcase_to_spaces(str) ⇒ Object
3 4 5 6 7 |
# File 'lib/wolf_core/utils/string_utils.rb', line 3 def camelcase_to_spaces(str) return if str.nil? str.to_s.gsub(/([A-Z])/, ' \1').strip.downcase.capitalize end |
#clean_phone_number(phone) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/wolf_core/utils/string_utils.rb', line 31 def clean_phone_number(phone) return if phone.nil? cleaned_phone = phone.to_s.gsub(/\D/, "") cleaned_phone[-10, 10] end |
#deep_parse_json(input) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/wolf_core/utils/string_utils.rb', line 50 def deep_parse_json(input) while input.is_a?(String) begin input = JSON.parse(input) rescue JSON::ParserError break end end input || {} end |
#hash_str_to_json(hash_str) ⇒ Object
44 45 46 47 48 |
# File 'lib/wolf_core/utils/string_utils.rb', line 44 def hash_str_to_json(hash_str) hash_str&.to_s&.gsub("=>", ":")&.gsub('\"', '"')&.gsub("{", "{")&.gsub("}", "}")&.gsub(/(\w+)(?=\s*:)/) do |key| "\"#{key}\"" end end |
#jaro_winkler_similarity(string1, string2) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/wolf_core/utils/string_utils.rb', line 148 def jaro_winkler_similarity(string1, string2) str1 = string1.to_s.strip.downcase str2 = string2.to_s.strip.downcase return 1.0 if str1 == str2 return 0.0 if str1.empty? || str2.empty? max_len = [str1.length, str2.length].max match_distance = [(max_len / 2) - 1, 0].max str1_matches = Array.new(str1.length, false) str2_matches = Array.new(str2.length, false) matches = 0 str1.each_char.with_index do |char, i| start = [i - match_distance, 0].max finish = [i + match_distance + 1, str2.length].min (start...finish).each do |j| next if str2_matches[j] next unless str2[j] == char str1_matches[i] = true str2_matches[j] = true matches += 1 break end end return 0.0 if matches.zero? transpositions = 0 j = 0 str1.each_char.with_index do |char, i| next unless str1_matches[i] j += 1 while j < str2.length && !str2_matches[j] break if j >= str2.length transpositions += 1 if str2[j] != char j += 1 end transpositions /= 2.0 jaro = ( (matches / str1.length.to_f) + (matches / str2.length.to_f) + ((matches - transpositions) / matches) ) / 3.0 prefix_length = 0 max_prefix = 4 while prefix_length < max_prefix && prefix_length < str1.length && prefix_length < str2.length && str1[prefix_length] == str2[prefix_length] prefix_length += 1 end jaro + (prefix_length * 0.1 * (1 - jaro)) end |
#remove_blank_spaces(str) ⇒ Object
38 39 40 41 42 |
# File 'lib/wolf_core/utils/string_utils.rb', line 38 def remove_blank_spaces(str) return if str.nil? str.to_s.gsub(/\s+/, "") end |
#remove_non_alphanumeric_chars(str, exceptions: nil) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/wolf_core/utils/string_utils.rb', line 21 def remove_non_alphanumeric_chars(str, exceptions: nil) return if str.nil? exceptions ||= [] exceptions = Array(exceptions) escaped_exceptions = exceptions.map { |char| Regexp.escape(char) }.join regex = /[^a-zA-Z0-9#{escaped_exceptions}]/ str.to_s.gsub(regex, "") end |
#split_address(address_string) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/wolf_core/utils/string_utils.rb', line 87 def split_address(address_string) address_string = address_string&.dup address_string ||= "" address = { street: nil, city: nil, state: nil, zip: nil } city_and_state_regex_found = false city_and_state_regex = /\b([A-Za-z\s]+),\s*([A-Za-z]{2})\b/ zip_regex = /\d{5}(?:-\d{4})?$/ street_regex = /\A([^,]+)/ state_regex = /\b[A-Za-z]{2}\b/ city_regex = /\b[a-zA-Z0-9\s]{2,}\b/ if match_data = address_string.match(city_and_state_regex) address[:city] = match_data[1].strip address[:state] = match_data[2].strip address_string.sub!(city_and_state_regex, "") city_and_state_regex_found = true end if zip_match = address_string.match(zip_regex) address[:zip] = zip_match[0].strip address_string.sub!(zip_regex, "") end if street_match = address_string.match(street_regex) address[:street] = street_match[0].strip address[:street] = nil if address[:street].empty? address_string.sub!(street_regex, "") end return address if city_and_state_regex_found if state_match = address_string.match(state_regex) address[:state] = state_match[0].strip address_string.sub!(state_regex, "") end if city_match = address_string.match(city_regex) address[:city] = city_match[0].strip address_string.sub!(city_regex, "") end address end |
#split_name(full_name, name_range: nil, lastname_range: nil, if_one_word: nil) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/wolf_core/utils/string_utils.rb', line 132 def split_name(full_name, name_range: nil, lastname_range: nil, if_one_word: nil) if_one_word = {} unless if_one_word.is_a?(Hash) if_one_word.merge!(lastname: "-") if if_one_word[:lastname].nil? words = full_name.strip.split return { first_name: words[0], last_name: if_one_word[:lastname] } if words.length == 1 return { first_name: words[0], last_name: words[1] } if words.length == 2 name_range ||= 0..-2 lastname_range ||= -1..-1 name = words[name_range].join(" ") lastname = words[lastname_range].join(" ") { first_name: name, last_name: lastname } end |
#to_kebab_case(str) ⇒ Object
15 16 17 18 19 |
# File 'lib/wolf_core/utils/string_utils.rb', line 15 def to_kebab_case(str) return if str.nil? str.to_s.gsub(/\s+/, "-").downcase end |
#to_snake_case(str) ⇒ Object
9 10 11 12 13 |
# File 'lib/wolf_core/utils/string_utils.rb', line 9 def to_snake_case(str) return if str.nil? str.to_s.gsub(/\s+/, "_").downcase end |
#valid_json?(str) ⇒ Boolean
78 79 80 81 82 83 84 85 |
# File 'lib/wolf_core/utils/string_utils.rb', line 78 def valid_json?(str) return false unless str.is_a?(String) JSON.parse(str) true rescue JSON::ParserError false end |
#valid_url?(url) ⇒ Boolean
62 63 64 65 66 67 68 69 |
# File 'lib/wolf_core/utils/string_utils.rb', line 62 def valid_url?(url) return false unless url.instance_of?(String) uri = URI.parse(url) uri.is_a?(URI::HTTP) || (uri.is_a?(URI::HTTPS) && !uri.host.nil?) rescue URI::InvalidURIError false end |