Skip to content

Commit

Permalink
Refactored code and fixed failing approval test
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherie authored and Oren Novotny committed Mar 13, 2019
1 parent e453139 commit 9b05dd2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/Humanizer.Tests.Shared/ArticlePrefixSortTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -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<ArgumentOutOfRangeException>(action);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TTargetEnum>(this string input)
Expand Down
32 changes: 16 additions & 16 deletions src/Humanizer/ArticlePrefixSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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();
}
Expand Down
3 changes: 0 additions & 3 deletions src/Humanizer/Articles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
/// </summary>
public enum EnglishArticles
{
a,
an,
the,
A,
An,
The
Expand Down

0 comments on commit 9b05dd2

Please sign in to comment.