Skip to content

Commit

Permalink
Add some rubocop cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rmm5t committed Feb 14, 2020
1 parent 4776258 commit 40a3939
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions lib/strip_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def strip_attributes!(options = {})
end

module StripAttributes
VALID_OPTIONS = [:only, :except, :allow_empty, :collapse_spaces, :replace_newlines, :regex, :if, :unless]
VALID_OPTIONS = [:only, :except, :allow_empty, :collapse_spaces, :replace_newlines, :regex, :if, :unless].freeze

# Unicode invisible and whitespace characters. The POSIX character class
# [:space:] corresponds to the Unicode class Z ("separator"). We also
Expand All @@ -32,10 +32,10 @@ module StripAttributes
# U+200D ZERO WIDTH JOINER
# U+2060 WORD JOINER
# U+FEFF ZERO WIDTH NO-BREAK SPACE
MULTIBYTE_WHITE = "\u180E\u200B\u200C\u200D\u2060\uFEFF"
MULTIBYTE_SPACE = /[[:space:]#{MULTIBYTE_WHITE}]/
MULTIBYTE_BLANK = /[[:blank:]#{MULTIBYTE_WHITE}]/
MULTIBYTE_SUPPORTED = "\u0020" == " "
MULTIBYTE_WHITE = "\u180E\u200B\u200C\u200D\u2060\uFEFF".freeze
MULTIBYTE_SPACE = /[[:space:]#{MULTIBYTE_WHITE}]/.freeze
MULTIBYTE_BLANK = /[[:blank:]#{MULTIBYTE_WHITE}]/.freeze
MULTIBYTE_SUPPORTED = "\u0020" == " "

def self.strip(record_or_string, options = {})
if record_or_string.respond_to?(:attributes)
Expand Down Expand Up @@ -63,23 +63,17 @@ def self.strip_string(value, options = {})
replace_newlines = options[:replace_newlines]
regex = options[:regex]

if value.respond_to?(:strip)
value = value.strip
end
value = value.strip if value.respond_to?(:strip)

if regex && value.respond_to?(:gsub!)
value.gsub!(regex, "")
end
value.gsub!(regex, "") if regex && value.respond_to?(:gsub!)

if MULTIBYTE_SUPPORTED && value.respond_to?(:gsub!) && Encoding.compatible?(value, MULTIBYTE_SPACE)
value.gsub!(/\A#{MULTIBYTE_SPACE}+|#{MULTIBYTE_SPACE}+\z/, "")
elsif value.respond_to?(:strip!)
value.strip!
end

if replace_newlines && value.respond_to?(:gsub!)
value.gsub!(/[\r\n]+/, " ")
end
value.gsub!(/[\r\n]+/, " ") if replace_newlines && value.respond_to?(:gsub!)

if collapse_spaces
if MULTIBYTE_SUPPORTED && value.respond_to?(:gsub!) && Encoding.compatible?(value, MULTIBYTE_BLANK)
Expand Down

0 comments on commit 40a3939

Please sign in to comment.