Skip to content

Commit ab04ab6

Browse files
committed
minor refactoring
1 parent f2b0042 commit ab04ab6

3 files changed

Lines changed: 44 additions & 42 deletions

File tree

ObjectSemantics.NET/Algorithim/GavinsAlgorithim.cs

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ public static class GavinsAlgorithim
2626
//Condition Passed
2727
TemplatedContent templatedIfContent = GenerateTemplateFromFileContents(ifCondition.IfOperationTrueTemplate, options);
2828
string templatedIfContentMapped = GenerateFromTemplate(record, templatedIfContent, parameterKeyValues, options);
29-
clonedTemplate.Template = ReplaceFirstOccurrence(clonedTemplate.Template, ifCondition.ReplaceRef, templatedIfContentMapped);
29+
clonedTemplate.Template = clonedTemplate.Template.ReplaceFirstOccurrence(ifCondition.ReplaceRef, templatedIfContentMapped);
3030
}
3131
else if (!string.IsNullOrEmpty(ifCondition.IfOperationFalseTemplate))
3232
{
3333
//If Else Condition Block
3434
TemplatedContent templatedIfContent = GenerateTemplateFromFileContents(ifCondition.IfOperationFalseTemplate, options);
3535
string templatedIfElseContentMapped = GenerateFromTemplate(record, templatedIfContent, parameterKeyValues, options);
36-
clonedTemplate.Template = ReplaceFirstOccurrence(clonedTemplate.Template, ifCondition.ReplaceRef, templatedIfElseContentMapped);
36+
clonedTemplate.Template = clonedTemplate.Template.ReplaceFirstOccurrence(ifCondition.ReplaceRef, templatedIfElseContentMapped);
3737
}
3838
else
39-
clonedTemplate.Template = ReplaceFirstOccurrence(clonedTemplate.Template, ifCondition.ReplaceRef, string.Empty);
39+
clonedTemplate.Template = clonedTemplate.Template.ReplaceFirstOccurrence(ifCondition.ReplaceRef, string.Empty);
4040
}
4141
else
42-
clonedTemplate.Template = ReplaceFirstOccurrence(clonedTemplate.Template, ifCondition.ReplaceRef, $"[IF-CONDITION EXCEPTION]: unrecognized property: [{ifCondition.IfPropertyName}]");
42+
clonedTemplate.Template = clonedTemplate.Template.ReplaceFirstOccurrence(ifCondition.ReplaceRef, $"[IF-CONDITION EXCEPTION]: unrecognized property: [{ifCondition.IfPropertyName}]");
4343
}
4444
#endregion
4545

@@ -63,20 +63,19 @@ public static class GavinsAlgorithim
6363
{
6464
ExtractedObjProperty objProperty = rowRecordValues.FirstOrDefault(x => x.Name.ToUpper().Equals(objLoopCode.TargetPropertyName.ToUpper()));
6565
if (objProperty != null)
66-
activeRow = ReplaceFirstOccurrence(activeRow, objLoopCode.ReplaceRef, GetValueFromPropertyFormatted(objProperty, objLoopCode.FormattingCommand));
66+
activeRow = activeRow.ReplaceFirstOccurrence(objLoopCode.ReplaceRef, objProperty.GetValueFromPropertyFormatted(objLoopCode.FormattingCommand));
6767
else
68-
activeRow = ReplaceFirstOccurrence(activeRow, objLoopCode.ReplaceRef, objLoopCode.ReplaceCommand);
68+
activeRow = activeRow.ReplaceFirstOccurrence(objLoopCode.ReplaceRef, objLoopCode.ReplaceCommand);
6969
}
7070
//Append Record row
7171
rowContentTemplater.Append(activeRow);
7272
}
73-
7473
objLoop.ObjLoopTemplate = rowContentTemplater.ToString().RemoveLastInstanceOfString('\r', '\n'); //Assign Auto Generated
7574
//Replace the main Loop area
76-
clonedTemplate.Template = ReplaceFirstOccurrence(clonedTemplate.Template, objLoop.ReplaceRef, objLoop.ObjLoopTemplate);
75+
clonedTemplate.Template = clonedTemplate.Template.ReplaceFirstOccurrence(objLoop.ReplaceRef, objLoop.ObjLoopTemplate);
7776
}
7877
else
79-
clonedTemplate.Template = ReplaceFirstOccurrence(clonedTemplate.Template, objLoop.ReplaceRef, string.Empty);
78+
clonedTemplate.Template = clonedTemplate.Template.ReplaceFirstOccurrence(objLoop.ReplaceRef, string.Empty);
8079

8180
}
8281
#endregion
@@ -86,9 +85,9 @@ public static class GavinsAlgorithim
8685
{
8786
ExtractedObjProperty property = objProperties.FirstOrDefault(x => x.Name.ToUpper().Equals(replaceCode.TargetPropertyName.ToUpper()));
8887
if (property != null)
89-
clonedTemplate.Template = ReplaceFirstOccurrence(clonedTemplate.Template, replaceCode.ReplaceRef, GetValueFromPropertyFormatted(property, replaceCode.FormattingCommand));
88+
clonedTemplate.Template = clonedTemplate.Template.ReplaceFirstOccurrence(replaceCode.ReplaceRef, property.GetValueFromPropertyFormatted(replaceCode.FormattingCommand));
9089
else
91-
clonedTemplate.Template = ReplaceFirstOccurrence(clonedTemplate.Template, replaceCode.ReplaceRef, @"{{ ##command## }}".Replace("##command##", replaceCode.ReplaceCommand));
90+
clonedTemplate.Template = clonedTemplate.Template.ReplaceFirstOccurrence(replaceCode.ReplaceRef, @"{{ ##command## }}".Replace("##command##", replaceCode.ReplaceCommand));
9291
}
9392
#endregion
9493
return clonedTemplate.Template;
@@ -184,13 +183,7 @@ internal static TemplatedContent GenerateTemplateFromFileContents(string fileCon
184183
return templatedContent;
185184
}
186185

