From c594ffefbc18933fbd89750a8720e1c0576f4ee7 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 11 Mar 2017 11:33:12 +0100 Subject: Add `Formatter::pluralize`. --- Library/Homebrew/utils/formatter.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'Library/Homebrew/utils') diff --git a/Library/Homebrew/utils/formatter.rb b/Library/Homebrew/utils/formatter.rb index 8a9afb9af..88616c6f8 100644 --- a/Library/Homebrew/utils/formatter.rb +++ b/Library/Homebrew/utils/formatter.rb @@ -90,4 +90,20 @@ module Formatter output end + + def pluralize(count, singular, plural = nil) + return "#{count} #{singular}" if count == 1 + + *adjectives, noun = singular.split(" ") + + if plural.nil? + plural = { + "formula" => "formulae", + }.fetch(noun, "#{noun}s") + end + + words = adjectives << plural + + "#{count} #{words.join(" ")}" + end end -- cgit v1.2.3