diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt index c002a5c8a..8c08df24c 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -245,19 +245,19 @@ public class In public class Vocabularies { - public Vocabularies() { } public Humanizer.Inflections.Vocabulary Default { get; } } public class Vocabulary { - public Vocabulary() { } public void AddIrregular(string singular, string plural) { } public void AddPlural(string rule, string replacement) { } public void AddSingular(string rule, string replacement) { } public void AddUncountable(string word) { } public string Pluralize(string word, bool inputIsKnownToBeSingular) { } + public string Pluralize(string word, Humanizer.Plurality plurality) { } public string Singularize(string word, bool inputIsKnownToBePlural) { } + public string Singularize(string word, Humanizer.Plurality plurality) { } } public class InflectorExtensions diff --git a/src/Humanizer/Inflections/Vocabularies.cs b/src/Humanizer/Inflections/Vocabularies.cs index d5d8f2488..753b70db0 100644 --- a/src/Humanizer/Inflections/Vocabularies.cs +++ b/src/Humanizer/Inflections/Vocabularies.cs @@ -3,7 +3,7 @@ namespace Humanizer.Inflections /// /// Container for registered Vocabularies. At present, only a single vocabulary is supported: Default. /// - public class Vocabularies + public static class Vocabularies { private static Vocabulary _default; diff --git a/src/Humanizer/Inflections/Vocabulary.cs b/src/Humanizer/Inflections/Vocabulary.cs index feb457bac..d751b41c2 100644 --- a/src/Humanizer/Inflections/Vocabulary.cs +++ b/src/Humanizer/Inflections/Vocabulary.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Text.RegularExpressions; @@ -10,6 +11,10 @@ namespace Humanizer.Inflections /// public class Vocabulary { + internal Vocabulary() + { + } + private readonly List _plurals = new List(); private readonly List _singulars = new List(); private readonly List _uncountables = new List(); @@ -75,6 +80,18 @@ public string Pluralize(string word, bool inputIsKnownToBeSingular = true) return result; } + /// + /// Pluralizes the provided input considering irregular words + /// + /// Word to be pluralized + /// Normally you call Pluralize on singular words; but if you're unsure call it with Plurality.CouldBeEither + /// + [Obsolete("Use string.Pluralize(bool) instead. This method will be removed in next major release.")] + public string Pluralize(string word, Plurality plurality) + { + return plurality == Plurality.Plural ? word : Pluralize(word, inputIsKnownToBeSingular: false); + } + /// /// Singularizes the provided input considering irregular words /// @@ -83,7 +100,6 @@ public string Pluralize(string word, bool inputIsKnownToBeSingular = true) /// public string Singularize(string word, bool inputIsKnownToBePlural = true) { - var result = ApplyRules(_singulars, word); if (inputIsKnownToBePlural) @@ -98,6 +114,18 @@ public string Singularize(string word, bool inputIsKnownToBePlural = true) return result ?? word; } + /// + /// Singularizes the provided input considering irregular words + /// + /// Word to be singularized + /// Normally you call Singularize on plural words; but if you're unsure call it with Plurality.CouldBeEither + /// + [Obsolete("Use string.Singularize(bool) instead. This method will be removed in next major release.")] + public string Singularize(string word, Plurality plurality) + { + return plurality == Plurality.Singular ? word : Singularize(word, inputIsKnownToBePlural: false); + } + private string ApplyRules(IList rules, string word) { if (word == null) @@ -110,9 +138,7 @@ private string ApplyRules(IList rules, string word) for (int i = rules.Count - 1; i >= 0; i--) { if ((result = rules[i].Apply(word)) != null) - { break; - } } return result; } @@ -136,9 +162,7 @@ public Rule(string pattern, string replacement) public string Apply(string word) { if (!_regex.IsMatch(word)) - { return null; - } return _regex.Replace(word, _replacement); }