Sha256: ca7ad527399a56b08f728a79c40b69a65445c01b07c257d16c4459dd1fb1bfd7
Contents?: true
Size: 1.72 KB
Versions: 125
Compression:
Stored size: 1.72 KB
Contents
# frozen_string_literal: true class ReeText::ToSentence include Ree::FnDSL fn :to_sentence do link :safe_join link :escape_html link :t, from: :ree_i18n end DEFAULTS = { locale: :en } DEFAULT_CONNECTORS = { words_connector: ", ", two_words_connector: " and ", last_word_connector: ", and " } doc(<<~DOC) Converts the array to a comma-separated sentence where the last element is joined by the connector word. to_sentence(["one", "two", "three"]) # => "one, two, and three" to_sentence(["one", "two", "three"], words_connector: " ")) # => "one two, and three" to_sentence(["one", "two", "three"], last_word_connector: ", and also ") # =>"one, two, and also three" to_sentence(["one", "two"], two_words_connector: " & ") # => "one & two" DOC contract( Array, Kwargs[ locale: Symbol ], Ksplat[ words_connector?: String, two_words_connector?: String, last_word_connector?: String ] => String ) def call(array, locale: nil, **opts) locale = locale || DEFAULTS[:locale] i18n_connectors = t("sentence", locale: locale, default_by_locale: :en) DEFAULT_CONNECTORS.merge(i18n_connectors) options = DEFAULT_CONNECTORS.merge(opts) case array.length when 0 "" when 1 escape_html(array[0]) when 2 safe_join([array[0], array[1]], sep: options[:two_words_connector]) else safe_join( [ safe_join( array[0...-1], sep: options[:words_connector] ), options[:last_word_connector], array[-1] ], sep: "" ) end end end
Version data entries
125 entries across 125 versions & 1 rubygems