From 9b05dd26bd719bd8cf231f027c3eb64361c7f538 Mon Sep 17 00:00:00 2001 From: Christopher Govender - Kubiec Date: Sat, 23 Jun 2018 11:20:31 +0100 Subject: [PATCH] Refactored code and fixed failing approval test --- .../ArticlePrefixSortTests.cs | 4 +-- ...provalTest.approve_public_api.approved.txt | 11 +++++++ src/Humanizer/ArticlePrefixSort.cs | 32 +++++++++---------- src/Humanizer/Articles.cs | 3 -- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs b/src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs index 2a77b5e82..8afbc1d33 100644 --- a/src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs +++ b/src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs @@ -6,7 +6,7 @@ namespace Humanizer.Tests public class ArticlePrefixSortTests { [Theory] - [InlineData(new string[] { "Ant", "The Theater", "The apple", "Fox", "Bear" }, new string[] { "Ant", "The apple", "Bear", "Fox", "The Theater" })] + [InlineData(new[] { "Ant", "The Theater", "The apple", "Fox", "Bear" }, new[] { "Ant", "The apple", "Bear", "Fox", "The Theater" })] public void SortStringArrayIgnoringArticlePrefixes(string[] input, string[] expectedOutput) { Assert.Equal(expectedOutput, EnglishArticle.PrependArticleSuffix(EnglishArticle.AppendArticlePrefix(input))); @@ -15,7 +15,7 @@ public void SortStringArrayIgnoringArticlePrefixes(string[] input, string[] expe [Fact] public void An_Empty_String_Array_Throws_ArgumentOutOfRangeException() { - string[] items = new string[] { }; + string[] items = { }; Action action = () => EnglishArticle.AppendArticlePrefix(items); Assert.Throws(action); } 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 341fe8b54..98e8a72ec 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -150,6 +150,17 @@ namespace Humanizer public static string ToOrdinalWords(this System.DateTime input) { } public static string ToOrdinalWords(this System.DateTime input, Humanizer.GrammaticalCase grammaticalCase) { } } + public class static EnglishArticle + { + public static string[] AppendArticlePrefix(string[] items) { } + public static string[] PrependArticleSuffix(string[] appended) { } + } + public enum EnglishArticles + { + A = 0, + An = 1, + The = 2, + } public class static EnumDehumanizeExtensions { public static TTargetEnum DehumanizeTo(this string input) diff --git a/src/Humanizer/ArticlePrefixSort.cs b/src/Humanizer/ArticlePrefixSort.cs index 0f82565cb..2a14e6f6c 100644 --- a/src/Humanizer/ArticlePrefixSort.cs +++ b/src/Humanizer/ArticlePrefixSort.cs @@ -20,15 +20,14 @@ public static string[] AppendArticlePrefix(string[] items) Regex regex = new Regex("^((The)|(the)|(a)|(A)|(An)|(an))\\s\\w+"); string[] transformed = new string[items.Length]; - string article, removed, appended; - for (int i = 0; i < items.Length; i++) + for (var i = 0; i < items.Length; i++) { if (regex.IsMatch(items[i])) { - article = items[i].Substring(0, items[i].IndexOf(" ")); - removed = items[i].Remove(0, items[i].IndexOf(" ")); - appended = $"{removed} {article}"; + string article = items[i].Substring(0, items[i].IndexOf(" ", StringComparison.CurrentCulture)); + string removed = items[i].Remove(0, items[i].IndexOf(" ", StringComparison.CurrentCulture)); + string appended = $"{removed} {article}"; transformed[i] = appended.Trim(); } else @@ -48,43 +47,44 @@ public static string[] AppendArticlePrefix(string[] items) public static string[] PrependArticleSuffix(string[] appended) { string[] inserted = new string[appended.Length]; - string suffix, original; for (int i = 0; i < appended.Length; i++) { + string suffix; + string original; if (appended[i].EndsWith(EnglishArticles.The.ToString())) { - suffix = appended[i].Substring(appended[i].IndexOf(" The")); + suffix = appended[i].Substring(appended[i].IndexOf(" The", StringComparison.CurrentCulture)); original = ToOriginalFormat(appended, suffix, i); inserted[i] = original; } else if (appended[i].EndsWith(EnglishArticles.A.ToString())) { - suffix = appended[i].Substring(appended[i].IndexOf(" A")); + suffix = appended[i].Substring(appended[i].IndexOf(" A", StringComparison.CurrentCulture)); original = ToOriginalFormat(appended, suffix, i); inserted[i] = original; } else if (appended[i].EndsWith(EnglishArticles.An.ToString())) { - suffix = appended[i].Substring(appended[i].IndexOf(" An")); + suffix = appended[i].Substring(appended[i].IndexOf(" An", StringComparison.CurrentCulture)); original = ToOriginalFormat(appended, suffix, i); inserted[i] = original; } - else if (appended[i].EndsWith(EnglishArticles.a.ToString())) + else if (appended[i].EndsWith(EnglishArticles.A.ToString().ToLowerInvariant())) { - suffix = appended[i].Substring(appended[i].IndexOf(" a")); + suffix = appended[i].Substring(appended[i].IndexOf(" a", StringComparison.CurrentCulture)); original = ToOriginalFormat(appended, suffix, i); inserted[i] = original; } - else if (appended[i].EndsWith(EnglishArticles.an.ToString())) + else if (appended[i].EndsWith(EnglishArticles.An.ToString().ToLowerInvariant())) { - suffix = appended[i].Substring(appended[i].IndexOf(" an")); + suffix = appended[i].Substring(appended[i].IndexOf(" an", StringComparison.CurrentCulture)); original = ToOriginalFormat(appended, suffix, i); inserted[i] = original; } - else if (appended[i].EndsWith(EnglishArticles.the.ToString())) + else if (appended[i].EndsWith(EnglishArticles.The.ToString().ToLowerInvariant())) { - suffix = appended[i].Substring(appended[i].IndexOf(" the")); + suffix = appended[i].Substring(appended[i].IndexOf(" the", StringComparison.CurrentCulture)); original = ToOriginalFormat(appended, suffix, i); inserted[i] = original; } @@ -98,7 +98,7 @@ public static string[] PrependArticleSuffix(string[] appended) private static string ToOriginalFormat(string[] appended, string suffix, int i) { - string insertion = appended[i].Remove(appended[i].IndexOf(suffix)); + string insertion = appended[i].Remove(appended[i].IndexOf(suffix, StringComparison.CurrentCulture)); string original = $"{suffix} {insertion}"; return original.Trim(); } diff --git a/src/Humanizer/Articles.cs b/src/Humanizer/Articles.cs index 0911cbc59..05c4d6805 100644 --- a/src/Humanizer/Articles.cs +++ b/src/Humanizer/Articles.cs @@ -5,9 +5,6 @@ /// public enum EnglishArticles { - a, - an, - the, A, An, The