Sha256: 70831d719955d4ea94dde39a7801e519e7532ac75f9fd6ce50326762b95bfc1c

Contents?: true

Size: 683 Bytes

Versions: 1

Compression:

Stored size: 683 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

1 entries across 1 versions & 1 rubygems

Version Path
strings-0.1.8 lib/strings/fold.rb