Class: Blufin::Strings

Inherits:
Object
  • Object
show all
Defined in:
lib/core/strings.rb

Constant Summary collapse

RETURN_CHARACTER =
''

Class Method Summary collapse

Class Method Details

.camel_case_to_snake_case(string) ⇒ Object

Convert ‘CamelCase’ to ‘snake_case’

Parameters:

  • String

Returns:

  • String



30
31
32
# 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) ⇒ Object

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

Returns:

  • boolean

Raises:

  • (RuntimeError)


48
49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object

Generate Random string. Currently returns something like: 1ec6c763

Returns:

  • String



92
93
94
95
96
97
98
# 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) ⇒ Object

Remove preceding/trailing slashes from a string (and trim preceding/trailing whitespace).

Parameters:

  • String
    • The string to be trimmed (and returned).

Returns:

  • String

Raises:

  • (RuntimeError)


37
38
39
40
41
42
43
44
# File 'lib/core/strings.rb', line 37

def self.remove_surrounding_slashes(string)
    raise RuntimeError, "Expected String, instead got:#{string.class}" 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) ⇒ Object

Convert ‘snake_case’ or ‘SnAKE_cAse’ to ‘SnakeCase’.

Parameters:

  • String

Returns:

  • String



12
13
14
15
16
17
# 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) ⇒ Object

Convert ‘snake_case’ or ‘SnAKE_cAse’ to ‘snakeCase’.

Parameters:

  • String

Returns:

  • String



22
23
24
25
# 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) ⇒ Object

Finds the difference between 2 string Anything up to 0.15 means they’re fairly similar.

Returns:

  • Float



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Object

Returns:

  • string

Raises:

  • (RuntimeError)


111
112
113
114
# 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) ⇒ Object

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.

Returns:

  • string

Raises:

  • (RuntimeError)


103
104
105
106
# 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