Skip to content

Commit

Permalink
Merge pull request #213 from shandybob76/add-date-formatting
Browse files Browse the repository at this point in the history
Add date formatting (optional)
  • Loading branch information
kevbite authored Nov 28, 2024
2 parents 6ba7998 + c4f4ec4 commit 30d02bc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/CompaniesHouse/Description/DescriptionProvider.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System.Text.RegularExpressions;
using System.Globalization;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;

namespace CompaniesHouse.Description
{
public class DescriptionProvider
{
private const string _sourceDateFormat = "yyyy-MM-dd";
private static readonly Regex _pattern = new Regex(@"({[a-zA-Z0-9.-_]*})");
private static readonly Regex _datePattern = new Regex(@"^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$");

public static string GetDescription(string format, JObject values)
public static string GetDescription(string format, JObject values, string dateFormat = null)
{
if (values != null)
{
Expand All @@ -19,7 +22,15 @@ public static string GetDescription(string format, JObject values)

if (variableValue != null)
{
format = format.Replace(placeHolder, variableValue.Value<string>());
var value = variableValue.Value<string>();
if (!string.IsNullOrEmpty(dateFormat) &&
_datePattern.IsMatch(value))
{
var date = DateTime.ParseExact(value, _sourceDateFormat, CultureInfo.InvariantCulture);
value = date.ToString(dateFormat);
}

format = format.Replace(placeHolder, value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,48 @@ public void GivenFormatAndNoVariable()

result.Should().Be(@"some value: {nullvariable}");
}

[Test]
public void GivenFormatAndMatchingDateVariableNoDateFormat()
{
var format = "some value: {variable}";
var values = JObject.Parse(@"{ ""variable"": ""2024-11-28"" }");
var result = DescriptionProvider.GetDescription(format, values);

result.Should().Be(@"some value: 2024-11-28");
}

[Test]
public void GivenFormatAndMatchingDateVariableWithDateFormat()
{
var format = "some value: {variable}";
var values = JObject.Parse(@"{ ""variable"": ""2024-11-28"" }");
var dateFormat = "dd-MMM-yyyy";
var result = DescriptionProvider.GetDescription(format, values, dateFormat);

result.Should().Be(@"some value: 28-Nov-2024");
}

[Test]
public void GivenFormatAndInvalidDateVariableWithDateFormat()
{
var format = "some value: {variable}";
var values = JObject.Parse(@"{ ""variable"": ""2024-20-28"" }");
var dateFormat = "dd-MMM-yyyy";
var result = DescriptionProvider.GetDescription(format, values, dateFormat);

result.Should().Be(@"some value: 2024-20-28");
}

[Test]
public void GivenFormatAndIsNotDateVariableWithDateFormat()
{
var format = "some value: {variable}";
var values = JObject.Parse(@"{ ""variable"": ""Value"" }");
var dateFormat = "dd-MMM-yyyy";
var result = DescriptionProvider.GetDescription(format, values, dateFormat);

result.Should().Be(@"some value: Value");
}
}
}

0 comments on commit 30d02bc

Please sign in to comment.