< Summary

Information
Class: MyNet.Globalization.Facade.InflectorExtensions
Assembly: MyNet.Globalization
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Globalization/Facade/Extensions/InflectorExtensions.cs
Tag: 342_31701152804
Line coverage
66%
Covered lines: 24
Uncovered lines: 12
Coverable lines: 36
Total lines: 224
Line coverage: 66.6%
Branch coverage
88%
Covered branches: 30
Total branches: 34
Branch coverage: 88.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Inflect(...)100%210%
Inflect(...)100%210%
Inflect(...)0%620%
Pluralize(...)100%1010100%
Singularize(...)92.85%141490.9%
LooksLikeSimpleSuffixPlural(...)87.5%88100%
GetPluralCategory(...)100%210%
IsPlural(...)100%210%
GetPluralCategory(...)100%210%
IsPlural(...)100%210%
GetPluralCategory(...)100%210%
IsPlural(...)100%210%
GetPluralCategory(...)100%210%
IsPlural(...)100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Globalization/Facade/Extensions/InflectorExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="InflectorExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Globalization;
 9using MyNet.Globalization.Inflection;
 10
 11#pragma warning disable IDE0130 // Namespace does not match folder structure
 12namespace MyNet.Globalization.Facade;
 13#pragma warning restore IDE0130 // Namespace does not match folder structure
 14
 15/// <summary>
 16/// Extension methods for inflection and text manipulation.
 17/// </summary>
 18public static class InflectorExtensions
 19{
 20    private const StringComparison Comparison = StringComparison.OrdinalIgnoreCase;
 21
 22    extension(string word)
 23    {
 24        /// <summary>
 25        /// Inflects the word based on the provided quantity and culture. Returns the singular or plural form of the wor
 26        /// </summary>
 27        /// <remarks>
 28        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 29        /// Prefer explicit DI overloads when services are available.
 30        /// </remarks>
 31        /// <param name="quantity">The quantity used to determine the correct form of the word.</param>
 32        /// <param name="culture">The culture used to determine the correct form of the word.</param>
 33        /// <returns>The inflected form of the word.</returns>
 034        public string Inflect(int quantity, CultureInfo? culture = null) => word.Inflect((decimal)quantity, culture);
 35
 36        /// <summary>
 37        /// Inflects the word based on the provided quantity and culture. Returns the singular or plural form of the wor
 38        /// </summary>
 39        /// <remarks>
 40        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 41        /// Prefer explicit DI overloads when services are available.
 42        /// </remarks>
 43        /// <param name="quantity">The quantity used to determine the correct form of the word.</param>
 44        /// <param name="culture">The culture used to determine the correct form of the word.</param>
 45        /// <returns>The inflected form of the word.</returns>
 046        public string Inflect(double quantity, CultureInfo? culture = null) => word.Inflect((decimal)quantity, culture);
 47
 48        /// <summary>
 49        /// Inflects the word based on the provided quantity and culture. Returns the singular or plural form of the wor
 50        /// </summary>
 51        /// <remarks>
 52        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 53        /// Prefer explicit DI overloads when services are available.
 54        /// </remarks>
 55        /// <param name="quantity">The quantity used to determine the correct form of the word.</param>
 56        /// <param name="culture">The culture used to determine the correct form of the word.</param>
 57        /// <returns>The inflected form of the word.</returns>
 058        public string Inflect(decimal quantity, CultureInfo? culture = null) => !quantity.IsPlural(culture) ? word.Singu
 59
 60        /// <summary>
 61        /// Pluralizes the provided input considering irregular words and culture-specific rules.
 62        /// </summary>
 63        /// <remarks>
 64        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 65        /// Prefer explicit DI overloads when services are available.
 66        /// </remarks>
 67        /// <param name="inputIsKnownToBeSingular">
 68        /// When <c>true</c> (default), always pluralizes.
 69        /// When <c>false</c>, returns the word unchanged if it appears to already be in plural form
 70        /// (detected via singularize→re-pluralize round-trip).
 71        /// </param>
 72        /// <param name="culture">Target culture used to resolve the inflector provider.</param>
 73        public string Pluralize(bool inputIsKnownToBeSingular = true, CultureInfo? culture = null)
 74        {
 157575            ArgumentException.ThrowIfNullOrEmpty(word);
 76
 210077            if (inputIsKnownToBeSingular) return Localizer.Pluralization.Pluralize(word, culture);
 105078            var asPlural = Localizer.Pluralization.Pluralize(word, culture);
 105079            if (asPlural == word)
 52580                return word;
 81
 52582            var asSingular = Localizer.Pluralization.Singularize(word, culture);
 83
 84            // Keep irregular plurals (people, children, mice, ...) unchanged when plurality is unknown.
 52585            return asSingular != word && Localizer.Pluralization.Pluralize(asSingular, culture) == word && !string.Looks
 52586                ? word
 52587                : asPlural;
 88        }
 89
 90        /// <summary>
 91        /// Singularizes the provided input considering irregular words and culture-specific rules.
 92        /// </summary>
 93        /// <remarks>
 94        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 95        /// Prefer explicit DI overloads when services are available.
 96        /// </remarks>
 97        /// <param name="inputIsKnownToBePlural">
 98        /// When <c>true</c> (default), always singularizes.
 99        /// When <c>false</c>, returns the word unchanged if it appears to already be in singular form.
 100        /// </param>
 101        /// <param name="skipSimpleWords">
 102        /// When <c>true</c>, words of 3 characters or fewer are returned unchanged to avoid misidentification.
 103        /// </param>
 104        /// <param name="culture">Target culture used to resolve the inflector provider.</param>
 105        public string Singularize(bool inputIsKnownToBePlural = true, bool skipSimpleWords = false, CultureInfo? culture
 106        {
 1584107            ArgumentException.ThrowIfNullOrEmpty(word);
 108
 1584109            if (skipSimpleWords && word.Length <= 3)
 0110                return word;
 111
 2118112            if (inputIsKnownToBePlural) return Localizer.Pluralization.Singularize(word, culture);
 1050113            var asSingular = Localizer.Pluralization.Singularize(word, culture);
 114
 1050115            if (asSingular == word)
 534116                return word;
 117
 516118            var asPlural = Localizer.Pluralization.Pluralize(word, culture);
 119
 120            // Guard against false positives for singular words ending with 's' (bus, status, analysis, ...).
 516121            return asPlural != word && Localizer.Pluralization.Singularize(asPlural, culture) == word && string.LooksLik
 516122                ? word
 516123                : asSingular;
 124        }
 125
 126        private static bool LooksLikeSimpleSuffixPlural(string input, string singularCandidate)
 423127            => input.Equals(singularCandidate + "s", Comparison)
 423128               || input.Equals(singularCandidate + "es", Comparison)
 423129               || (singularCandidate.Length > 1
 423130                   && singularCandidate.EndsWith('y')
 423131                   && input.Equals(singularCandidate[..^1] + "ies", Comparison));
 132    }
 133
 134    extension(decimal count)
 135    {
 136        /// <summary>
 137        /// Gets the plural category for a decimal quantity using the culture-specific provider.
 138        /// Falls back to a simple invariant rule when no provider is registered.
 139        /// </summary>
 140        /// <remarks>
 141        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 142        /// Prefer explicit DI overloads when services are available.
 143        /// </remarks>
 0144        public PluralCategory GetPluralCategory(CultureInfo? culture = null) => Localizer.Pluralization.GetPluralCategor
 145
 146        /// <summary>
 147        /// Returns <c>true</c> when <paramref name="count"/> requires the plural form.
 148        /// Zero is treated as plural (e.g. "0 dogs" in English), only 1 is singular.
 149        /// Falls back to <c>count != 1</c> when no inflector is registered.
 150        /// </summary>
 151        /// <remarks>
 152        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 153        /// Prefer explicit DI overloads when services are available.
 154        /// </remarks>
 0155        public bool IsPlural(CultureInfo? culture = null) => Localizer.Pluralization.IsPlural(count, culture);
 156    }
 157
 158    extension(int count)
 159    {
 160        /// <summary>
 161        /// Gets the plural category for an integer quantity using the culture-specific provider.
 162        /// </summary>
 163        /// <remarks>
 164        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 165        /// Prefer explicit DI overloads when services are available.
 166        /// </remarks>
 0167        public PluralCategory GetPluralCategory(CultureInfo? culture = null) => ((decimal)count).GetPluralCategory(cultu
 168
 169        /// <summary>
 170        /// Returns <c>true</c> when <paramref name="count"/> requires the plural form.
 171        /// Zero is treated as plural. Falls back to <c>count != 1</c> when no inflector is registered.
 172        /// </summary>
 173        /// <remarks>
 174        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 175        /// Prefer explicit DI overloads when services are available.
 176        /// </remarks>
 0177        public bool IsPlural(CultureInfo? culture = null) => ((decimal)count).IsPlural(culture);
 178    }
 179
 180    extension(long count)
 181    {
 182        /// <summary>
 183        /// Gets the plural category for a long quantity using the culture-specific provider.
 184        /// </summary>
 185        /// <remarks>
 186        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 187        /// Prefer explicit DI overloads when services are available.
 188        /// </remarks>
 0189        public PluralCategory GetPluralCategory(CultureInfo? culture = null) => ((decimal)count).GetPluralCategory(cultu
 190
 191        /// <summary>
 192        /// Returns <c>true</c> when <paramref name="count"/> requires the plural form.
 193        /// Zero is treated as plural. Falls back to <c>count != 1</c> when no inflector is registered.
 194        /// </summary>
 195        /// <remarks>
 196        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 197        /// Prefer explicit DI overloads when services are available.
 198        /// </remarks>
 0199        public bool IsPlural(CultureInfo? culture = null) => ((decimal)count).IsPlural(culture);
 200    }
 201
 202    extension(double count)
 203    {
 204        /// <summary>
 205        /// Gets the plural category for a double quantity using the culture-specific provider.
 206        /// </summary>
 207        /// <remarks>
 208        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 209        /// Prefer explicit DI overloads when services are available.
 210        /// </remarks>
 0211        public PluralCategory GetPluralCategory(CultureInfo? culture = null) => ((decimal)count).GetPluralCategory(cultu
 212
 213        /// <summary>
 214        /// Returns <c>true</c> when <paramref name="count"/> requires the plural form.
 215        /// Zero is treated as plural. Falls back to <c>count != 1</c> when no inflector is registered.
 216        /// </summary>
 217        /// <remarks>
 218        /// This overload is a convenience facade that uses <see cref="Localizer"/>.
 219        /// Prefer explicit DI overloads when services are available.
 220        /// </remarks>
 0221        public bool IsPlural(CultureInfo? culture = null) => ((decimal)count).IsPlural(culture);
 222    }
 223}
 224