Hello
Change from Regex to IF statement
Change from Regex to IF statement
C#:
namespace Service
{
public class Service : Iervice
{
private readonly Func<string, bool> IsNew;
private readonly Func<string, string> Target;
public Service()
{
RequestTypeParser parser = new RequestTypeParser
((pattern) =>
{
Regex regex = new Regex(pattern);
return (input) => regex.IsMatch(input);
});
IsNew = (string Number) => parser.Parse(Number) == RequestType.New; //<====Here(Change from Regex to IF statement)
Target = (string requestNumber) => IsNew(Number) ? "SO" : "YS";//<====Here(Change from Regex to IF statement)
}
private enum RequestType
{
Invalid,
Old,
Current,
New,
RN
}
private class RequestTypeParser
{
public readonly Func<string, RequestType> Parse;
public RequestTypeParser(Func<string, Func<string, bool>> regexCreator)
{
var IsOld = regexCreator(@"R\d{8}");
var IsCurrent = regexCreator(@"C\d{8}");
var IsNew = regexCreator(@"R\d{8}N");
var IsRN = regexCreator(@"N\d{8}");
Parse = (Number) =>
{
RequestType type = RequestType.Invalid;
if (IsOld(requestNumber))
type = RequestType.Old;
else if (IsCurrent(requestNumber))
type = RequestType.Current;
else if (IsNew(requestNumber))
type = RequestType.New;
else if (IsRN(requestNumber))
type = RequestType.RN;
return type;
};
}
}
}
}
Last edited: