Skip to content

Commit a0972d2

Browse files
Added string helper
1 parent 9f167be commit a0972d2

File tree

4 files changed

+123
-60
lines changed

4 files changed

+123
-60
lines changed

src/Reflector/Is.cs

+2-36
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,8 @@ public static bool InRange<T>([DisallowNull] T comparable, T? from, T? to)
99
where T : IComparable<T> =>
1010
comparable.CompareTo(from) >= 0 &&
1111
comparable.CompareTo(to) <= 0;
12-
public static Type? GetType(object? obj) => obj?.GetType();
1312
public static Type GetType<T>() => typeof(T);
14-
public static bool Same(object? actual, object? expected)
15-
{
16-
if (actual == null && expected == null)
17-
return true;
18-
if (actual == null || expected == null)
19-
return false;
20-
21-
return ReferenceEquals(actual, expected);
22-
}
13+
2314
public static bool Same<T>(T? actual, T? expected) where T : class
2415
{
2516
if (actual == null && expected == null)
@@ -29,32 +20,7 @@ public static bool Same<T>(T? actual, T? expected) where T : class
2920

3021
return ReferenceEquals(actual, expected);
3122
}
32-
public static bool Equal(object? actual, object? expected)
33-
{
34-
if (actual == null && expected == null)
35-
return true;
36-
if (actual == null || expected == null)
37-
return false;
38-
39-
Type actualType = actual.GetType();
40-
Type expectedType = expected.GetType();
41-
42-
if (actualType != expectedType)
43-
return false;
44-
45-
if (actualType.IsValueType)
46-
return actual.Equals(expected);
47-
48-
if (IsType.Record(actualType))
49-
return actual.Equals(expected);
50-
51-
if (actual is IComparable comparable)
52-
{
53-
return comparable.CompareTo(expected) == 0;
54-
}
55-
56-
return ReferenceEquals(actual, expected);
57-
}
23+
5824
public static bool Equal<T>(T? actual, T? expected) where T : IEquatable<T>, IComparable<T>
5925
{
6026
if (actual == null && expected == null)

src/Reflector/IsObject.cs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Diagnostics;
2+
3+
namespace VReflector;
4+
5+
public static class IsObject
6+
{
7+
public static Type? GetType(this object? obj) => obj?.GetType();
8+
public static bool Same(this object? actual, object? expected)
9+
{
10+
if (actual == null || expected == null)
11+
return false;
12+
13+
return ReferenceEquals(actual, expected);
14+
}
15+
public static bool Equal(this object? actual, object? expected)
16+
{
17+
if (actual == null || expected == null)
18+
return false;
19+
20+
Type actualType = actual.GetType();
21+
Type expectedType = expected.GetType();
22+
23+
if (actualType != expectedType)
24+
return false;
25+
26+
if (actualType.IsValueType)
27+
return actual.Equals(expected);
28+
29+
if (IsType.Record(actualType))
30+
return actual.Equals(expected);
31+
32+
if (actual is IComparable comparable)
33+
{
34+
return comparable.CompareTo(expected) == 0;
35+
}
36+
37+
return ReferenceEquals(actual, expected);
38+
}
39+
public static string GetStackTrace(this object obj)
40+
{
41+
if (obj is StackTrace stackTrace)
42+
{
43+
return stackTrace.ToString();
44+
}
45+
return string.Empty;
46+
}
47+
public static bool IsStackTraceType(this object obj)
48+
{
49+
return obj is StackTrace;
50+
}
51+
public static string GetCallerMethod(this object obj)
52+
{
53+
if (obj is StackTrace stackTrace)
54+
{
55+
var frame = stackTrace.GetFrame(1); // Caller frame
56+
return frame?.GetMethod()?.Name ?? string.Empty;
57+
}
58+
return string.Empty;
59+
}
60+
}

src/Reflector/IsStackTrace.cs

+1-24
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,17 @@ public static string GetStackTrace()
99
{
1010
return new StackTrace().ToString();
1111
}
12-
public static string GetStackTrace(object obj)
13-
{
14-
if (obj is StackTrace stackTrace)
15-
{
16-
return stackTrace.ToString();
17-
}
18-
return string.Empty;
19-
}
2012
public static StackFrame[] GetStackFrames()
2113
{
2214
return new StackTrace(true).GetFrames();
2315
}
24-
25-
public static bool IsStackTraceType(object obj)
26-
{
27-
return obj is StackTrace;
28-
}
29-
3016
public static string GetCallerMethod()
3117
{
3218
var stackTrace = new StackTrace();
3319
var frame = stackTrace.GetFrame(1); // Get the caller's stack frame
3420
return frame?.GetMethod()?.Name ?? string.Empty;
3521
}
36-
37-
public static string GetCallerMethod(object obj)
38-
{
39-
if (obj is StackTrace stackTrace)
40-
{
41-
var frame = stackTrace.GetFrame(1); // Caller frame
42-
return frame?.GetMethod()?.Name ?? string.Empty;
43-
}
44-
return string.Empty;
45-
}
22+
4623
public static string GetMethodFromStackTrace(int frameIndex)
4724
{
4825
var stackTrace = new StackTrace();

src/Reflector/IsString.cs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
namespace VReflector;
2+
3+
public static class IsString
4+
{
5+
public static bool HasMixedOrNoCase(this string? value)
6+
{
7+
8+
if (value == null) return false;
9+
var hasUpperCase = false;
10+
var hasLowerCase = false;
11+
12+
foreach (var ch in value)
13+
{
14+
hasUpperCase |= char.IsUpper(ch);
15+
hasLowerCase |= char.IsLower(ch);
16+
17+
if (hasUpperCase && hasLowerCase)
18+
{
19+
return true;
20+
}
21+
}
22+
23+
return !hasUpperCase && !hasLowerCase;
24+
}
25+
public static bool IsUpperCase(this string? value)
26+
{
27+
if (value == null) return false;
28+
foreach (var ch in value)
29+
{
30+
if (!char.IsUpper(ch))
31+
{
32+
return false;
33+
}
34+
}
35+
36+
return true;
37+
}
38+
public static bool IsLowerCase(this string? value)
39+
{
40+
if (value == null) return false;
41+
foreach (var ch in value)
42+
{
43+
if (!char.IsLower(ch))
44+
{
45+
return false;
46+
}
47+
}
48+
49+
return true;
50+
}
51+
public static bool HasValue(this string? value)
52+
{
53+
if (value is null || value.Trim().Length == 0)
54+
{
55+
return false;
56+
}
57+
return true;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)