Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: pluralize int extension #53

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions TakasakiStudio.Lina.Utils/Extensions/IntegerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace TakasakiStudio.Lina.Utils.Extensions;

/// <summary>
/// Utility integer extensions
/// </summary>
public static class IntegerExtensions
{
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The quantity value</param>
/// <param name="singular">The word in singular</param>
/// <param name="plural">The word in plural. Defaults to the singular + 's'</param>
/// <param name="prependValue">Prepend the value to the word. Defaults to true.</param>
/// <returns>The desired word</returns>
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;
}
}