Skip to main content

C# helper method for creating pluralized words (when applicable).

// ============================================================================
// Pluralize
// ============================================================================

namespace Helpers
{
    public static class StringHelper
    {
        /// <summary>
        /// Perform simple pluralization.
        /// </summary>
        /// <param name="singularForm">The word in singular form.</param>
        /// <param name="howMany">The amount of times <paramref name="singularForm"/> is expressed.</param>
        /// <returns>The exact value of <see cref="singularForm"/> if <paramref name="howMany"/> is equal to 1,
        /// otherwise <see cref="singularForm"/> with an appending "s".</returns>
        /// <example>
        ///     Pluralize("Item", 1) >> "Item"
        ///     Pluralize("Item", 2) >> "Items"
        /// </example>
        /// <remarks>Hat tip: https://stackoverflow.com/a/9192510</remarks>
        public static string Pluralize(string singularForm, int howMany)
        {
            return Pluralize(singularForm, howMany, singularForm + "s");
        }

        /// <summary>
        /// Perform simple pluralization.
        /// </summary>
        /// <param name="singularForm">The word in singular form.</param>
        /// <param name="howMany">The amount of times <paramref name="singularForm"/> is expressed.</param>
        /// <param name="pluralForm">The custom pluralized value to express (when applicable).</param>
        /// <returns>The exact value of <see cref="singularForm"/> if <paramref name="howMany"/> is equal to 1,
        /// otherwise <see cref="singularForm"/> with an appending "s".</returns>
        /// <example>
        ///     Pluralize("Person", 1, "People") >> "Person"
        ///     Pluralize("Person", 2, "People") >> "People"
        /// </example>
        /// <remarks>Hat tip: https://stackoverflow.com/a/9192510</remarks>
        public static string Pluralize(string singularForm, int howMany, string pluralForm)
        {
            return howMany == 1 ? singularForm : pluralForm;
        }
    }
}


// ============================================================================
// Unit Tests
// ============================================================================

namespace Tests
{
    using System;
    using Xunit;

    using Helpers;

    public class StringHelperTests
    {
        #region - Pluralize -

        [Theory]
        [InlineData("Item", 0, "Items")]
        [InlineData("Item", 2, "Items")]
        [InlineData("Item", 3, "Items")]
        public void Words_should_be_pluralized(string word, int howMany, string expect)
        {
            Assert.Equal(StringHelper.Pluralize(word, howMany), expect, StringComparer.Ordinal);
        }

        [Theory]
        [InlineData("Item", 1, "Item")]
        public void Words_should_not_be_pluralized(string word, int howMany, string expect)
        {
            Assert.Equal(StringHelper.Pluralize(word, howMany), expect, StringComparer.Ordinal);
        }

        [Theory]
        [InlineData("Person", 0, "People", "People")]
        [InlineData("Person", 2, "People", "People")]
        [InlineData("Person", 3, "People", "People")]
        public void Word_with_custom_plural_form_should_be_pluralized(string word, int howMany, string pluralizedForm, string expect)
        {
            Assert.Equal(StringHelper.Pluralize(word, howMany, pluralizedForm), expect, StringComparer.Ordinal);
        }

        [Theory]
        [InlineData("Person", 1, "People", "Person")]
        public void Word_with_custom_plural_form_should_not_be_pluralized(string word, int howMany, string pluralizedForm, string expect)
        {
            Assert.Equal(StringHelper.Pluralize(word, howMany, pluralizedForm), expect, StringComparer.Ordinal);
        }

        #endregion
    }
}