From adae5d78f5efd95a7728a3be0c734e55a0344f9b Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Sun, 20 Mar 2016 08:35:48 -0400 Subject: [PATCH] Rewrite capitalize for readability Makes capitalize logic more clear with @sam suggestion --- lib/text_cleaner.rb | 5 ++++- spec/components/text_cleaner_spec.rb | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/text_cleaner.rb b/lib/text_cleaner.rb index be528936e..a7ecd4857 100644 --- a/lib/text_cleaner.rb +++ b/lib/text_cleaner.rb @@ -33,7 +33,10 @@ class TextCleaner # Replace all-caps text with regular case letters text = text.mb_chars.downcase.to_s if opts[:replace_all_upper_case] && (text =~ /[A-Z]+/) && (text == text.upcase) # Capitalize first letter, but only when entire first word is lowercase - text = text.mb_chars.capitalize.to_s if opts[:capitalize_first_letter] && (text =~ /\S+/) && text.split(' ').first == text.split(' ').first.mb_chars.downcase + first, rest = text.split(' ', 2) + if opts[:capitalize_first_letter] && first == first.mb_chars.downcase + text = "#{first.mb_chars.capitalize}#{rest ? ' ' + rest : ''}" + end # Remove unnecessary periods at the end text.sub!(/([^.])\.+(\s*)\z/, '\1\2') if opts[:remove_all_periods_from_the_end] # Remove extraneous space before the end punctuation diff --git a/spec/components/text_cleaner_spec.rb b/spec/components/text_cleaner_spec.rb index 41d65a4ec..a41a1e7c1 100644 --- a/spec/components/text_cleaner_spec.rb +++ b/spec/components/text_cleaner_spec.rb @@ -194,7 +194,7 @@ describe TextCleaner do it "capitalizes first unicode letter" do expect(TextCleaner.clean_title("épico encontro")).to eq("Épico encontro") end - + end end