Module: IprogStringUtils
- Included in:
- String
- Defined in:
- lib/iprog_string_utils.rb,
lib/iprog_string_utils/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Method Summary collapse
- #camel_case ⇒ Object
- #count_words ⇒ Object
- #kebab_case ⇒ Object
- #palindrome? ⇒ Boolean
- #reverse_words ⇒ Object
- #snake_case ⇒ Object
- #strip_html ⇒ Object
- #title_case ⇒ Object
- #to_bool ⇒ Object
- #trim_custom(char = " ") ⇒ Object
- #truncate(max_length, ellipsis = "...") ⇒ Object
Instance Method Details
#camel_case ⇒ Object
6 7 8 9 10 |
# File 'lib/iprog_string_utils.rb', line 6 def camel_case split(/[^a-zA-Z0-9]/).map.with_index do |word, index| index.zero? ? word.downcase : word.capitalize end.join end |
#count_words ⇒ Object
38 39 40 |
# File 'lib/iprog_string_utils.rb', line 38 def count_words split.size end |
#kebab_case ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/iprog_string_utils.rb', line 21 def kebab_case gsub(/([a-z\d])([A-Z])/, '\1-\2') # Insert hyphen between a lowercase/digit and an uppercase letter .gsub(/([A-Z]+)([A-Z][a-z\d]+)/, '\1-\2') # Handle cases like "ATest" by inserting a hyphen between uppercase letters followed by a lowercase or digit .gsub(/\W/, "-") # Replace non-word characters with hyphens .downcase # Convert the entire string to lowercase .gsub(/--+/, "-") # Collapse multiple hyphens into a single hyphen .gsub(/^-|-$/, "") # Remove leading and trailing hyphens end |
#palindrome? ⇒ Boolean
61 62 63 64 |
# File 'lib/iprog_string_utils.rb', line 61 def palindrome? cleaned = gsub(/\W/, "").downcase cleaned == cleaned.reverse end |
#reverse_words ⇒ Object
34 35 36 |
# File 'lib/iprog_string_utils.rb', line 34 def reverse_words split.reverse.join(" ") end |
#snake_case ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/iprog_string_utils.rb', line 12 def snake_case gsub(/([a-z\d])([A-Z])/, '\1_\2') # Insert underscore between a lowercase/digit and an uppercase letter .gsub(/([A-Z]+)([A-Z][a-z\d]+)/, '\1_\2') # Handle cases like "ATest" by inserting an underscore between uppercase letters followed by a lowercase or digit .gsub(/\W/, "_") # Replace non-word characters with underscores .downcase # Convert the entire string to lowercase .gsub(/__+/, "_") # Collapse multiple underscores into a single underscore .gsub(/^_|_$/, "") # Remove leading and trailing underscores end |
#strip_html ⇒ Object
50 51 52 |
# File 'lib/iprog_string_utils.rb', line 50 def strip_html gsub(%r{</?[^>]*>}, "") end |
#title_case ⇒ Object
30 31 32 |
# File 'lib/iprog_string_utils.rb', line 30 def title_case split.map(&:capitalize).join(" ") end |
#to_bool ⇒ Object
54 55 56 57 58 59 |
# File 'lib/iprog_string_utils.rb', line 54 def to_bool return true if downcase == "true" return false if downcase == "false" nil end |
#trim_custom(char = " ") ⇒ Object
66 67 68 |
# File 'lib/iprog_string_utils.rb', line 66 def trim_custom(char = " ") gsub(/^#{Regexp.escape(char)}+|#{Regexp.escape(char)}+$/, "") end |
#truncate(max_length, ellipsis = "...") ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/iprog_string_utils.rb', line 42 def truncate(max_length, ellipsis = "...") return self if length <= max_length truncated_string = self[0, max_length] "#{truncated_string.rstrip}#{ellipsis}" end |