module TextUtil
TextUtil — shared string helpers for search, report, and lookup modules. Public: squish, truncate, title_words, format_count Depends: (none) Tests: test/lib/text_util_test.rb
Public Instance Methods
Source
# File lib/text_util.rb, line 27 def format_count(total, comma: false) value = total.to_i formatted = if comma value.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse else value.to_s end value == 1 ? "1 result" : "#{formatted} results" end
Source
# File lib/text_util.rb, line 11 def squish(text) text.to_s.gsub(/\s+/, " ").strip end
Source
# File lib/text_util.rb, line 23 def title_words(text) text.to_s.split.map(&:capitalize).join(" ") end
Source
# File lib/text_util.rb, line 15 def truncate(text, max_length, ellipsis: "...") value = text.to_s.strip return value if value.length <= max_length suffix_length = ellipsis.length "#{value[0, max_length - suffix_length]}#{ellipsis}" end