Sha256: 34dbe58cc915056573ec4b79aae007ef0131ddf7551f309f5c8522e12e01d7a1

Contents?: true

Size: 682 Bytes

Versions: 8

Compression:

Stored size: 682 Bytes

Contents

# frozen_string_literal: true

module Strings
  module Fold
    LINE_BREAK = "(\r\n+|\r+|\n+|\t+)".freeze

    # Fold a multiline text into a single line string
    #
    # @example
    #   fold("\tfoo \r\n\n bar") # => "foo  bar"
    #
    # @param [String] text
    #
    # @param [String] separator
    #   the separators to be removed from the text, default: (\r\n+|\r+|\n+|\t+)
    #
    # @return [String]
    #
    # @api public
    def fold(text, separator = LINE_BREAK)
      text.gsub(/([ ]+)#{separator}/, "\\1")
          .gsub(/#{separator}(?<space>[ ]+)/, "\\k<space>")
          .gsub(/#{separator}/, ' ')
    end
    module_function :fold
  end # Fold
end # Strings

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
strings-0.1.7 lib/strings/fold.rb
strings-0.1.6 lib/strings/fold.rb
strings-0.1.5 lib/strings/fold.rb
strings-0.1.4 lib/strings/fold.rb
strings-0.1.3 lib/strings/fold.rb
strings-0.1.2 lib/strings/fold.rb
strings-0.1.1 lib/strings/fold.rb
strings-0.1.0 lib/strings/fold.rb