Module: SlimString
Overview
A module containing a method to slim down a UTF-8 String
It handles UTF8 characters, such as CJK (Zenkaku) spaces and offers many choices how to slim down a character string.
There are 3 ways to use the method #slim_string
(1) Simply call it in a full path
SlimString.slim_string("abc", trim: false)
(2) Include in your class/module (or top level), and you can
use it like: slim_string("abc", trim: false)
(3) Include it in String class like and you can used it like
an instance method.
class String
include SlimString
end
"abc".slim_string(trim: false)
Constant Summary collapse
- DEF_SLIM_OPTIONS =
Default options of operations for #slim_string, which processes in this order if specified.
{ :delete_newlines => false, # Delete all new lines (\f\n\r\v) (useful for Japanese text, unless more than 1 alphabet word are split across 2 lines). :convert_spaces => false, # Convert any sort of spaces (including new lines) into an ASCII space. :convert_blanks => true, # Convert all blanks (=spaces except kind of new lines) into an ASCII space. :truncate_spaces => false, # Truncate any consecutive spaces into one. :truncate_blanks => true, # Truncate any consecutive blanks into (the last) one. :truncate_triple_newlines => false, # Truncate 3 or more consecutive newlines into 2. :delete_blanks => false, # Simply delete all blanks excluding new lines (n.b., "a top" and "atop" are regarded identical. :strip => true, # Ruby strip; strip spaces including new lines at the head and tail. :trim_blanks => false, # strip blanks (but new lines) at the tail. :trim => true, # Trim the tail to strip any spaces including new lines at the tail. }
Instance Method Summary collapse
-
#slim_string(str = nil, **opts) ⇒ String
Returns a slimmed down string.
Instance Method Details
#slim_string(str = nil, **opts) ⇒ String
Returns a slimmed down string
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/slim_string.rb', line 67 def slim_string(str=nil, **opts) begin ret = (str || self).dup rescue NoMethodError raise ArgumentError, 'first argument is mandatory and must be String-like.' end DEF_SLIM_OPTIONS.merge(opts).each_pair do |ek, tf| next if !tf case ek when :delete_newlines ret.tr! "\f\n\r\v", "" # ASCII Formfeed (FF), Linefeed (LF), Carriage Return (CR), Vertical Tab (VT) when :convert_spaces ret.gsub!(/[[:space:]]/, ' ') when :convert_blanks ret.gsub!(/[[:blank:]]/, ' ') when :truncate_spaces ret.gsub!(/([[:space:]])+/, '\1') when :truncate_blanks ret.gsub!(/([[:blank:]])+/, '\1') when :truncate_triple_newlines ret.gsub!(/([\f\n\r\v]){3,}/, '\1\1') when :delete_blanks ret.gsub!(/[[:blank:]]+/, "") when :strip ret.strip! when :trim_blanks ret.gsub!(/[[:blank:]]+\z/, "") when :trim ret.gsub!(/([[:space:]])+\z/, "") else raise ArgumentError, 'Invalid optional argument: '+tf.inspect end end ret end |