-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from Takasaki-Studio/feat/pluralize
add: pluralize int extension
- Loading branch information
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |