-
Notifications
You must be signed in to change notification settings - Fork 966
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System.Collections.Generic; | ||
using Xunit; | ||
|
||
namespace Humanizer.Tests | ||
{ | ||
public class SomeClass | ||
{ | ||
public string SomeString; | ||
public int SomeInt; | ||
public override string ToString() | ||
{ | ||
return "ToString"; | ||
} | ||
} | ||
|
||
public class CollectionHumanizeTests : AmbientCulture | ||
{ | ||
public CollectionHumanizeTests() : base("en") { } | ||
|
||
[Fact] | ||
public void HumanizeReturnsOnlyNameWhenCollectionContainsOneItem() | ||
{ | ||
var collection = new List<string> { "A String" }; | ||
|
||
Assert.Equal("A String", collection.Humanize()); | ||
} | ||
|
||
[Fact] | ||
public void HumanizeUsesSeparatorWhenMoreThanOneItemIsInCollection() | ||
{ | ||
var collection = new List<string> | ||
{ | ||
"A String", | ||
"Another String", | ||
}; | ||
|
||
Assert.Equal("A String or Another String", collection.Humanize("or")); | ||
} | ||
|
||
[Fact] | ||
public void HumanizeDefaultsSeparatorToAnd() | ||
{ | ||
var collection = new List<string> | ||
{ | ||
"A String", | ||
"Another String", | ||
}; | ||
|
||
Assert.Equal("A String and Another String", collection.Humanize()); | ||
} | ||
|
||
[Fact] | ||
public void HumanizeUsesOxfordComma() | ||
{ | ||
var collection = new List<string> | ||
{ | ||
"A String", | ||
"Another String", | ||
"A Third String", | ||
}; | ||
|
||
Assert.Equal("A String, Another String, or A Third String", collection.Humanize("or")); | ||
} | ||
|
||
private readonly List<SomeClass> _testCollection = new List<SomeClass> | ||
{ | ||
new SomeClass { SomeInt = 1, SomeString = "One" }, | ||
new SomeClass { SomeInt = 2, SomeString = "Two" }, | ||
new SomeClass { SomeInt = 3, SomeString = "Three" } | ||
}; | ||
|
||
[Fact] | ||
public void HumanizeDefaultsToToString() | ||
{ | ||
Assert.Equal("ToString, ToString, or ToString", _testCollection.Humanize("or")); | ||
} | ||
|
||
[Fact] | ||
public void HumanizeUsesObjectFormatter() | ||
{ | ||
var humanized = _testCollection.Humanize(sc => string.Format("SomeObject #{0} - {1}", sc.SomeInt, sc.SomeString)); | ||
Assert.Equal("SomeObject #1 - One, SomeObject #2 - Two, and SomeObject #3 - Three", humanized); | ||
} | ||
|
||
[Fact] | ||
public void HumanizeUsesObjectFormatterWhenSeparatorIsProvided() | ||
{ | ||
var humanized = _testCollection.Humanize(sc => string.Format("SomeObject #{0} - {1}", sc.SomeInt, sc.SomeString), "or"); | ||
Assert.Equal("SomeObject #1 - One, SomeObject #2 - Two, or SomeObject #3 - Three", humanized); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Humanizer.Configuration; | ||
|
||
|
||
namespace Humanizer | ||
{ | ||
/// <summary> | ||
/// Humanizes an IEnumerable into a human readable list | ||
/// </summary> | ||
public static class CollectionHumanizeExtensions | ||
{ | ||
/// <summary> | ||
/// Formats the collection for display, calling ToString() on each object and | ||
/// using the default separator for the current culture. | ||
/// </summary> | ||
/// <returns></returns> | ||
public static string Humanize<T>(this IEnumerable<T> collection) | ||
{ | ||
return Configurator.CollectionFormatter.Humanize(collection); | ||
} | ||
|
||
/// <summary> | ||
/// Formats the collection for display, calling `objectFormatter` on each object | ||
/// and using the default separator for the current culture. | ||
/// </summary> | ||
/// <returns></returns> | ||
public static string Humanize<T>(this IEnumerable<T> collection, Func<T, String> displayFormatter) | ||
{ | ||
if (displayFormatter == null) | ||
throw new ArgumentNullException("displayFormatter"); | ||
|
||
return Configurator.CollectionFormatter.Humanize(collection, displayFormatter); | ||
} | ||
|
||
/// <summary> | ||
/// Formats the collection for display, calling ToString() on each object | ||
/// and using the provided separator. | ||
/// </summary> | ||
/// <returns></returns> | ||
public static string Humanize<T>(this IEnumerable<T> collection, String separator) | ||
{ | ||
|
||
return Configurator.CollectionFormatter.Humanize(collection, separator); | ||
} | ||
|
||
/// <summary> | ||
/// Formats the collection for display, calling `objectFormatter` on each object | ||
/// and using the provided separator. | ||
/// </summary> | ||
/// <returns></returns> | ||
public static string Humanize<T>(this IEnumerable<T> collection, Func<T, String> displayFormatter, String separator) | ||
{ | ||
if (displayFormatter == null) | ||
throw new ArgumentNullException("displayFormatter"); | ||
|
||
return Configurator.CollectionFormatter.Humanize(collection, displayFormatter, separator); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Humanizer/Configuration/CollectionFormatterRegistry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Humanizer.Localisation.CollectionFormatters; | ||
|
||
namespace Humanizer.Configuration | ||
{ | ||
internal class CollectionFormatterRegistry : LocaliserRegistry<ICollectionFormatter> | ||
{ | ||
public CollectionFormatterRegistry() | ||
: base(new DefaultCollectionFormatter()) | ||
{ | ||
Register<EnglishCollectionFormatter>("en"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/Humanizer/Localisation/CollectionFormatters/DefaultCollectionFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Humanizer.Localisation.CollectionFormatters | ||
{ | ||
class DefaultCollectionFormatter : ICollectionFormatter | ||
{ | ||
protected String DefaultSeparator = ""; | ||
|
||
public virtual string Humanize<T>(IEnumerable<T> collection) | ||
{ | ||
return Humanize(collection, o => o.ToString(), DefaultSeparator); | ||
} | ||
|
||
public virtual string Humanize<T>(IEnumerable<T> collection, Func<T, String> objectFormatter) | ||
{ | ||
return Humanize(collection, objectFormatter, DefaultSeparator); | ||
} | ||
|
||
public virtual string Humanize<T>(IEnumerable<T> collection, String separator) | ||
{ | ||
return Humanize(collection, o => o.ToString(), separator); | ||
} | ||
|
||
public virtual string Humanize<T>(IEnumerable<T> collection, Func<T, String> objectFormatter, String separator) | ||
{ | ||
throw new NotImplementedException("A collection formatter for the current culture has not been implemented yet."); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Humanizer/Localisation/CollectionFormatters/EnglishCollectionFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Humanizer.Localisation.CollectionFormatters | ||
{ | ||
internal class EnglishCollectionFormatter : DefaultCollectionFormatter | ||
{ | ||
public EnglishCollectionFormatter() | ||
{ | ||
DefaultSeparator = "and"; | ||
} | ||
|
||
public override string Humanize<T>(IEnumerable<T> collection, Func<T, String> objectFormatter, String separator) | ||
{ | ||
if (collection == null) | ||
throw new ArgumentException("collection"); | ||
|
||
var enumerable = collection as T[] ?? collection.ToArray(); | ||
|
||
int count = enumerable.Count(); | ||
|
||
if (count == 0) | ||
return ""; | ||
|
||
if (count == 1) | ||
return objectFormatter(enumerable.First()); | ||
|
||
string formatString = count > 2 ? "{0}, {1} {2}" : "{0} {1} {2}"; | ||
|
||
return String.Format(formatString, | ||
String.Join(", ", enumerable.Take(count - 1).Select(objectFormatter)), | ||
separator, | ||
objectFormatter(enumerable.Skip(count - 1).First())); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Humanizer/Localisation/CollectionFormatters/ICollectionFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Humanizer.Localisation.CollectionFormatters | ||
{ | ||
/// <summary> | ||
/// An interface you should implement to localize Humanize for collections | ||
/// </summary> | ||
public interface ICollectionFormatter | ||
{ | ||
/// <summary> | ||
/// Formats the collection for display, calling ToString() on each object. | ||
/// </summary> | ||
/// <returns></returns> | ||
String Humanize<T>(IEnumerable<T> collection); | ||
|
||
/// <summary> | ||
/// Formats the collection for display, calling `objectFormatter` on each object. | ||
/// </summary> | ||
/// <returns></returns> | ||
String Humanize<T>(IEnumerable<T> collection, Func<T, String> objectFormatter); | ||
|
||
/// <summary> | ||
/// Formats the collection for display, calling ToString() on each object | ||
/// and using `separator` before the final item. | ||
/// </summary> | ||
/// <returns></returns> | ||
String Humanize<T>(IEnumerable<T> collection, String separator); | ||
|
||
/// <summary> | ||
/// Formats the collection for display, calling `objectFormatter` on each object | ||
/// and using `separator` before the final item. | ||
/// </summary> | ||
/// <returns></returns> | ||
String Humanize<T>(IEnumerable<T> collection, Func<T, String> objectFormatter, String separator); | ||
} | ||
} |