class Blufin::Strings

Constants

RETURN_CHARACTER

Public Class Methods

camel_case_to_snake_case(string) click to toggle source

Convert 'CamelCase' to 'snake_case' @param String @return String

# File lib/core/strings.rb, line 30
def self.camel_case_to_snake_case(string)
    string.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').tr('-', '_').downcase
end
extract_using_regex(string, regex, strings_to_remove) click to toggle source

Extracts parts of a string. Regex is what to match, array is what to remove (gsub) afterwards. @return boolean

# File lib/core/strings.rb, line 48
def self.extract_using_regex(string, regex, strings_to_remove)
    unless strings_to_remove.is_a?(Array)
        raise RuntimeError, "'strings_to_remove' must be an Array. You passed: #{strings_to_remove.class}"

    end
    match = string.match(regex)
    raise RuntimeError, "No match found for '#{string}' and regex '#{regex}'." if match.nil?
    match_to_return = match[0]
    strings_to_remove.each do |string_to_remove|
        match_to_return = match_to_return.gsub(string_to_remove, '')
    end
    match_to_return
end
random_string(x = 1) click to toggle source

Generate Random string. Currently returns something like: 1ec6c763 @return String

# File lib/core/strings.rb, line 92
def self.random_string(x = 1)
    rs = ''
    x.times do
        rs = "#{rs}#{SecureRandom.uuid.split('-')[0].downcase}"
    end
    rs
end
remove_surrounding_slashes(string) click to toggle source

Remove preceding/trailing slashes from a string (and trim preceding/trailing whitespace). @param String - The string to be trimmed (and returned). @return String

# File lib/core/strings.rb, line 37
def self.remove_surrounding_slashes(string)
    raise RuntimeError, "Expected String, instead got: #{string.class} (#{string.inspect})" unless string.is_a?(String)
    string = string.strip
    validate_string(string)
    string = string.gsub(/\A\/+/, '')
    string = string.gsub(/\/+\z/, '')
    string
end
snake_case_to_camel_case(string) click to toggle source

Convert 'snake_case' or 'SnAKE_cAse' to 'SnakeCase'. @param String @return String

# File lib/core/strings.rb, line 12
def self.snake_case_to_camel_case(string)
    validate_string(string)
    string = string.downcase
    return string if string !~ /_/ && string =~ /[A-Z]+.*/
    string.split('_').map { |e| e.capitalize }.join
end
snake_case_to_camel_case_lower(string) click to toggle source

Convert 'snake_case' or 'SnAKE_cAse' to 'snakeCase'. @param String @return String

# File lib/core/strings.rb, line 22
def self.snake_case_to_camel_case_lower(string)
    string = snake_case_to_camel_case(string)
    "#{string[0, 1].downcase}#{string[1..-1]}"
end
string_difference_percent(a, b) click to toggle source

Finds the difference between 2 string Anything up to 0.15 means they're fairly similar. @return Float

# File lib/core/strings.rb, line 65
def self.string_difference_percent(a, b)
    validate_string(a)
    validate_string(b)
    x             = a.dup.gsub(' ', '')
    y             = b.dup.gsub(' ', '')
    x_hash        = string_difference_as_hash(x)
    y_hash        = string_difference_as_hash(y)
    total_letters = x.length + y.length
    x_hash.each do |key, value|
        if y_hash.has_key?(key)
            if y_hash[key] == x_hash[key]
                y_hash.delete(key)
            else
                y_hash[key] = (x_hash[key].to_i - y_hash[key].to_i).abs
            end
        end
    end
    discrepancies = 0
    y_hash.each do |key, value|
        discrepancies = discrepancies + value.to_i
    end
    return ((discrepancies.to_f / total_letters.to_f) * 100).round
end
strip_ansi_colors(string) click to toggle source

Removes all colors from a string. stackoverflow.com/questions/16032726/removing-color-decorations-from-strings-before-writing-them-to-logfile @return string

# File lib/core/strings.rb, line 111
def self.strip_ansi_colors(string)
    raise RuntimeError, "Expected String, instead got: #{string.class}" unless string.is_a?(String)
    string.gsub(/\e\[([;\d]+)?m/, '').strip
end
strip_newline(string) click to toggle source

Strips all newline character(s) – IE: “abcn” -> “abc” Be careful, if multiple newlines are in string, they will all get stripped and you might end up with weird output. @return string

# File lib/core/strings.rb, line 103
def self.strip_newline(string)
    raise RuntimeError, "Expected String, instead got: #{string.class}" unless string.is_a?(String)
    string.gsub(/[\r\n]+/m, '').strip
end

Private Class Methods

string_difference_as_hash(string) click to toggle source

Internal method helper for string_difference_percent(). @return Hash

# File lib/core/strings.rb, line 126
def self.string_difference_as_hash(string)
    hash = {}
    string.split('').each do |letter|
        if !hash.has_key?(letter)
            hash[letter] = 1
        else
            hash[letter] = hash[letter].to_i + 1
        end
    end
    hash
end
validate_string(string) click to toggle source

Internal string validation. @return void

# File lib/core/strings.rb, line 120
def self.validate_string(string)
    raise RuntimeError, "Expected String, instead got:#{string.class}" if string.nil? || !string.is_a?(String)
end