187-
private static string ReplaceFirstOccurrence(this string text, string search, string replace)
188-
{
189-
int pos = text.IndexOf(search);
190-
if (pos < 0)
191-
return text;
192-
return string.Format("{0}{1}{2}", text.Substring(0, pos), replace, text.Substring(pos + search.Length));
193-
}
186+
194187
private static List<ExtractedObjProperty> GetObjectProperties<T>(T value, List<ObjectSemanticsKeyValue> parameters = null) where T : new()
195188
{
196189
List<ExtractedObjProperty> extractedObjProperties = new List<ExtractedObjProperty>();
@@ -236,28 +229,5 @@ private static List<ExtractedObjProperty> GetObjPropertiesFromUnknown(object val
236229
return list;
237230
}
238231

239-
private static string GetValueFromPropertyFormatted(ExtractedObjProperty p, string customFormattingValue)
240-
{
241-
if (string.IsNullOrWhiteSpace(customFormattingValue))
242-
return p.StringFormatted;
243-
if (p.Type.Equals(typeof(int)))
244-
return int.Parse(p.StringFormatted).ToString(customFormattingValue);
245-
else if (p.Type.Equals(typeof(double)))
246-
return double.Parse(p.StringFormatted).ToString(customFormattingValue);
247-
else if (p.Type.Equals(typeof(long)))
248-
return long.Parse(p.StringFormatted).ToString(customFormattingValue);
249-
else if (p.Type.Equals(typeof(float)))
250-
return float.Parse(p.StringFormatted).ToString(customFormattingValue);
251-
else if (p.Type.Equals(typeof(decimal)))
252-
return decimal.Parse(p.StringFormatted).ToString(customFormattingValue);
253-
else if (p.Type.Equals(typeof(DateTime)))
254-
return DateTime.Parse(p.StringFormatted).ToString(customFormattingValue);
255-
//Custom Formats
256-
else if (customFormattingValue.ToLower().Equals("uppercase"))
257-
return p.StringFormatted?.ToUpper();
258-
else if (customFormattingValue.ToLower().Equals("lowercase"))
259-
return p.StringFormatted?.ToLower();
260-
else
261-
return p.StringFormatted?.ToUpper();
262-
}
232+
263233
}

ObjectSemantics.NET/Extensions/ExtractedObjPropertyExtensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@ namespace ObjectSemantics.NET
55
{
66
public static class ExtractedObjPropertyExtensions
77
{
8+
public static string GetValueFromPropertyFormatted(this ExtractedObjProperty p, string customFormattingValue)
9+
{
10+
if (string.IsNullOrWhiteSpace(customFormattingValue))
11+
return p.StringFormatted;
12+
if (p.Type.Equals(typeof(int)))
13+
return int.Parse(p.StringFormatted).ToString(customFormattingValue);
14+
else if (p.Type.Equals(typeof(double)))
15+
return double.Parse(p.StringFormatted).ToString(customFormattingValue);
16+
else if (p.Type.Equals(typeof(long)))
17+
return long.Parse(p.StringFormatted).ToString(customFormattingValue);
18+
else if (p.Type.Equals(typeof(float)))
19+
return float.Parse(p.StringFormatted).ToString(customFormattingValue);
20+
else if (p.Type.Equals(typeof(decimal)))
21+
return decimal.Parse(p.StringFormatted).ToString(customFormattingValue);
22+
else if (p.Type.Equals(typeof(DateTime)))
23+
return DateTime.Parse(p.StringFormatted).ToString(customFormattingValue);
24+
//Custom Formats
25+
else if (customFormattingValue.ToLower().Equals("uppercase"))
26+
return p.StringFormatted?.ToUpper();
27+
else if (customFormattingValue.ToLower().Equals("lowercase"))
28+
return p.StringFormatted?.ToLower();
29+
else
30+
return p.StringFormatted?.ToUpper();
31+
}
32+
833
private static T GetConvertibleValue<T>(string value) where T : IConvertible
934
{
1035
return (string.IsNullOrEmpty(value) || value?.ToLower()?.Trim() == "null") ? default : (T)Convert.ChangeType(value, typeof(T));

ObjectSemantics.NET/Extensions/StringExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ namespace ObjectSemantics.NET
44
{
55
public static class StringExtensions
66
{
7+
public static string ReplaceFirstOccurrence(this string text, string search, string replace)
8+
{
9+
int pos = text.IndexOf(search);
10+
if (pos < 0)
11+
return text;
12+
return string.Format("{0}{1}{2}", text.Substring(0, pos), replace, text.Substring(pos + search.Length));
13+
}
714
public static string RemoveLastInstanceOfString(this string value, string removeString)
815
{
916
int index = value.LastIndexOf(removeString, StringComparison.Ordinal);

0 commit comments

Comments
 (0)