Skip to content
Merged
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
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");
}
}
}
Loading