lib/wolf_core/utils/string_utils.rb



module WolfCore
  module StringUtils
    def camelcase_to_spaces(str)
      return if str.nil?
      str.gsub(/([A-Z])/, ' \1').strip.downcase.capitalize
    end

    def clean_phone_number(phone)
      return if phone.nil?
      cleaned_phone = phone.to_s.gsub(/\D/, '')
      last_ten_digits = cleaned_phone[-10, 10]
      last_ten_digits
    end

    def remove_blank_spaces(str)
      str&.gsub(/\s+/, '')
    end

    def hash_str_to_json(hash_str)
      hash_str&.gsub('=>', ':')&.gsub('\"', '"')&.gsub('{', '{')&.gsub('}', '}')&.gsub(/(\w+)(?=\s*:)/) { |key| "\"#{key}\"" }
    end

    def deep_parse_json(input)
      while input.is_a?(String)
        begin
          input = JSON.parse(input)
        rescue JSON::ParserError
          break
        end
      end

      input || {}
    end
  end
end