Skip to content

Commit

Permalink
default implementation now does not throw an error, it just concatena…
Browse files Browse the repository at this point in the history
…tes it in regular style with "&" as separator. in already implemented languages, it returns the language specific translation for "and". #392
  • Loading branch information
Thomas Mentzel authored and MehdiK committed Mar 28, 2015
1 parent bbc758c commit 9957d3e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 49 deletions.
16 changes: 8 additions & 8 deletions src/Humanizer/Configuration/CollectionFormatterRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ namespace Humanizer.Configuration
internal class CollectionFormatterRegistry : LocaliserRegistry<ICollectionFormatter>
{
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"));
}
}
}
1 change: 0 additions & 1 deletion src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
<Compile Include="DateTimeHumanizeStrategy\IDateTimeOffsetHumanizeStrategy.cs" />
<Compile Include="DateTimeHumanizeStrategy\PrecisionDateTimeOffsetHumanizeStrategy.cs" />
<Compile Include="Localisation\CollectionFormatters\DefaultCollectionFormatter.cs" />
<Compile Include="Localisation\CollectionFormatters\RegularStyleCollectionFormatter.cs" />
<Compile Include="Localisation\CollectionFormatters\ICollectionFormatter.cs" />
<Compile Include="Localisation\CollectionFormatters\OxfordStyleCollectionFormatter.cs" />
<Compile Include="Localisation\Formatters\SerbianFormatter.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Humanizer.Localisation.CollectionFormatters
{
class DefaultCollectionFormatter : ICollectionFormatter
{
protected String DefaultSeparator = "";

public DefaultCollectionFormatter(string defaultSeparator)
{
DefaultSeparator = defaultSeparator;
}

public virtual string Humanize<T>(IEnumerable<T> collection)
{
return Humanize(collection, o => o.ToString(), DefaultSeparator);
Expand All @@ -24,7 +30,26 @@ public virtual string Humanize<T>(IEnumerable<T> collection, String 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.");
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<T> 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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(IEnumerable<T> collection, Func<T, string> objectFormatter, String separator)
Expand Down

This file was deleted.

0 comments on commit 9957d3e

Please sign in to comment.