Sha256: 97f792edf78d06f3dd75f27ce6faf10980db19c3d815aa0e0845feb94fe728e1

Contents?: true

Size: 1.24 KB

Versions: 125

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

class ReeString::TruncateWords
  include Ree::FnDSL

  fn :truncate_words

  DEFAULT_OMISSION = "..."

  doc(<<~DOC)
    Truncates a given +text+ after a given number of words (<tt>words_count</tt>):
    
      truncate_words('Once upon a time in a world far far away', 4)
      # => "Once upon a time..."
    
    Pass a string or regexp <tt>:separator</tt> to specify a different separator of words:
    
      truncate_words('Once<br>upon<br>a<br>time<br>in<br>a<br>world', 5, separator: '<br>')
      # => "Once<br>upon<br>a<br>time<br>in..."
    
    The last characters will be replaced with the <tt>:omission</tt> string (defaults to "..."):
    
      truncate_words('And they found that many people were sleeping better.', 5, omission: '... (continued)')
      # => "And they found that many... (continued)"
  DOC
  contract(
    String,
    Integer,
    Ksplat[
      omission?: String,
      separator?: Or[String, Regexp]
    ] => String
  )
  def call(str, words_count, **opts)
    str = str.dup
    sep = opts[:separator] || /\s+/
    sep = Regexp.escape(sep.to_s) unless Regexp === sep

    if str =~ /\A((?>.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m
      $1 + (opts[:omission] || "...")
    else
      str
    end
  end
end

Version data entries

125 entries across 125 versions & 1 rubygems

Version Path
ree_lib-1.1.0 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.124 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_string/package/ree_string/functions/truncate_words.rb