Skip to content

Commit

Permalink
Merge pull request #47 from rmm5t/46-fix-zero-width-space-strip-to-nil
Browse files Browse the repository at this point in the history
Fix zero width space strip to nil
  • Loading branch information
rmm5t authored Feb 14, 2020
2 parents 4fef468 + 40a3939 commit ce6dc05
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
24 changes: 9 additions & 15 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.blank? && !allow_empty) ? nil : 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 All @@ -89,7 +83,7 @@ def self.strip_string(value, options = {})
end
end

value
(value.blank? && !allow_empty) ? nil : value
end

# Necessary because Rails has removed the narrowing of attributes using :only
Expand Down
8 changes: 7 additions & 1 deletion test/strip_attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def self.included(base)
base.attribute :bang
base.attribute :foz
base.attribute :fiz
base.attribute :qax
base.attribute :qux
base.attribute :strip_me
base.attribute :skip_me
end
Expand Down Expand Up @@ -106,7 +108,9 @@ def setup
baz: "",
bang: " ",
foz: " foz foz",
fiz: "fiz \n fiz"
fiz: "fiz \n fiz",
qax: "\n\t ",
qux: "\u200B"
}
end

Expand All @@ -124,6 +128,8 @@ def test_should_strip_all_fields
assert_equal "fiz \n fiz", record.fiz
assert_nil record.baz
assert_nil record.bang
assert_nil record.qax
assert_nil record.qux
end

def test_should_strip_only_one_field
Expand Down

0 comments on commit ce6dc05

Please sign in to comment.