From ec0792280ecd2d3e2becc2c550b5c9340e115714 Mon Sep 17 00:00:00 2001 From: Max Malook Date: Tue, 24 Jun 2014 09:44:12 +0200 Subject: [PATCH 1/5] add singularize/pluralize overlead without using obsolete plurality enum --- ...provalTest.approve_public_api.approved.txt | 2 ++ src/Humanizer/InflectorExtensions.cs | 27 +++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) 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 721a6a028..7ab833d8e 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -182,7 +182,9 @@ public class InflectorExtensions public string Hyphenate(string underscoredWord) { } public string Pascalize(string input) { } public string Pluralize(string word, Humanizer.Plurality plurality) { } + public string Pluralize(string word, bool inputIsSingular) { } public string Singularize(string word, Humanizer.Plurality plurality) { } + public string Singularize(string word, bool inputIsPlural) { } public string Titleize(string input) { } public string Underscore(string input) { } } diff --git a/src/Humanizer/InflectorExtensions.cs b/src/Humanizer/InflectorExtensions.cs index 1f2aa2c76..7668bd372 100644 --- a/src/Humanizer/InflectorExtensions.cs +++ b/src/Humanizer/InflectorExtensions.cs @@ -21,6 +21,7 @@ //IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN //CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +using System; using System.Collections.Generic; using System.Text.RegularExpressions; @@ -168,7 +169,7 @@ private static void AddSingular(string rule, string replacement) /// Word to be pluralized /// Normally you call Pluralize on singular words; but if you're unsure call it with Plurality.CouldBeEither /// - public static string Pluralize(this string word, Plurality plurality = Plurality.Singular) + public static string Pluralize(this string word, Plurality plurality) { if (plurality == Plurality.Plural) return word; @@ -186,13 +187,24 @@ public static string Pluralize(this string word, Plurality plurality = Plurality 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 inputIsSingular = false + /// + public static string Pluralize(this string word, bool inputIsSingular = true) + { + return Pluralize(word, inputIsSingular ? Plurality.Singular : Plurality.CouldBeEither); + } + /// /// 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 /// - public static string Singularize(this string word, Plurality plurality = Plurality.Plural) + public static string Singularize(this string word, Plurality plurality) { if (plurality == Plurality.Singular) return word; @@ -211,6 +223,17 @@ public static string Singularize(this string word, Plurality plurality = Plurali 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 inputIsPlural = false + /// + public static string Singularize(this string word, bool inputIsPlural = true) + { + return Singularize(word, inputIsPlural ? Plurality.Plural : Plurality.CouldBeEither); + } + private static string ApplyRules(List rules, string word) { if (word == null) From c2cc4cc484f0fb587dd6d51c49a79fba38a1fb93 Mon Sep 17 00:00:00 2001 From: Max Malook Date: Tue, 24 Jun 2014 09:50:10 +0200 Subject: [PATCH 2/5] add PR to release notes --- release_notes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/release_notes.md b/release_notes.md index b1d9e7faf..cfd3b5ae5 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1,4 +1,5 @@ ###In Development + - [#306](https://github.com/MehdiK/Humanizer/pull/306): Added Singularize/Pluralize overload without using obsolete plurality enum [Commits](https://github.com/MehdiK/Humanizer/compare/v1.27.0...master) From 52f309cf9d1eaa5fe49325205851925e0cca7c51 Mon Sep 17 00:00:00 2001 From: Max Malook Date: Wed, 25 Jun 2014 09:47:07 +0200 Subject: [PATCH 3/5] mark Singularize/Pluralize with Plurality as obsolete --- src/Humanizer.Tests/InflectorTests.cs | 22 +++---------- src/Humanizer/InflectorExtensions.cs | 47 +++++++++++++-------------- src/Humanizer/ToQuantityExtensions.cs | 4 +-- 3 files changed, 28 insertions(+), 45 deletions(-) diff --git a/src/Humanizer.Tests/InflectorTests.cs b/src/Humanizer.Tests/InflectorTests.cs index f8942c925..af5dc5d6f 100644 --- a/src/Humanizer.Tests/InflectorTests.cs +++ b/src/Humanizer.Tests/InflectorTests.cs @@ -39,19 +39,12 @@ public void Pluralize(string singular, string plural) Assert.Equal(plural, singular.Pluralize()); } - [Theory] - [ClassData(typeof(PluralTestSource))] - public void PluralizeAlreadyPluralWord(string singular, string plural) - { - Assert.Equal(plural, plural.Pluralize(Plurality.Plural)); - } - [Theory] [ClassData(typeof(PluralTestSource))] public void PluralizeWordsWithUnknownPlurality(string singular, string plural) { - Assert.Equal(plural, plural.Pluralize(Plurality.CouldBeEither)); - Assert.Equal(plural, singular.Pluralize(Plurality.CouldBeEither)); + Assert.Equal(plural, plural.Pluralize(false)); + Assert.Equal(plural, singular.Pluralize(false)); } [Theory] @@ -61,19 +54,12 @@ public void Singularize(string singular, string plural) Assert.Equal(singular, plural.Singularize()); } - [Theory] - [ClassData(typeof(PluralTestSource))] - public void SingularizeAlreadySingularWord(string singular, string plural) - { - Assert.Equal(singular, singular.Singularize(Plurality.Singular)); - } - [Theory] [ClassData(typeof(PluralTestSource))] public void SingularizeWordsWithUnknownSingularity(string singular, string plural) { - Assert.Equal(singular, singular.Singularize(Plurality.CouldBeEither)); - Assert.Equal(singular, plural.Singularize(Plurality.CouldBeEither)); + Assert.Equal(singular, singular.Singularize(false)); + Assert.Equal(singular, plural.Singularize(false)); } //Uppercases individual words and removes some characters diff --git a/src/Humanizer/InflectorExtensions.cs b/src/Humanizer/InflectorExtensions.cs index 7668bd372..c702d6f8f 100644 --- a/src/Humanizer/InflectorExtensions.cs +++ b/src/Humanizer/InflectorExtensions.cs @@ -169,14 +169,23 @@ private static void AddSingular(string rule, string replacement) /// 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 static string Pluralize(this string word, Plurality plurality) { - if (plurality == Plurality.Plural) - return word; + return plurality == Plurality.Plural ? word : word.Pluralize(inputIsSingular: false); + } + /// + /// 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 inputIsSingular = false + /// + public static string Pluralize(this string word, bool inputIsSingular = true) + { var result = ApplyRules(Plurals, word); - if (plurality == Plurality.Singular) + if (inputIsSingular) return result; var asSingular = ApplyRules(Singulars, word); @@ -188,52 +197,40 @@ public static string Pluralize(this string word, Plurality plurality) } /// - /// Pluralizes the provided input considering irregular words + /// Singularizes 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 inputIsSingular = false + /// Word to be singularized + /// Normally you call Singularize on plural words; but if you're unsure call it with Plurality.CouldBeEither /// - public static string Pluralize(this string word, bool inputIsSingular = true) + [Obsolete("Use string.Singularize(bool) instead. This method will be removed in next major release.")] + public static string Singularize(this string word, Plurality plurality) { - return Pluralize(word, inputIsSingular ? Plurality.Singular : Plurality.CouldBeEither); + return plurality == Plurality.Singular ? word : word.Singularize(inputIsPlural: false); } /// /// 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 + /// Normally you call Singularize on plural words; but if you're unsure call it with inputIsPlural = false /// - public static string Singularize(this string word, Plurality plurality) + public static string Singularize(this string word, bool inputIsPlural = true) { - if (plurality == Plurality.Singular) - return word; var result = ApplyRules(Singulars, word); - if (plurality == Plurality.Plural) + if (inputIsPlural) return result; // the Plurality is unknown so we should check all possibilities var asPlural = ApplyRules(Plurals, word); var asPluralAsSingular = ApplyRules(Singulars, asPlural); - if (asPlural != word && word+"s" != asPlural && asPluralAsSingular == word && result != word) + if (asPlural != word && word + "s" != asPlural && asPluralAsSingular == word && result != word) return word; 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 inputIsPlural = false - /// - public static string Singularize(this string word, bool inputIsPlural = true) - { - return Singularize(word, inputIsPlural ? Plurality.Plural : Plurality.CouldBeEither); - } - private static string ApplyRules(List rules, string word) { if (word == null) diff --git a/src/Humanizer/ToQuantityExtensions.cs b/src/Humanizer/ToQuantityExtensions.cs index 12e844237..0a0466159 100644 --- a/src/Humanizer/ToQuantityExtensions.cs +++ b/src/Humanizer/ToQuantityExtensions.cs @@ -70,8 +70,8 @@ public static string ToQuantity(this string input, int quantity, string format, private static string ToQuantity(this string input, int quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric, string format = null, IFormatProvider formatProvider = null) { var transformedInput = quantity == 1 - ? input.Singularize(Plurality.CouldBeEither) - : input.Pluralize(Plurality.CouldBeEither); + ? input.Singularize(inputIsPlural: false) + : input.Pluralize(inputIsSingular: false); if (showQuantityAs == ShowQuantityAs.None) return transformedInput; From faf6d568796dc117faabd135851541999fc854ed Mon Sep 17 00:00:00 2001 From: Max Malook Date: Wed, 25 Jun 2014 09:53:36 +0200 Subject: [PATCH 4/5] provide better name for new paramters for Singularize/Pluralize --- ...iApprovalTest.approve_public_api.approved.txt | 4 ++-- src/Humanizer/InflectorExtensions.cs | 16 ++++++++-------- src/Humanizer/ToQuantityExtensions.cs | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) 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 7ab833d8e..3f792e8cf 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -182,9 +182,9 @@ public class InflectorExtensions public string Hyphenate(string underscoredWord) { } public string Pascalize(string input) { } public string Pluralize(string word, Humanizer.Plurality plurality) { } - public string Pluralize(string word, bool inputIsSingular) { } + public string Pluralize(string word, bool inputIsKnownToBeSingular) { } public string Singularize(string word, Humanizer.Plurality plurality) { } - public string Singularize(string word, bool inputIsPlural) { } + public string Singularize(string word, bool inputIsKnownToBePlural) { } public string Titleize(string input) { } public string Underscore(string input) { } } diff --git a/src/Humanizer/InflectorExtensions.cs b/src/Humanizer/InflectorExtensions.cs index c702d6f8f..2a07a8c2a 100644 --- a/src/Humanizer/InflectorExtensions.cs +++ b/src/Humanizer/InflectorExtensions.cs @@ -172,20 +172,20 @@ private static void AddSingular(string rule, string replacement) [Obsolete("Use string.Pluralize(bool) instead. This method will be removed in next major release.")] public static string Pluralize(this string word, Plurality plurality) { - return plurality == Plurality.Plural ? word : word.Pluralize(inputIsSingular: false); + return plurality == Plurality.Plural ? word : word.Pluralize(inputIsKnownToBeSingular: false); } /// /// 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 inputIsSingular = false + /// Normally you call Pluralize on singular words; but if you're unsure call it with false /// - public static string Pluralize(this string word, bool inputIsSingular = true) + public static string Pluralize(this string word, bool inputIsKnownToBeSingular = true) { var result = ApplyRules(Plurals, word); - if (inputIsSingular) + if (inputIsKnownToBeSingular) return result; var asSingular = ApplyRules(Singulars, word); @@ -205,21 +205,21 @@ public static string Pluralize(this string word, bool inputIsSingular = true) [Obsolete("Use string.Singularize(bool) instead. This method will be removed in next major release.")] public static string Singularize(this string word, Plurality plurality) { - return plurality == Plurality.Singular ? word : word.Singularize(inputIsPlural: false); + return plurality == Plurality.Singular ? word : word.Singularize(inputIsKnownToBePlural: false); } /// /// 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 inputIsPlural = false + /// Normally you call Singularize on plural words; but if you're unsure call it with false /// - public static string Singularize(this string word, bool inputIsPlural = true) + public static string Singularize(this string word, bool inputIsKnownToBePlural = true) { var result = ApplyRules(Singulars, word); - if (inputIsPlural) + if (inputIsKnownToBePlural) return result; // the Plurality is unknown so we should check all possibilities diff --git a/src/Humanizer/ToQuantityExtensions.cs b/src/Humanizer/ToQuantityExtensions.cs index 0a0466159..574bce730 100644 --- a/src/Humanizer/ToQuantityExtensions.cs +++ b/src/Humanizer/ToQuantityExtensions.cs @@ -70,8 +70,8 @@ public static string ToQuantity(this string input, int quantity, string format, private static string ToQuantity(this string input, int quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric, string format = null, IFormatProvider formatProvider = null) { var transformedInput = quantity == 1 - ? input.Singularize(inputIsPlural: false) - : input.Pluralize(inputIsSingular: false); + ? input.Singularize(inputIsKnownToBePlural: false) + : input.Pluralize(inputIsKnownToBeSingular: false); if (showQuantityAs == ShowQuantityAs.None) return transformedInput; From 66e1a3b076e507bdf3cc94af418655f6d1dd0bd1 Mon Sep 17 00:00:00 2001 From: Max Malook Date: Wed, 25 Jun 2014 10:04:53 +0200 Subject: [PATCH 5/5] update readme with new preferred Singularize/Pluralize API --- readme.md | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index a2a574a02..1ffae5b1c 100644 --- a/readme.md +++ b/readme.md @@ -368,14 +368,17 @@ There are also a few inflector methods: "string".Pluralize() => "strings" ``` -Normally you would call `Pluralize` on a singular word but if you're unsure about the singularity of the word you can call the method with the optional `plurality` argument: +Normally you would call `Pluralize` on a singular word but if you're unsure about the singularity of the word you can call the method with the optional `inputIsKnownToBeSingular` argument: ```C# -"Men".Pluralize(Plurality.CouldBeEither) => "Men" -"Man".Pluralize(Plurality.CouldBeEither) => "Men" -"string".Pluralize(Plurality.CouldBeEither) => "strings" +"Men".Pluralize(inputIsKnownToBeSingular: false) => "Men" +"Man".Pluralize(inputIsKnownToBeSingular: false) => "Men" +"string".Pluralize(inputIsKnownToBeSingular: false) => "strings" ``` + +The overload of `Pluralize` with `plurality` argument is obsolete and will be removed in next major release. + ####Singularize `Singularize` singularizes the provided input while taking irregular and uncountable words into consideration: @@ -384,14 +387,17 @@ Normally you would call `Pluralize` on a singular word but if you're unsure abou "strings".Singularize() => "string" ``` -Normally you would call `Singularize` on a plural word but if you're unsure about the plurality of the word you can call the method with the optional `plurality` argument: +Normally you would call `Singularize` on a plural word but if you're unsure about the plurality of the word you can call the method with the optional `inputIsKnownToBePlural` argument: ```C# -"Men".Singularize(Plurality.CouldBeEither) => "Man" -"Man".Singularize(Plurality.CouldBeEither) => "Man" -"strings".Singularize(Plurality.CouldBeEither) => "string" +"Men".Singularize(inputIsKnownToBePlural: false) => "Man" +"Man".Singularize(inputIsKnownToBePlural: false) => "Man" +"strings".Singularize(inputIsKnownToBePlural: false) => "string" ``` + +The overload of `Singularize` with `plurality` argument is obsolete and will be removed in next major release. + ####ToQuantity Many times you want to call `Singularize` and `Pluralize` to prefix a word with a number; e.g. "2 requests", "3 men". `ToQuantity` prefixes the provided word with the number and accordingly pluralizes or singularizes the word: