Revert Utils implementation
This commit is contained in:
parent
b8ddecf322
commit
ecfceb3fee
@ -46,7 +46,7 @@ module Cask
|
|||||||
pkgs = Pathname.glob(cask.staged_path/"**"/"*.pkg").map { |path| path.relative_path_from(cask.staged_path) }
|
pkgs = Pathname.glob(cask.staged_path/"**"/"*.pkg").map { |path| path.relative_path_from(cask.staged_path) }
|
||||||
|
|
||||||
message = "Could not find PKG source file '#{pkg}'"
|
message = "Could not find PKG source file '#{pkg}'"
|
||||||
message += ", found #{::Utils.to_sentence(pkgs.map { |path| "'#{path}'" })} instead" if pkgs.any?
|
message += ", found #{pkgs.map { |path| "'#{path}'" }.to_sentence} instead" if pkgs.any?
|
||||||
message += "."
|
message += "."
|
||||||
|
|
||||||
raise CaskError, message
|
raise CaskError, message
|
||||||
|
@ -57,8 +57,7 @@ module Cask
|
|||||||
if !language && language_blocks
|
if !language && language_blocks
|
||||||
sample_languages = if language_blocks.length > LANGUAGE_BLOCK_LIMIT && !@audit_new_cask
|
sample_languages = if language_blocks.length > LANGUAGE_BLOCK_LIMIT && !@audit_new_cask
|
||||||
sample_keys = language_blocks.keys.sample(LANGUAGE_BLOCK_LIMIT)
|
sample_keys = language_blocks.keys.sample(LANGUAGE_BLOCK_LIMIT)
|
||||||
sample_keys_sentence = ::Utils.to_sentence(sample_keys.map { |lang| lang[0].to_s })
|
ohai "Auditing a sample of available languages: #{sample_keys.map { |lang| lang[0].to_s }.to_sentence}"
|
||||||
ohai "Auditing a sample of available languages: #{sample_keys_sentence}"
|
|
||||||
language_blocks.select { |k| sample_keys.include?(k) }
|
language_blocks.select { |k| sample_keys.include?(k) }
|
||||||
else
|
else
|
||||||
language_blocks
|
language_blocks
|
||||||
@ -68,7 +67,7 @@ module Cask
|
|||||||
audit = audit_languages(l)
|
audit = audit_languages(l)
|
||||||
summary = audit.summary(include_passed: output_passed?, include_warnings: output_warnings?)
|
summary = audit.summary(include_passed: output_passed?, include_warnings: output_warnings?)
|
||||||
if summary.present? && output_summary?(audit)
|
if summary.present? && output_summary?(audit)
|
||||||
ohai "Auditing language: #{::Utils.to_sentence(l.map { |lang| "'#{lang}'" })}" if output_summary?
|
ohai "Auditing language: #{l.map { |lang| "'#{lang}'" }.to_sentence}" if output_summary?
|
||||||
puts summary
|
puts summary
|
||||||
end
|
end
|
||||||
warnings += audit.warnings
|
warnings += audit.warnings
|
||||||
|
@ -46,7 +46,7 @@ module Cask
|
|||||||
next if (versions = cask.versions).empty?
|
next if (versions = cask.versions).empty?
|
||||||
|
|
||||||
puts <<~EOS
|
puts <<~EOS
|
||||||
#{cask} #{::Utils.to_sentence(versions)} #{(versions.count == 1) ? "is" : "are"} still installed.
|
#{cask} #{versions.to_sentence} #{(versions.count == 1) ? "is" : "are"} still installed.
|
||||||
Remove #{(versions.count == 1) ? "it" : "them all"} with `brew uninstall --cask --force #{cask}`.
|
Remove #{(versions.count == 1) ? "it" : "them all"} with `brew uninstall --cask --force #{cask}`.
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
@ -306,7 +306,7 @@ module Cask
|
|||||||
rescue TSort::Cyclic
|
rescue TSort::Cyclic
|
||||||
strongly_connected_components = graph.strongly_connected_components.sort_by(&:count)
|
strongly_connected_components = graph.strongly_connected_components.sort_by(&:count)
|
||||||
cyclic_dependencies = strongly_connected_components.last - [@cask]
|
cyclic_dependencies = strongly_connected_components.last - [@cask]
|
||||||
raise CaskCyclicDependencyError.new(@cask.token, ::Utils.to_sentence(cyclic_dependencies))
|
raise CaskCyclicDependencyError.new(@cask.token, cyclic_dependencies.to_sentence)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -683,8 +683,8 @@ module Homebrew
|
|||||||
"This command does not take named arguments."
|
"This command does not take named arguments."
|
||||||
else
|
else
|
||||||
types << :named if types.empty?
|
types << :named if types.empty?
|
||||||
arg_types = Utils.to_sentence(types.map { |type| type.to_s.tr("_", " ") },
|
arg_types = types.map { |type| type.to_s.tr("_", " ") }
|
||||||
two_words_connector: " or ", last_word_connector: " or ")
|
.to_sentence two_words_connector: " or ", last_word_connector: " or "
|
||||||
|
|
||||||
"This command does not take more than #{maximum} #{arg_types} #{Utils.pluralize("argument", maximum)}."
|
"This command does not take more than #{maximum} #{arg_types} #{Utils.pluralize("argument", maximum)}."
|
||||||
end
|
end
|
||||||
@ -697,8 +697,8 @@ module Homebrew
|
|||||||
sig { params(minimum: Integer, types: T::Array[Symbol]).void }
|
sig { params(minimum: Integer, types: T::Array[Symbol]).void }
|
||||||
def initialize(minimum, types: [])
|
def initialize(minimum, types: [])
|
||||||
types << :named if types.empty?
|
types << :named if types.empty?
|
||||||
arg_types = Utils.to_sentence(types.map { |type| type.to_s.tr("_", " ") },
|
arg_types = types.map { |type| type.to_s.tr("_", " ") }
|
||||||
two_words_connector: " or ", last_word_connector: " or ")
|
.to_sentence two_words_connector: " or ", last_word_connector: " or "
|
||||||
|
|
||||||
super "This command requires at least #{minimum} #{arg_types} #{Utils.pluralize("argument", minimum)}."
|
super "This command requires at least #{minimum} #{arg_types} #{Utils.pluralize("argument", minimum)}."
|
||||||
end
|
end
|
||||||
@ -710,8 +710,8 @@ module Homebrew
|
|||||||
sig { params(minimum: Integer, types: T::Array[Symbol]).void }
|
sig { params(minimum: Integer, types: T::Array[Symbol]).void }
|
||||||
def initialize(minimum, types: [])
|
def initialize(minimum, types: [])
|
||||||
types << :named if types.empty?
|
types << :named if types.empty?
|
||||||
arg_types = Utils.to_sentence(types.map { |type| type.to_s.tr("_", " ") },
|
arg_types = types.map { |type| type.to_s.tr("_", " ") }
|
||||||
two_words_connector: " or ", last_word_connector: " or ")
|
.to_sentence two_words_connector: " or ", last_word_connector: " or "
|
||||||
|
|
||||||
super "This command requires exactly #{minimum} #{arg_types} #{Utils.pluralize("argument", minimum)}."
|
super "This command requires exactly #{minimum} #{arg_types} #{Utils.pluralize("argument", minimum)}."
|
||||||
end
|
end
|
||||||
|
@ -40,8 +40,7 @@ module Homebrew
|
|||||||
case args.named.first
|
case args.named.first
|
||||||
when nil, "state"
|
when nil, "state"
|
||||||
if env_vars.any?
|
if env_vars.any?
|
||||||
env_vars_str = Utils.to_sentence(env_vars)
|
puts "Developer mode is enabled because #{env_vars.to_sentence} #{(env_vars.count == 1) ? "is" : "are"} set."
|
||||||
puts "Developer mode is enabled because #{env_vars_str} #{(env_vars.count == 1) ? "is" : "are"} set."
|
|
||||||
elsif Homebrew::Settings.read("devcmdrun") == "true"
|
elsif Homebrew::Settings.read("devcmdrun") == "true"
|
||||||
puts "Developer mode is enabled."
|
puts "Developer mode is enabled."
|
||||||
else
|
else
|
||||||
@ -51,7 +50,7 @@ module Homebrew
|
|||||||
Homebrew::Settings.write "devcmdrun", true
|
Homebrew::Settings.write "devcmdrun", true
|
||||||
when "off"
|
when "off"
|
||||||
Homebrew::Settings.delete "devcmdrun"
|
Homebrew::Settings.delete "devcmdrun"
|
||||||
puts "To fully disable developer mode, you must unset #{Utils.to_sentence(env_vars)}." if env_vars.any?
|
puts "To fully disable developer mode, you must unset #{env_vars.to_sentence}." if env_vars.any?
|
||||||
else
|
else
|
||||||
raise UsageError, "unknown subcommand: #{args.named.first}"
|
raise UsageError, "unknown subcommand: #{args.named.first}"
|
||||||
end
|
end
|
||||||
|
@ -234,7 +234,7 @@ module Homebrew
|
|||||||
unless updated_taps.empty?
|
unless updated_taps.empty?
|
||||||
auto_update_header args: args
|
auto_update_header args: args
|
||||||
noun = Utils.pluralize("tap", updated_taps.count)
|
noun = Utils.pluralize("tap", updated_taps.count)
|
||||||
puts "Updated #{updated_taps.count} #{noun} (#{Utils.to_sentence(updated_taps)})."
|
puts "Updated #{updated_taps.count} #{noun} (#{updated_taps.to_sentence})."
|
||||||
updated = true
|
updated = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -286,7 +286,7 @@ module Homebrew
|
|||||||
error_sources << "#{cask_count} #{Utils.pluralize("cask", cask_count)}" if cask_count.positive?
|
error_sources << "#{cask_count} #{Utils.pluralize("cask", cask_count)}" if cask_count.positive?
|
||||||
error_sources << "#{tap_count} #{Utils.pluralize("tap", tap_count)}" if tap_count.positive?
|
error_sources << "#{tap_count} #{Utils.pluralize("tap", tap_count)}" if tap_count.positive?
|
||||||
|
|
||||||
errors_summary += " in #{Utils.to_sentence(error_sources)}" if error_sources.any?
|
errors_summary += " in #{error_sources.to_sentence}" if error_sources.any?
|
||||||
|
|
||||||
errors_summary += " detected"
|
errors_summary += " detected"
|
||||||
|
|
||||||
|
@ -18,7 +18,6 @@ module Homebrew
|
|||||||
|
|
||||||
sig { returns(CLI::Parser) }
|
sig { returns(CLI::Parser) }
|
||||||
def contributions_args
|
def contributions_args
|
||||||
supported_repos_sentence = Utils.to_sentence(SUPPORTED_REPOS.map { |t| "`#{t}`" })
|
|
||||||
Homebrew::CLI::Parser.new do
|
Homebrew::CLI::Parser.new do
|
||||||
usage_banner "`contributions` [--user=<email|username>] [<--repositories>`=`] [<--csv>]"
|
usage_banner "`contributions` [--user=<email|username>] [<--repositories>`=`] [<--csv>]"
|
||||||
description <<~EOS
|
description <<~EOS
|
||||||
@ -27,7 +26,7 @@ module Homebrew
|
|||||||
|
|
||||||
comma_array "--repositories",
|
comma_array "--repositories",
|
||||||
description: "Specify a comma-separated (no spaces) list of repositories to search. " \
|
description: "Specify a comma-separated (no spaces) list of repositories to search. " \
|
||||||
"Supported repositories: #{supported_repos_sentence}. " \
|
"Supported repositories: #{SUPPORTED_REPOS.map { |t| "`#{t}`" }.to_sentence}. " \
|
||||||
"Omitting this flag, or specifying `--repositories=all`, searches all repositories. " \
|
"Omitting this flag, or specifying `--repositories=all`, searches all repositories. " \
|
||||||
"Use `--repositories=primary` to search only the main repositories: brew,core,cask."
|
"Use `--repositories=primary` to search only the main repositories: brew,core,cask."
|
||||||
flag "--from=",
|
flag "--from=",
|
||||||
|
@ -37,7 +37,7 @@ module Homebrew
|
|||||||
members.each do |group, hash|
|
members.each do |group, hash|
|
||||||
hash.slice!(*public_members)
|
hash.slice!(*public_members)
|
||||||
hash.each { |login, name| hash[login] = "[#{name}](https://github.com/#{login})" }
|
hash.each { |login, name| hash[login] = "[#{name}](https://github.com/#{login})" }
|
||||||
sentences[group] = Utils.to_sentence(hash.values.sort)
|
sentences[group] = hash.values.sort.to_sentence
|
||||||
end
|
end
|
||||||
|
|
||||||
readme = HOMEBREW_REPOSITORY/"README.md"
|
readme = HOMEBREW_REPOSITORY/"README.md"
|
||||||
|
@ -57,7 +57,7 @@ module Homebrew
|
|||||||
|
|
||||||
readme = HOMEBREW_REPOSITORY/"README.md"
|
readme = HOMEBREW_REPOSITORY/"README.md"
|
||||||
content = readme.read
|
content = readme.read
|
||||||
content.gsub!(/(Homebrew is generously supported by) .*\Z/m, "\\1 #{Utils.to_sentence(named_sponsors)}.\n")
|
content.gsub!(/(Homebrew is generously supported by) .*\Z/m, "\\1 #{named_sponsors.to_sentence}.\n")
|
||||||
content << "\n#{logo_sponsors.join}\n" if logo_sponsors.presence
|
content << "\n#{logo_sponsors.join}\n" if logo_sponsors.presence
|
||||||
|
|
||||||
File.write(readme, content)
|
File.write(readme, content)
|
||||||
|
@ -911,7 +911,7 @@ module Homebrew
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
taps = Utils.pluralize("tap", error_tap_paths.count)
|
taps = Utils.pluralize("tap", error_tap_paths.count)
|
||||||
"Unable to read from cask #{taps}: #{Utils.to_sentence(error_tap_paths)}" if error_tap_paths.present?
|
"Unable to read from cask #{taps}: #{error_tap_paths.to_sentence}" if error_tap_paths.present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_cask_load_path
|
def check_cask_load_path
|
||||||
|
@ -100,8 +100,7 @@ class FormulaOrCaskUnavailableError < RuntimeError
|
|||||||
similar_formula_names = Formula.fuzzy_search(name)
|
similar_formula_names = Formula.fuzzy_search(name)
|
||||||
return "" if similar_formula_names.blank?
|
return "" if similar_formula_names.blank?
|
||||||
|
|
||||||
"Did you mean #{Utils.to_sentence(similar_formula_names, two_words_connector: " or ",
|
"Did you mean #{similar_formula_names.to_sentence two_words_connector: " or ", last_word_connector: " or "}?"
|
||||||
last_word_connector: " or ")}?"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { returns(String) }
|
sig { returns(String) }
|
||||||
@ -571,7 +570,7 @@ class UnbottledError < RuntimeError
|
|||||||
msg = +<<~EOS
|
msg = +<<~EOS
|
||||||
The following #{Utils.pluralize("formula", formulae.count, plural: "e")} cannot be installed from #{Utils.pluralize("bottle", formulae.count)} and must be
|
The following #{Utils.pluralize("formula", formulae.count, plural: "e")} cannot be installed from #{Utils.pluralize("bottle", formulae.count)} and must be
|
||||||
built from source.
|
built from source.
|
||||||
#{Utils.to_sentence(formulae)}
|
#{formulae.to_sentence}
|
||||||
EOS
|
EOS
|
||||||
msg += "#{DevelopmentTools.installation_instructions}\n" unless DevelopmentTools.installed?
|
msg += "#{DevelopmentTools.installation_instructions}\n" unless DevelopmentTools.installed?
|
||||||
msg.freeze
|
msg.freeze
|
||||||
@ -805,8 +804,7 @@ class CyclicDependencyError < RuntimeError
|
|||||||
def initialize(strongly_connected_components)
|
def initialize(strongly_connected_components)
|
||||||
super <<~EOS
|
super <<~EOS
|
||||||
The following packages contain cyclic dependencies:
|
The following packages contain cyclic dependencies:
|
||||||
#{strongly_connected_components.select { |packages| packages.count > 1 }
|
#{strongly_connected_components.select { |packages| packages.count > 1 }.map(&:to_sentence).join("\n ")}
|
||||||
.map { |p| Utils.to_sentence(p) }.join("\n ")}
|
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -652,7 +652,7 @@ class FormulaInstaller
|
|||||||
puts "All dependencies for #{formula.full_name} are satisfied."
|
puts "All dependencies for #{formula.full_name} are satisfied."
|
||||||
elsif !deps.empty?
|
elsif !deps.empty?
|
||||||
oh1 "Installing dependencies for #{formula.full_name}: " \
|
oh1 "Installing dependencies for #{formula.full_name}: " \
|
||||||
"#{Utils.to_sentence(deps.map(&:first).map(&Formatter.method(:identifier)))}",
|
"#{deps.map(&:first).map(&Formatter.method(:identifier)).to_sentence}",
|
||||||
truncate: false
|
truncate: false
|
||||||
deps.each { |dep, options| install_dependency(dep, options) }
|
deps.each { |dep, options| install_dependency(dep, options) }
|
||||||
end
|
end
|
||||||
@ -1163,7 +1163,7 @@ class FormulaInstaller
|
|||||||
return if deps.empty?
|
return if deps.empty?
|
||||||
|
|
||||||
oh1 "Fetching dependencies for #{formula.full_name}: " \
|
oh1 "Fetching dependencies for #{formula.full_name}: " \
|
||||||
"#{Utils.to_sentence(deps.map(&:first).map(&Formatter.method(:identifier)))}",
|
"#{deps.map(&:first).map(&Formatter.method(:identifier)).to_sentence}",
|
||||||
truncate: false
|
truncate: false
|
||||||
|
|
||||||
deps.each { |dep, _options| fetch_dependency(dep) }
|
deps.each { |dep, _options| fetch_dependency(dep) }
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
# typed: strict
|
# typed: strict
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# TODO: remove this (and avoid further active support in rubocops)
|
||||||
|
require "active_support/core_ext/array/conversions"
|
||||||
|
|
||||||
require_relative "io_read"
|
require_relative "io_read"
|
||||||
require_relative "move_to_extend_os"
|
require_relative "move_to_extend_os"
|
||||||
require_relative "shell_commands"
|
require_relative "shell_commands"
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
require "ast_constants"
|
require "ast_constants"
|
||||||
require "rubocops/extend/formula_cop"
|
require "rubocops/extend/formula_cop"
|
||||||
require "utils/array"
|
|
||||||
|
|
||||||
module RuboCop
|
module RuboCop
|
||||||
module Cop
|
module Cop
|
||||||
@ -76,8 +75,8 @@ module RuboCop
|
|||||||
[:url, :version, :sha256],
|
[:url, :version, :sha256],
|
||||||
[:url, :mirror, :version, :sha256],
|
[:url, :mirror, :version, :sha256],
|
||||||
]
|
]
|
||||||
minimum_methods = Utils.to_sentence(allowed_methods.first.map { |m| "`#{m}`" })
|
minimum_methods = allowed_methods.first.map { |m| "`#{m}`" }.to_sentence
|
||||||
maximum_methods = Utils.to_sentence(allowed_methods.last.map { |m| "`#{m}`" })
|
maximum_methods = allowed_methods.last.map { |m| "`#{m}`" }.to_sentence
|
||||||
|
|
||||||
on_system_bodies.each do |on_system_block, on_system_body|
|
on_system_bodies.each do |on_system_block, on_system_body|
|
||||||
method_name = on_system_block.method_name
|
method_name = on_system_block.method_name
|
||||||
|
@ -326,7 +326,7 @@ class Tap
|
|||||||
Commands.rebuild_commands_completion_list
|
Commands.rebuild_commands_completion_list
|
||||||
link_completions_and_manpages
|
link_completions_and_manpages
|
||||||
|
|
||||||
formatted_contents = contents.presence&.then { |c| Utils.to_sentence(c) }&.dup&.prepend(" ")
|
formatted_contents = contents.presence&.to_sentence&.dup&.prepend(" ")
|
||||||
$stderr.puts "Tapped#{formatted_contents} (#{path.abv})." unless quiet
|
$stderr.puts "Tapped#{formatted_contents} (#{path.abv})." unless quiet
|
||||||
CacheStoreDatabase.use(:descriptions) do |db|
|
CacheStoreDatabase.use(:descriptions) do |db|
|
||||||
DescriptionCacheStore.new(db)
|
DescriptionCacheStore.new(db)
|
||||||
@ -414,7 +414,7 @@ class Tap
|
|||||||
$stderr.puts "Untapping #{name}..."
|
$stderr.puts "Untapping #{name}..."
|
||||||
|
|
||||||
abv = path.abv
|
abv = path.abv
|
||||||
formatted_contents = contents.presence&.then { |c| Utils.to_sentence(c) }&.dup&.prepend(" ")
|
formatted_contents = contents.presence&.to_sentence&.dup&.prepend(" ")
|
||||||
|
|
||||||
CacheStoreDatabase.use(:descriptions) do |db|
|
CacheStoreDatabase.use(:descriptions) do |db|
|
||||||
DescriptionCacheStore.new(db)
|
DescriptionCacheStore.new(db)
|
||||||
|
@ -52,7 +52,7 @@ module Homebrew
|
|||||||
if rack.directory?
|
if rack.directory?
|
||||||
versions = rack.subdirs.map(&:basename)
|
versions = rack.subdirs.map(&:basename)
|
||||||
puts <<~EOS
|
puts <<~EOS
|
||||||
#{keg.name} #{Utils.to_sentence(versions)} #{(versions.count == 1) ? "is" : "are"} still installed.
|
#{keg.name} #{versions.to_sentence} #{(versions.count == 1) ? "is" : "are"} still installed.
|
||||||
To remove all versions, run:
|
To remove all versions, run:
|
||||||
brew uninstall --force #{keg.name}
|
brew uninstall --force #{keg.name}
|
||||||
EOS
|
EOS
|
||||||
@ -136,7 +136,7 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
|
|
||||||
def are_required_by_deps
|
def are_required_by_deps
|
||||||
"#{(reqs.count == 1) ? "is" : "are"} required by #{Utils.to_sentence(deps)}, " \
|
"#{(reqs.count == 1) ? "is" : "are"} required by #{deps.to_sentence}, " \
|
||||||
"which #{(deps.count == 1) ? "is" : "are"} currently installed"
|
"which #{(deps.count == 1) ? "is" : "are"} currently installed"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -145,7 +145,7 @@ module Homebrew
|
|||||||
class DeveloperDependentsMessage < DependentsMessage
|
class DeveloperDependentsMessage < DependentsMessage
|
||||||
def output
|
def output
|
||||||
opoo <<~EOS
|
opoo <<~EOS
|
||||||
#{Utils.to_sentence(reqs)} #{are_required_by_deps}.
|
#{reqs.to_sentence} #{are_required_by_deps}.
|
||||||
You can silence this warning with:
|
You can silence this warning with:
|
||||||
#{sample_command}
|
#{sample_command}
|
||||||
EOS
|
EOS
|
||||||
@ -156,7 +156,7 @@ module Homebrew
|
|||||||
class NondeveloperDependentsMessage < DependentsMessage
|
class NondeveloperDependentsMessage < DependentsMessage
|
||||||
def output
|
def output
|
||||||
ofail <<~EOS
|
ofail <<~EOS
|
||||||
Refusing to uninstall #{Utils.to_sentence(reqs)}
|
Refusing to uninstall #{reqs.to_sentence}
|
||||||
because #{(reqs.count == 1) ? "it" : "they"} #{are_required_by_deps}.
|
because #{(reqs.count == 1) ? "it" : "they"} #{are_required_by_deps}.
|
||||||
You can override this and force removal with:
|
You can override this and force removal with:
|
||||||
#{sample_command}
|
#{sample_command}
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
require "time"
|
require "time"
|
||||||
|
|
||||||
require "utils/analytics"
|
require "utils/analytics"
|
||||||
require "utils/array"
|
|
||||||
require "utils/curl"
|
require "utils/curl"
|
||||||
require "utils/fork"
|
require "utils/fork"
|
||||||
require "utils/formatter"
|
require "utils/formatter"
|
||||||
|
@ -1,53 +0,0 @@
|
|||||||
# typed: strict
|
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module Utils
|
|
||||||
extend T::Sig
|
|
||||||
|
|
||||||
# Converts the array to a comma-separated sentence where the last element is
|
|
||||||
# joined by the connector word.
|
|
||||||
#
|
|
||||||
# You can pass the following options to change the default behavior. If you
|
|
||||||
# pass an option key that doesn't exist in the list below, it will raise an
|
|
||||||
# <tt>ArgumentError</tt>.
|
|
||||||
#
|
|
||||||
# ==== Options
|
|
||||||
#
|
|
||||||
# * <tt>:words_connector</tt> - The sign or word used to join all but the last
|
|
||||||
# element in arrays with three or more elements (default: ", ").
|
|
||||||
# * <tt>:last_word_connector</tt> - The sign or word used to join the last element
|
|
||||||
# in arrays with three or more elements (default: ", and ").
|
|
||||||
# * <tt>:two_words_connector</tt> - The sign or word used to join the elements
|
|
||||||
# in arrays with two elements (default: " and ").
|
|
||||||
#
|
|
||||||
# ==== Examples
|
|
||||||
#
|
|
||||||
# [].to_sentence # => ""
|
|
||||||
# ['one'].to_sentence # => "one"
|
|
||||||
# ['one', 'two'].to_sentence # => "one and two"
|
|
||||||
# ['one', 'two', 'three'].to_sentence # => "one, two, and three"
|
|
||||||
#
|
|
||||||
# ['one', 'two'].to_sentence(two_words_connector: '-')
|
|
||||||
# # => "one-two"
|
|
||||||
#
|
|
||||||
# ['one', 'two', 'three'].to_sentence(words_connector: ' or ', last_word_connector: ' or at least ')
|
|
||||||
# # => "one or two or at least three"
|
|
||||||
# @see https://github.com/rails/rails/blob/v6.1.7.2/activesupport/lib/active_support/core_ext/array/conversions.rb#L10-L85
|
|
||||||
# ActiveSupport implementation
|
|
||||||
sig {
|
|
||||||
params(array: T::Array[String], words_connector: String, two_words_connector: String, last_word_connector: String)
|
|
||||||
.returns(String)
|
|
||||||
}
|
|
||||||
def self.to_sentence(array, words_connector: ", ", two_words_connector: " and ", last_word_connector: ", and ")
|
|
||||||
case array.length
|
|
||||||
when 0
|
|
||||||
+""
|
|
||||||
when 1
|
|
||||||
+(array[0]).to_s
|
|
||||||
when 2
|
|
||||||
+"#{array[0]}#{two_words_connector}#{array[1]}"
|
|
||||||
else
|
|
||||||
+"#{T.must(array[0...-1]).join(words_connector)}#{last_word_connector}#{array[-1]}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
x
Reference in New Issue
Block a user