Module: StringMagic::Core::Transformation
- Included in:
- String, StringMagic
- Defined in:
- lib/string_magic/core/transformation.rb
Instance Method Summary collapse
- #alternating_case ⇒ Object
- #escape_html ⇒ Object
- #remove_duplicate_chars ⇒ Object
- #remove_duplicate_words ⇒ Object
- #remove_html_tags ⇒ Object
-
#reverse_words ⇒ Object
———————————————————— Simple word / char utilities ————————————————————.
-
#squeeze_whitespace ⇒ Object
———————————————————— Misc.
-
#to_camel_case(first_letter: :lower) ⇒ Object
first_letter: :lower (default) or :upper.
- #to_kebab_case ⇒ Object
- #to_pascal_case ⇒ Object
-
#to_snake_case ⇒ Object
———————————————————— Case conversions ————————————————————.
- #to_title_case ⇒ Object
Instance Method Details
#alternating_case ⇒ Object
62 63 64 65 |
# File 'lib/string_magic/core/transformation.rb', line 62 def alternating_case return '' if empty? each_char.with_index.map { |c, i| i.even? ? c.upcase : c.downcase }.join end |
#escape_html ⇒ Object
91 92 93 94 |
# File 'lib/string_magic/core/transformation.rb', line 91 def escape_html return '' if empty? CGI.escapeHTML(self) end |
#remove_duplicate_chars ⇒ Object
67 68 69 70 |
# File 'lib/string_magic/core/transformation.rb', line 67 def remove_duplicate_chars return '' if empty? each_char.to_a.uniq.join end |
#remove_duplicate_words ⇒ Object
72 73 74 75 |
# File 'lib/string_magic/core/transformation.rb', line 72 def remove_duplicate_words return '' if empty? split(/\s+/).uniq.join(' ') end |
#remove_html_tags ⇒ Object
86 87 88 89 |
# File 'lib/string_magic/core/transformation.rb', line 86 def return '' if empty? gsub(/<[^>]*>/, '') end |
#reverse_words ⇒ Object
Simple word / char utilities
57 58 59 60 |
# File 'lib/string_magic/core/transformation.rb', line 57 def reverse_words return '' if empty? split(/\s+/).reverse.join(' ') end |
#squeeze_whitespace ⇒ Object
Misc. text transformations
81 82 83 84 |
# File 'lib/string_magic/core/transformation.rb', line 81 def squeeze_whitespace return '' if empty? gsub(/\s+/, ' ').strip end |
#to_camel_case(first_letter: :lower) ⇒ Object
first_letter: :lower (default) or :upper
30 31 32 33 34 35 36 |
# File 'lib/string_magic/core/transformation.rb', line 30 def to_camel_case(first_letter: :lower) return '' if empty? words = to_snake_case.split('_').map(&:capitalize) words[0].downcase! if first_letter == :lower && words.any? words.join end |
#to_kebab_case ⇒ Object
25 26 27 |
# File 'lib/string_magic/core/transformation.rb', line 25 def to_kebab_case to_snake_case.tr('_', '-') end |
#to_pascal_case ⇒ Object
38 39 40 |
# File 'lib/string_magic/core/transformation.rb', line 38 def to_pascal_case to_camel_case(first_letter: :upper) end |
#to_snake_case ⇒ Object
Case conversions
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/string_magic/core/transformation.rb', line 12 def to_snake_case return '' if empty? str = dup str.gsub!(/::/, '/') str.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2') str.gsub!(/([a-z\d])([A-Z])/, '\1_\2') str.tr!('-', '_') str.gsub!(/\s+/, '_') str.downcase! str.squeeze('_').gsub(/^_+|_+$/, '') end |
#to_title_case ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/string_magic/core/transformation.rb', line 42 def to_title_case return '' if empty? small = %w[a an and as at but by for if in nor of on or so the to up yet] words = downcase.split(/\s+/) words.map!.with_index do |w, i| (i.zero? || i == words.size - 1 || !small.include?(w)) ? w.capitalize : w end words.join(' ') end |