Skip to content

Commit

Permalink
Remove FormatWith extension (#1395)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Feb 17, 2024
1 parent dafb148 commit cb832cf
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 194 deletions.
18 changes: 0 additions & 18 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,24 +199,6 @@ Default is the right as shown in the examples above. The examples below show how
"Long text to truncate".Truncate(2, "---", Truncator.FixedNumberOfWords, TruncateFrom.Left) => "---to truncate"
```

### <a id="format-string">Format String</a>
You can format a `string` using the `FormatWith()` method:

```c#
"To be formatted -> {0}/{1}.".FormatWith(1, "A") => "To be formatted -> 1/A."
```

This is an extension method based on `String.Format`, so exact rules applies to it.
If `format` is null, it'll throw `ArgumentNullException`.
If passed a fewer number for arguments, it'll throw `String.FormatException` exception.

You also can specify the culture to use explicitly as the first parameter for the `FormatWith()` method:

```c#
"{0:N2}".FormatWith(new CultureInfo("ru-RU"), 6666.66) => "6 666,66"
```

If a culture is not specified, current thread's current culture is used.

### <a id="humanize-enums">Humanize Enums</a>
Calling `ToString` directly on enum members usually results in less than ideal output for users. The solution to this is usually to use `DescriptionAttribute` data annotation and then read that at runtime to get a more friendly output. That is a great solution; but more often than not we only need to put some space between words of an enum member - which is what `String.Humanize()` does well. For an enum like:
Expand Down
33 changes: 0 additions & 33 deletions src/Humanizer.Tests.Shared/StringExtensionsTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -1850,11 +1850,6 @@ namespace Humanizer
{
public static string Dehumanize(this string input) { }
}
public static class StringExtensions
{
public static string FormatWith(this string format, params object[] args) { }
public static string FormatWith(this string format, System.IFormatProvider provider, params object[] args) { }
}
public static class StringHumanizeExtensions
{
public static string Humanize(this string input) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1194,11 +1194,6 @@ namespace Humanizer
{
public static string Dehumanize(this string input) { }
}
public static class StringExtensions
{
public static string FormatWith(this string format, params object[] args) { }
public static string FormatWith(this string format, System.IFormatProvider provider, params object[] args) { }
}
public static class StringHumanizeExtensions
{
public static string Humanize(this string input) { }
Expand Down
9 changes: 6 additions & 3 deletions src/Humanizer/Localisation/Formatters/DefaultFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,12 @@ protected virtual string Format(string resourceKey, int number, bool toWords = f
throw new ArgumentException($"The resource object with key '{resourceKey}' was not found", nameof(resourceKey));
}

return toWords
? resourceString.FormatWith(number.ToWords(_culture))
: resourceString.FormatWith(number);
if (toWords)
{
return string.Format(resourceString, number.ToWords(_culture));
}

return string.Format(resourceString, number);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Humanizer/Localisation/Formatters/RomanianFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected override string Format(string resourceKey, int number, bool toWords =
? UnitPreposition
: string.Empty;

return format.FormatWith(number, preposition);
return string.Format(format, number, preposition);
}

static bool ShouldUsePreposition(int number)
Expand Down
18 changes: 7 additions & 11 deletions src/Humanizer/Localisation/ResourceKeys.Common.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
namespace Humanizer
namespace Humanizer;

public partial class ResourceKeys
{
public partial class ResourceKeys
static void ValidateRange(int count)
{
const string Single = "Single";
const string Multiple = "Multiple";

static void ValidateRange(int count)
if (count < 0)
{
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
throw new ArgumentOutOfRangeException(nameof(count));
}
}
}
}
88 changes: 40 additions & 48 deletions src/Humanizer/Localisation/ResourceKeys.DateHumanize.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,54 @@
namespace Humanizer
namespace Humanizer;

public partial class ResourceKeys
{
public partial class ResourceKeys
/// <summary>
/// Encapsulates the logic required to get the resource keys for DateTime.Humanize
/// </summary>
public static class DateHumanize
{
/// <summary>
/// Encapsulates the logic required to get the resource keys for DateTime.Humanize
/// Resource key for Now.
/// </summary>
public static class DateHumanize
public const string Now = "DateHumanize_Now";

/// <summary>
/// Resource key for Never.
/// </summary>
public const string Never = "DateHumanize_Never";

/// <summary>
/// Generates Resource Keys according to convention.
/// </summary>
/// <param name="timeUnit">Time unit</param>
/// <param name="timeUnitTense">Is time unit in future or past</param>
/// <param name="count">Number of units, default is One.</param>
/// <returns>Resource key, like DateHumanize_SingleMinuteAgo</returns>
public static string GetResourceKey(TimeUnit timeUnit, Tense timeUnitTense, int count = 1)
{
/// <summary>
/// Resource key for Now.
/// </summary>
public const string Now = "DateHumanize_Now";

/// <summary>
/// Resource key for Never.
/// </summary>
public const string Never = "DateHumanize_Never";

/// <summary>
/// Examples: DateHumanize_SingleMinuteAgo, DateHumanize_MultipleHoursAgo
/// Note: "s" for plural served separately by third part.
/// </summary>
const string DateTimeFormat = "DateHumanize_{0}{1}{2}";

const string Ago = "Ago";
const string FromNow = "FromNow";

/// <summary>
/// Generates Resource Keys according to convention.
/// </summary>
/// <param name="timeUnit">Time unit</param>
/// <param name="timeUnitTense">Is time unit in future or past</param>
/// <param name="count">Number of units, default is One.</param>
/// <returns>Resource key, like DateHumanize_SingleMinuteAgo</returns>
public static string GetResourceKey(TimeUnit timeUnit, Tense timeUnitTense, int count = 1)
ValidateRange(count);

if (count == 0)
{
ValidateRange(count);
return Now;
}

if (count == 0)
if (count == 1)
{
if (timeUnitTense == Tense.Future)
{
return Now;
return $"DateHumanize_Single{timeUnit}FromNow";
}

string singularity;
var unit = timeUnit.ToString();
if (count == 1)
{
singularity = Single;
}
else
{
unit += "s";
singularity = Multiple;
}
return $"DateHumanize_Single{timeUnit}Ago";
}

var tense = timeUnitTense == Tense.Future ? FromNow : Ago;
return DateTimeFormat.FormatWith(singularity, unit, tense);
if (timeUnitTense == Tense.Future)
{
return $"DateHumanize_Multiple{timeUnit}sFromNow";
}

return $"DateHumanize_Multiple{timeUnit}sAgo";
}
}
}
}
50 changes: 24 additions & 26 deletions src/Humanizer/Localisation/ResourceKeys.TimeSpanHumanize.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
namespace Humanizer
namespace Humanizer;

public partial class ResourceKeys
{
public partial class ResourceKeys
/// <summary>
/// Encapsulates the logic required to get the resource keys for TimeSpan.Humanize
/// Examples: TimeSpanHumanize_SingleMinute, TimeSpanHumanize_MultipleHours.
/// </summary>
public static class TimeSpanHumanize
{
/// <summary>
/// Encapsulates the logic required to get the resource keys for TimeSpan.Humanize
/// Generates Resource Keys according to convention.
/// </summary>
public static class TimeSpanHumanize
/// <param name="unit">Time unit, <see cref="TimeUnit"/>.</param>
/// <param name="count">Number of units, default is One.</param>
/// <param name="toWords">Result to words, default is false.</param>
/// <returns>Resource key, like TimeSpanHumanize_SingleMinute</returns>
public static string GetResourceKey(TimeUnit unit, int count = 1, bool toWords = false)
{
/// <summary>
/// Examples: TimeSpanHumanize_SingleMinute, TimeSpanHumanize_MultipleHours.
/// Note: "s" for plural served separately by third part.
/// </summary>
const string TimeSpanFormat = "TimeSpanHumanize_{0}{1}{2}";
const string Zero = "TimeSpanHumanize_Zero";
ValidateRange(count);

/// <summary>
/// Generates Resource Keys according to convention.
/// </summary>
/// <param name="unit">Time unit, <see cref="TimeUnit"/>.</param>
/// <param name="count">Number of units, default is One.</param>
/// <param name="toWords">Result to words, default is false.</param>
/// <returns>Resource key, like TimeSpanHumanize_SingleMinute</returns>
public static string GetResourceKey(TimeUnit unit, int count = 1, bool toWords = false)
if (count == 0 && toWords)
{
ValidateRange(count);

if (count == 0 && toWords)
{
return Zero;
}
return "TimeSpanHumanize_Zero";
}

return TimeSpanFormat.FormatWith(count == 1 ? Single : Multiple, unit, count == 1 ? "" : "s");
if (count == 1)
{
return $"TimeSpanHumanize_Single{unit}";
}

return $"TimeSpanHumanize_Multiple{unit}s";
}
}
}
}
33 changes: 14 additions & 19 deletions src/Humanizer/Localisation/ResourceKeys.TimeUnitSymbol.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
namespace Humanizer
namespace Humanizer;

public partial class ResourceKeys
{
public partial class ResourceKeys
/// <summary>
/// Encapsulates the logic required to get the resource keys for TimeUnit.ToSymbol
/// </summary>
public static class TimeUnitSymbol
{
/// <summary>
/// Encapsulates the logic required to get the resource keys for TimeUnit.ToSymbol
/// Generates Resource Keys according to convention.
/// Examples: TimeUnit_Minute, TimeUnit_Hour.
/// </summary>
public static class TimeUnitSymbol
{
/// <summary>
/// Examples: TimeUnit_Minute, TimeUnit_Hour.
/// </summary>
const string TimeUnitFormat = "TimeUnit_{0}";

/// <summary>
/// Generates Resource Keys according to convention.
/// </summary>
/// <param name="unit">Time unit, <see cref="TimeUnit"/>.</param>
/// <returns>Resource key, like TimeSpanHumanize_SingleMinute</returns>
public static string GetResourceKey(TimeUnit unit) =>
TimeUnitFormat.FormatWith(unit);
}
/// <param name="unit">Time unit, <see cref="TimeUnit"/>.</param>
/// <returns>Resource key, like TimeSpanHumanize_SingleMinute</returns>
public static string GetResourceKey(TimeUnit unit) =>
$"TimeUnit_{unit}";
}
}
}
25 changes: 0 additions & 25 deletions src/Humanizer/StringExtensions.cs

This file was deleted.

0 comments on commit cb832cf

Please sign in to comment.