Skip to content

Commit 30d02bc

Browse files
authored
Merge pull request #213 from shandybob76/add-date-formatting
Add date formatting (optional)
2 parents 6ba7998 + c4f4ec4 commit 30d02bc

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

src/CompaniesHouse/Description/DescriptionProvider.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
using System.Text.RegularExpressions;
1+
using System.Globalization;
2+
using System.Text.RegularExpressions;
23
using Newtonsoft.Json.Linq;
34

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

10-
public static string GetDescription(string format, JObject values)
13+
public static string GetDescription(string format, JObject values, string dateFormat = null)
1114
{
1215
if (values != null)
1316
{
@@ -19,7 +22,15 @@ public static string GetDescription(string format, JObject values)
1922

2023
if (variableValue != null)
2124
{
22-
format = format.Replace(placeHolder, variableValue.Value<string>());
25+
var value = variableValue.Value<string>();
26+
if (!string.IsNullOrEmpty(dateFormat) &&
27+
_datePattern.IsMatch(value))
28+
{
29+
var date = DateTime.ParseExact(value, _sourceDateFormat, CultureInfo.InvariantCulture);
30+
value = date.ToString(dateFormat);
31+
}
32+
33+
format = format.Replace(placeHolder, value);
2334
}
2435
}
2536
}

tests/CompaniesHouse.Tests/DescriptionTests/DescriptionProviderTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,48 @@ public void GivenFormatAndNoVariable()
5555

5656
result.Should().Be(@"some value: {nullvariable}");
5757
}
58+
59+
[Test]
60+
public void GivenFormatAndMatchingDateVariableNoDateFormat()
61+
{
62+
var format = "some value: {variable}";
63+
var values = JObject.Parse(@"{ ""variable"": ""2024-11-28"" }");
64+
var result = DescriptionProvider.GetDescription(format, values);
65+
66+
result.Should().Be(@"some value: 2024-11-28");
67+
}
68+
69+
[Test]
70+
public void GivenFormatAndMatchingDateVariableWithDateFormat()
71+
{
72+
var format = "some value: {variable}";
73+
var values = JObject.Parse(@"{ ""variable"": ""2024-11-28"" }");
74+
var dateFormat = "dd-MMM-yyyy";
75+
var result = DescriptionProvider.GetDescription(format, values, dateFormat);
76+
77+
result.Should().Be(@"some value: 28-Nov-2024");
78+
}
79+
80+
[Test]
81+
public void GivenFormatAndInvalidDateVariableWithDateFormat()
82+
{
83+
var format = "some value: {variable}";
84+
var values = JObject.Parse(@"{ ""variable"": ""2024-20-28"" }");
85+
var dateFormat = "dd-MMM-yyyy";
86+
var result = DescriptionProvider.GetDescription(format, values, dateFormat);
87+
88+
result.Should().Be(@"some value: 2024-20-28");
89+
}
90+
91+
[Test]
92+
public void GivenFormatAndIsNotDateVariableWithDateFormat()
93+
{
94+
var format = "some value: {variable}";
95+
var values = JObject.Parse(@"{ ""variable"": ""Value"" }");
96+
var dateFormat = "dd-MMM-yyyy";
97+
var result = DescriptionProvider.GetDescription(format, values, dateFormat);
98+
99+
result.Should().Be(@"some value: Value");
100+
}
58101
}
59102
}

0 commit comments

Comments
 (0)