From 393ae071bb4bba1f1942e98573dec2e6d156aebc Mon Sep 17 00:00:00 2001 From: LuckShiba Date: Sun, 17 Mar 2024 20:31:10 -0300 Subject: [PATCH] add: pluralize int extension --- .../Extensions/IntegerExtensions.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 TakasakiStudio.Lina.Utils/Extensions/IntegerExtensions.cs diff --git a/TakasakiStudio.Lina.Utils/Extensions/IntegerExtensions.cs b/TakasakiStudio.Lina.Utils/Extensions/IntegerExtensions.cs new file mode 100644 index 0000000..93c5769 --- /dev/null +++ b/TakasakiStudio.Lina.Utils/Extensions/IntegerExtensions.cs @@ -0,0 +1,24 @@ +namespace TakasakiStudio.Lina.Utils.Extensions; + +/// +/// Utility integer extensions +/// +public static class IntegerExtensions +{ + /// + /// Return singular or plural string depending on the value, e.g. 1 item, 2 items + /// If plural is not provided, it will be the singular + 's'. + /// If the value is 1 or -1, the singular will be returned, otherwise the plural. + /// + /// The quantity value + /// The word in singular + /// The word in plural. Defaults to the singular + 's' + /// Prepend the value to the word. Defaults to true. + /// The desired word + public static string Pluralize(this int value, string singular, string? plural = null, bool prependValue = true) + { + plural ??= singular + "s"; + var word = Math.Abs(value) == 1 ? singular : plural; + return prependValue ? $"{value} {word}" : word; + } +} \ No newline at end of file