I have an API that accepts a string that needs to be properly formatted before going into the server.
The format for going into the server is the following
"{Country ABR} {Day/Hour} {State ABR} {Title} {hrs.} ({Month Year}.)"
Several Possibilities the client may send in :
"US Construction 7/70 hrs."
"IA Private hrs US.
"OIL US 8/70 hrs (Dec 2014).
Several valid examples after converting user input are:
"US 7/70 MI Construction hrs."
"US IA Private hrs."
"US OIL 8/70 hrs. (Dec 2014)"
the converter arranges the input into the correct order. hrs always ends with a period and rearranges ({Month Year}) outside the sentence as shown.
so far I have
[TestMethod]
public void TestMethod1()
{
var toConvert = "USA Construction 70/700 (Dec 2014) hrs";
var converted = ConvertHOSRules(toConvert);
Assert.AreEqual(converted, "USA 70/700 Construction hrs.(Dec 2014)");
}
private string ConvertHOSRules(string input)
{
//todo refactor
string output = "";
string country = Regex.Match(input, @"\b(USA|CAN|MEX)\b").Value +" ";
string dateHours = Regex.Match(input,@"\d{1,2}\/\d{1,3}").Value + " ";
string hrs = Regex.Match(input, @"\b(hrs)\b").Value ;
var date = Regex.Match(input, @"\(([a-zA-Z]+\s{1}[0-9]{4})\)").Value + " ";
string title = input.Replace(country, "").Replace(date, "").Replace(dateHours, "").Replace(hrs, "");
output = $"{country} {dateHours} {title} {hrs}.{date}";
return output;
}
This is passing i need to refactor.. the + " " is like a null guard by lazy programmer