Module: StringMagic::Formatting::Truncation

Included in:
String, StringMagic
Defined in:
lib/string_magic/formatting/truncation.rb

Instance Method Summary collapse

Instance Method Details

#smart_truncate(limit, suffix: '...') ⇒ Object


“Smart” truncate: sentence ▸ word ▸ hard cut




55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/string_magic/formatting/truncation.rb', line 55

def smart_truncate(limit, suffix: '...')
  return '' if empty? || length <= limit
  hard_len = limit - suffix.length
  return suffix[0, limit] if hard_len.negative?

  # Try sentence boundary
  sent_break = rindex(/[.!?]\s/, hard_len)
  return self[0..sent_break].rstrip + suffix if sent_break && sent_break > limit * 0.5

  # Try word boundary
  word_break = rindex(' ', hard_len)
  return self[0, word_break].rstrip + suffix if word_break && word_break > limit * 0.3

  # Fallback
  self[0, hard_len] + suffix
end

#truncate_characters(limit, suffix: '...', break_on_word: false) ⇒ Object


Character-based


break_on_word: true → keep whole words break_on_word: false → may cut mid-word (default)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/string_magic/formatting/truncation.rb', line 37

def truncate_characters(limit, suffix: '...', break_on_word: false)
  return '' if empty? || length <= limit
  
  # When break_on_word is true, we want the result to be exactly `limit` chars total
  target_content_len = limit - suffix.length
  return suffix[0, limit] if target_content_len <= 0

  if break_on_word
    cut = rindex(' ', target_content_len) || target_content_len
    self[0, cut].rstrip + suffix
  else
    self[0, target_content_len] + suffix
  end
end

#truncate_sentences(limit, suffix: '...') ⇒ Object


Sentence-based




19
20
21
22
23
24
25
26
27
28
# File 'lib/string_magic/formatting/truncation.rb', line 19

def truncate_sentences(limit, suffix: '...')
  return '' if empty?
  sentences = split(/(?<=[.!?])\s+/)
  return self if sentences.size <= limit
  result = sentences.first(limit).join(' ')
  result.chomp!('.')
  result.chomp!('!')
  result.chomp!('?')
  result += suffix unless result == self
end

#truncate_words(limit, suffix: '...', separator: ' ') ⇒ Object


Word-based




9
10
11
12
13
14
# File 'lib/string_magic/formatting/truncation.rb', line 9

def truncate_words(limit, suffix: '...', separator: ' ')
  return '' if empty?
  words = split(separator)
  return self if words.size <= limit
  words.first(limit).join(separator) + suffix
end