FIX: Skip gsub for normalizing whitespaces when text is nil (#6631)

FIX: Skip gsub for normalizing whitespaces when text is nil (#6631)

From eacbe28f554ec18e81019e4cd1b75bd1ff4feb2f Mon Sep 17 00:00:00 2001
From: Rishabh <rishabh.nambiar@discourse.org>
Date: Tue, 20 Nov 2018 13:42:32 +0530
Subject: [PATCH] FIX: Skip gsub for normalizing whitespaces when text is nil
 (#6631)


diff --git a/lib/text_cleaner.rb b/lib/text_cleaner.rb
index f258f90..48307d3 100644
--- a/lib/text_cleaner.rb
+++ b/lib/text_cleaner.rb
@@ -57,7 +57,7 @@ class TextCleaner
   @@whitespaces_regexp = Regexp.new("(\u00A0|\u1680|\u180E|[\u2000-\u200A]|\u2028|\u2029|\u202F|\u205F|\u3000)", "u").freeze
 
   def self.normalize_whitespaces(text)
-    text.gsub(@@whitespaces_regexp, ' ')
+    text&.gsub(@@whitespaces_regexp, ' ')
   end
 
 end
diff --git a/spec/components/text_cleaner_spec.rb b/spec/components/text_cleaner_spec.rb
index f9a9b10..d5cc332 100644
--- a/spec/components/text_cleaner_spec.rb
+++ b/spec/components/text_cleaner_spec.rb
@@ -218,6 +218,7 @@ describe TextCleaner do
       whitespaces = "\u0020\u00A0\u1680\u180E\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000"
       expect(whitespaces.strip).not_to eq("")
       expect(TextCleaner.normalize_whitespaces(whitespaces).strip).to eq("")
+      expect(TextCleaner.normalize_whitespaces(nil)).to be_nil
     end
 
     it "does not muck with zero width white space" do

GitHub

2 Likes

text_cleaner.rb is an excellent candidate for frozen string literal, recommend reading through:

https://samsaffron.com/archive/2018/02/16/reducing-string-duplication-in-ruby

4 Likes

I’ll have to read it a few more times to take it all in but that was an awesome read, thanks! :grinning:
I’ll add "adding frozen string literals to text_cleaner.rb" to my list unless someone beats me to it.

2 Likes