diff --git a/release_notes.md b/release_notes.md index 5577ff553..d84c889df 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1,5 +1,7 @@ ###In Development + - [#392](https://github.com/MehdiK/Humanizer/issues/392): Default implementation for collection formatter using regular style instead of an exception. Default separator is ampercent. + [Commits](https://github.com/MehdiK/Humanizer/compare/v1.34.0...master) ###v1.34.0 - 2015-03-04 diff --git a/src/Humanizer.Tests/Localisation/DefaultFormatterTests.cs b/src/Humanizer.Tests/Localisation/DefaultFormatterTests.cs index e00222ce5..f2bb86d71 100644 --- a/src/Humanizer.Tests/Localisation/DefaultFormatterTests.cs +++ b/src/Humanizer.Tests/Localisation/DefaultFormatterTests.cs @@ -1,5 +1,6 @@ using System; using System.Globalization; +using System.Threading; using Humanizer.Localisation; using Humanizer.Localisation.Formatters; using Xunit; @@ -20,5 +21,25 @@ public void TimeSpanHumanizeThrowsExceptionForTimeUnitsLargerThanWeek(TimeUnit t { Assert.Throws(() => new DefaultFormatter(CultureInfo.InvariantCulture.Name).TimeSpanHumanize(timeUnit, unit)); } + + [Fact] + public void Issue_392_A_collection_formatter_for_the_current_culture_has_not_been_implemented_yet() + { + var originalCulture = Thread.CurrentThread.CurrentCulture; + var originalUiCulture = Thread.CurrentThread.CurrentUICulture; + + Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("es"); + Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("es"); + + // start: code from issue report + var a = new[] { DateTime.UtcNow, DateTime.UtcNow.AddDays(10) }; + var b = a.Humanize(); // THROWS! + // end: code from issue report + + Assert.Equal(a[0] + " & " + a[1], b); + + Thread.CurrentThread.CurrentCulture = originalCulture; + Thread.CurrentThread.CurrentUICulture = originalUiCulture; + } } } \ No newline at end of file diff --git a/src/Humanizer/Configuration/CollectionFormatterRegistry.cs b/src/Humanizer/Configuration/CollectionFormatterRegistry.cs index 7df70f243..a75e026bc 100644 --- a/src/Humanizer/Configuration/CollectionFormatterRegistry.cs +++ b/src/Humanizer/Configuration/CollectionFormatterRegistry.cs @@ -5,16 +5,16 @@ namespace Humanizer.Configuration internal class CollectionFormatterRegistry : LocaliserRegistry { public CollectionFormatterRegistry() - : base(new DefaultCollectionFormatter()) + : base(new DefaultCollectionFormatter("&")) { Register("en", new OxfordStyleCollectionFormatter("and")); - Register("it", new RegularStyleCollectionFormatter("e")); - Register("de", new RegularStyleCollectionFormatter("und")); - Register("dk", new RegularStyleCollectionFormatter("og")); - Register("nl", new RegularStyleCollectionFormatter("en")); - Register("pt", new RegularStyleCollectionFormatter("e")); - Register("nn", new RegularStyleCollectionFormatter("og")); - Register("nb", new RegularStyleCollectionFormatter("og")); + Register("it", new DefaultCollectionFormatter("e")); + Register("de", new DefaultCollectionFormatter("und")); + Register("dk", new DefaultCollectionFormatter("og")); + Register("nl", new DefaultCollectionFormatter("en")); + Register("pt", new DefaultCollectionFormatter("e")); + Register("nn", new DefaultCollectionFormatter("og")); + Register("nb", new DefaultCollectionFormatter("og")); } } } \ No newline at end of file diff --git a/src/Humanizer/Humanizer.csproj b/src/Humanizer/Humanizer.csproj index 78d0e49b4..0d82a62eb 100644 --- a/src/Humanizer/Humanizer.csproj +++ b/src/Humanizer/Humanizer.csproj @@ -53,7 +53,6 @@ - diff --git a/src/Humanizer/Localisation/CollectionFormatters/DefaultCollectionFormatter.cs b/src/Humanizer/Localisation/CollectionFormatters/DefaultCollectionFormatter.cs index 525bea9b5..95481becc 100644 --- a/src/Humanizer/Localisation/CollectionFormatters/DefaultCollectionFormatter.cs +++ b/src/Humanizer/Localisation/CollectionFormatters/DefaultCollectionFormatter.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; namespace Humanizer.Localisation.CollectionFormatters { @@ -7,6 +8,11 @@ class DefaultCollectionFormatter : ICollectionFormatter { protected String DefaultSeparator = ""; + public DefaultCollectionFormatter(string defaultSeparator) + { + DefaultSeparator = defaultSeparator; + } + public virtual string Humanize(IEnumerable collection) { return Humanize(collection, o => o.ToString(), DefaultSeparator); @@ -24,7 +30,26 @@ public virtual string Humanize(IEnumerable collection, String separator) public virtual string Humanize(IEnumerable collection, Func objectFormatter, String separator) { - throw new NotImplementedException("A collection formatter for the current culture has not been implemented yet."); + if (collection == null) + throw new ArgumentException("collection"); + + T[] itemsArray = collection as T[] ?? collection.ToArray(); + + int count = itemsArray.Length; + + if (count == 0) + return ""; + + if (count == 1) + return objectFormatter(itemsArray[0]); + + IEnumerable itemsBeforeLast = itemsArray.Take(count - 1); + T lastItem = itemsArray.Skip(count - 1).First(); + + return String.Format("{0} {1} {2}", + String.Join(", ", itemsBeforeLast.Select(objectFormatter)), + separator, + objectFormatter(lastItem)); } } } diff --git a/src/Humanizer/Localisation/CollectionFormatters/OxfordStyleCollectionFormatter.cs b/src/Humanizer/Localisation/CollectionFormatters/OxfordStyleCollectionFormatter.cs index 14c04da2c..093930b34 100644 --- a/src/Humanizer/Localisation/CollectionFormatters/OxfordStyleCollectionFormatter.cs +++ b/src/Humanizer/Localisation/CollectionFormatters/OxfordStyleCollectionFormatter.cs @@ -7,8 +7,8 @@ namespace Humanizer.Localisation.CollectionFormatters internal class OxfordStyleCollectionFormatter : DefaultCollectionFormatter { public OxfordStyleCollectionFormatter(string defaultSeparator) + : base(defaultSeparator ?? "and") { - DefaultSeparator = defaultSeparator ?? "and"; } public override string Humanize(IEnumerable collection, Func objectFormatter, String separator) diff --git a/src/Humanizer/Localisation/CollectionFormatters/RegularStyleCollectionFormatter.cs b/src/Humanizer/Localisation/CollectionFormatters/RegularStyleCollectionFormatter.cs deleted file mode 100644 index d7891d095..000000000 --- a/src/Humanizer/Localisation/CollectionFormatters/RegularStyleCollectionFormatter.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Humanizer.Localisation.CollectionFormatters -{ - internal class RegularStyleCollectionFormatter : DefaultCollectionFormatter - { - public RegularStyleCollectionFormatter(string defaultSeparator) - { - DefaultSeparator = defaultSeparator ?? "and"; - } - - public override string Humanize(IEnumerable collection, Func objectFormatter, String separator) - { - if (collection == null) - throw new ArgumentException("collection"); - - T[] itemsArray = collection as T[] ?? collection.ToArray(); - - int count = itemsArray.Length; - - if (count == 0) - return ""; - - if (count == 1) - return objectFormatter(itemsArray[0]); - - IEnumerable itemsBeforeLast = itemsArray.Take(count - 1); - T lastItem = itemsArray.Skip(count - 1).First(); - - return String.Format("{0} {1} {2}", - String.Join(", ", itemsBeforeLast.Select(objectFormatter)), - separator, - objectFormatter(lastItem)); - } - } -}