Skip to content

Commit d8ede19

Browse files
committed
Added Extension method for ToBoolean
1 parent 90dd731 commit d8ede19

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

Source/Shared/Extensions/StringExtensions.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,26 @@ public static bool IsPatternMatch(this string value, string pattern, bool ignore
6363

6464
return value.Equals(pattern);
6565
}
66-
66+
67+
public static bool ToBoolean(this string input, bool @default = false) {
68+
if (String.IsNullOrEmpty(input))
69+
return @default;
70+
71+
input = input.ToLowerInvariant().Trim();
72+
73+
bool value;
74+
if (bool.TryParse(input, out value))
75+
return value;
76+
77+
if (String.Equals(input, "yes") || String.Equals(input, "1"))
78+
return true;
79+
80+
if (String.Equals(input, "no") || String.Equals(input, "0"))
81+
return false;
82+
83+
return @default;
84+
}
85+
6786
public static string ToHex(this IEnumerable<byte> bytes) {
6887
var sb = new StringBuilder();
6988
foreach (byte b in bytes)

Source/Shared/Models/Collections/SettingsDictionary.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,8 @@ public bool GetBoolean(string name, bool @default) {
3838

3939
if (String.IsNullOrEmpty(temp))
4040
return @default;
41-
42-
temp = temp.ToLowerInvariant().Trim();
43-
44-
bool value;
45-
if (bool.TryParse(temp, out value))
46-
return value;
47-
48-
if (String.Equals(temp, "yes") || String.Equals(temp, "1"))
49-
return true;
5041

51-
if (String.Equals(temp, "no") || String.Equals(temp, "0"))
52-
return false;
53-
54-
return @default;
42+
return temp.ToBoolean(@default);
5543
}
5644

5745
public int GetInt32(string name) {
@@ -230,11 +218,7 @@ public bool GetTypeAndSourceEnabled(string type, string source) {
230218
return sourceEnabled;
231219
}
232220

233-
var setting = GetTypeAndSourceSetting(type, source, "true");
234-
if (!Boolean.TryParse(setting, out sourceEnabled))
235-
sourceEnabled = true;
236-
237-
return sourceEnabled;
221+
return GetTypeAndSourceSetting(type, source, "true").ToBoolean(true);
238222
}
239223

240224
private readonly Dictionary<string, string> _eventTypes = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

0 commit comments

Comments
 (0)