Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collection formatter generalized to "OxfordStyle" and "RegularStyle" #386

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
###In Development
- [#381](https://github.com/MehdiK/Humanizer/pull/381): Fixes trailing question mark reported in #378.
- [#382](https://github.com/MehdiK/Humanizer/pull/382): Fix 90000th and -thousandth in RussianNumberToWordsConverter.
- [#386](https://github.com/MehdiK/Humanizer/pull/386): Collection formatter support for German, Danish, Dutch, Portuguese and Norwegian

[Commits](https://github.com/MehdiK/Humanizer/compare/v1.33.7...master)

Expand Down
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<Compile Include="Localisation\bn-BD\NumberToWordsTests.cs" />
<Compile Include="Localisation\bn-BD\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\DefaultFormatterTests.cs" />
<Compile Include="Localisation\de\CollectionFormatterTests.cs" />
<Compile Include="Localisation\nb\DateHumanizeTests.cs" />
<Compile Include="Localisation\nb\TimeSpanHumanizeTests.cs" />
<Compile Include="Localisation\cs\DateHumanizeTests.cs" />
Expand Down
37 changes: 37 additions & 0 deletions src/Humanizer.Tests/Localisation/de/CollectionFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Generic;
using Xunit;

namespace Humanizer.Tests.Localisation.de
{
public class CollectionFormatterTests : AmbientCulture
{
public CollectionFormatterTests()
: base("de")
{
}

[Fact]
public void OneItem()
{
var collection = new List<int>(new int[] {1});
string humanized = "1";
Assert.Equal(humanized, collection.Humanize());
}

[Fact]
public void TwoItems()
{
var collection = new List<int>(new int[] {1, 2});
string humanized = "1 und 2";
Assert.Equal(humanized, collection.Humanize());
}

[Fact]
public void MoreThanTwoItems()
{
var collection = new List<int>(new int[] {1, 2, 3});
string humanized = "1, 2 und 3";
Assert.Equal(humanized, collection.Humanize());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests.Localisation.it
{
Expand Down
12 changes: 9 additions & 3 deletions src/Humanizer/Configuration/CollectionFormatterRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ internal class CollectionFormatterRegistry : LocaliserRegistry<ICollectionFormat
public CollectionFormatterRegistry()
: base(new DefaultCollectionFormatter())
{
Register("en", new EnglishCollectionFormatter());
Register("it", new ItalianCollectionFormatter());
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"));
}
}
}
}
6 changes: 3 additions & 3 deletions src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
<Compile Include="CollectionHumanizeExtensions.cs" />
<Compile Include="Configuration\CollectionFormatterRegistry.cs" />
<Compile Include="Localisation\CollectionFormatters\DefaultCollectionFormatter.cs" />
<Compile Include="Localisation\CollectionFormatters\EnglishCollectionFormatter.cs" />
<Compile Include="Localisation\CollectionFormatters\ItalianCollectionFormatter.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" />
<Compile Include="Localisation\Formatters\SlovenianFormatter.cs" />
<Compile Include="Configuration\LocaliserRegistry.cs" />
Expand Down Expand Up @@ -233,4 +233,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Humanizer.Localisation.CollectionFormatters
{
internal class EnglishCollectionFormatter : DefaultCollectionFormatter
internal class OxfordStyleCollectionFormatter : DefaultCollectionFormatter
{
public EnglishCollectionFormatter()
public OxfordStyleCollectionFormatter(string defaultSeparator)
{
DefaultSeparator = "and";
DefaultSeparator = defaultSeparator ?? "and";
}

public override string Humanize<T>(IEnumerable<T> collection, Func<T, String> objectFormatter, String separator)
public override string Humanize<T>(IEnumerable<T> collection, Func<T, string> objectFormatter, String separator)
{
if (collection == null)
throw new ArgumentException("collection");
Expand All @@ -29,9 +29,9 @@ public override string Humanize<T>(IEnumerable<T> collection, Func<T, String> ob
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()));
String.Join(", ", enumerable.Take(count - 1).Select(objectFormatter)),
separator,
objectFormatter(enumerable.Skip(count - 1).First()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

namespace Humanizer.Localisation.CollectionFormatters
{
internal class ItalianCollectionFormatter : EnglishCollectionFormatter
internal class RegularStyleCollectionFormatter : DefaultCollectionFormatter
{
public ItalianCollectionFormatter()
: base()
public RegularStyleCollectionFormatter(string defaultSeparator)
{
DefaultSeparator = "e";
DefaultSeparator = defaultSeparator ?? "and";
}

public override string Humanize<T>(IEnumerable<T> collection, Func<T, String> objectFormatter, String separator)
Expand Down