From c4f4ec47cdfd9b98376c881dba505062609b89db Mon Sep 17 00:00:00 2001 From: Rob Vining Date: Thu, 28 Nov 2024 08:08:07 +0000 Subject: [PATCH] Add date formatting (optional) --- .../Description/DescriptionProvider.cs | 17 ++++++-- .../DescriptionProviderTests.cs | 43 +++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/CompaniesHouse/Description/DescriptionProvider.cs b/src/CompaniesHouse/Description/DescriptionProvider.cs index 3186d0a..ebaf27c 100644 --- a/src/CompaniesHouse/Description/DescriptionProvider.cs +++ b/src/CompaniesHouse/Description/DescriptionProvider.cs @@ -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) { @@ -19,7 +22,15 @@ public static string GetDescription(string format, JObject values) if (variableValue != null) { - format = format.Replace(placeHolder, variableValue.Value()); + var value = variableValue.Value(); + if (!string.IsNullOrEmpty(dateFormat) && + _datePattern.IsMatch(value)) + { + var date = DateTime.ParseExact(value, _sourceDateFormat, CultureInfo.InvariantCulture); + value = date.ToString(dateFormat); + } + + format = format.Replace(placeHolder, value); } } } diff --git a/tests/CompaniesHouse.Tests/DescriptionTests/DescriptionProviderTests.cs b/tests/CompaniesHouse.Tests/DescriptionTests/DescriptionProviderTests.cs index c8dbe57..f2b777c 100644 --- a/tests/CompaniesHouse.Tests/DescriptionTests/DescriptionProviderTests.cs +++ b/tests/CompaniesHouse.Tests/DescriptionTests/DescriptionProviderTests.cs @@ -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"); + } } }