Skip to content

Commit 9c8fbfc

Browse files
committed
Varie ed eventuali
1 parent a478b2d commit 9c8fbfc

File tree

15 files changed

+357
-4
lines changed

15 files changed

+357
-4
lines changed

SharpApi.sln

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7F2C4A9A-9D4
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{39C13328-C644-47C7-B8AB-0729098E365E}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpApi.Helpers.test", "src\Helpers\test\SharpApi.Helpers.test\SharpApi.Helpers.test.csproj", "{7ABE27BC-90F5-44B8-B9EA-6C374A51EB07}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpApi.Helpers.test", "src\Helpers\test\SharpApi.Helpers.test\SharpApi.Helpers.test.csproj", "{7ABE27BC-90F5-44B8-B9EA-6C374A51EB07}"
13+
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpApi.Utility", "src\Utility\src\SharpApi.Utility\SharpApi.Utility.csproj", "{1D9DF565-82E4-4A34-9FCF-85C7D7C08687}"
15+
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpApi.Utility.Test", "src\Utility\test\SharpApi.Utility.Test\SharpApi.Utility.Test.csproj", "{CED57B51-F5F2-4046-A688-C27EF19D1134}"
1317
EndProject
1418
Global
1519
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -25,13 +29,23 @@ Global
2529
{7ABE27BC-90F5-44B8-B9EA-6C374A51EB07}.Debug|Any CPU.Build.0 = Debug|Any CPU
2630
{7ABE27BC-90F5-44B8-B9EA-6C374A51EB07}.Release|Any CPU.ActiveCfg = Release|Any CPU
2731
{7ABE27BC-90F5-44B8-B9EA-6C374A51EB07}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{1D9DF565-82E4-4A34-9FCF-85C7D7C08687}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{1D9DF565-82E4-4A34-9FCF-85C7D7C08687}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{1D9DF565-82E4-4A34-9FCF-85C7D7C08687}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{1D9DF565-82E4-4A34-9FCF-85C7D7C08687}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{CED57B51-F5F2-4046-A688-C27EF19D1134}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{CED57B51-F5F2-4046-A688-C27EF19D1134}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{CED57B51-F5F2-4046-A688-C27EF19D1134}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{CED57B51-F5F2-4046-A688-C27EF19D1134}.Release|Any CPU.Build.0 = Release|Any CPU
2840
EndGlobalSection
2941
GlobalSection(SolutionProperties) = preSolution
3042
HideSolutionNode = FALSE
3143
EndGlobalSection
3244
GlobalSection(NestedProjects) = preSolution
3345
{5888BBC9-3D12-4433-B0EA-6A82C3D06661} = {7F2C4A9A-9D46-4828-A2D5-1635456F0072}
3446
{7ABE27BC-90F5-44B8-B9EA-6C374A51EB07} = {39C13328-C644-47C7-B8AB-0729098E365E}
47+
{1D9DF565-82E4-4A34-9FCF-85C7D7C08687} = {7F2C4A9A-9D46-4828-A2D5-1635456F0072}
48+
{CED57B51-F5F2-4046-A688-C27EF19D1134} = {39C13328-C644-47C7-B8AB-0729098E365E}
3549
EndGlobalSection
3650
GlobalSection(ExtensibilityGlobals) = postSolution
3751
SolutionGuid = {07338C3C-7F2A-4002-9F95-958D7A8EB435}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace SharpApi.Helpers.ObjectExtensions
6+
{
7+
public static class CollectionsHelper
8+
{
9+
}
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Security.Cryptography.X509Certificates;
5+
6+
namespace SharpApi.Helpers.ObjectExtensions
7+
{
8+
public static class GenericsHelper
9+
{
10+
public static bool IsDefault<T>(this T x)
11+
{
12+
return EqualityComparer<T>.Default.Equals(x, default);
13+
}
14+
15+
public static void ForEach<T>(this IEnumerable<T>? source, Action<T>? action)
16+
{
17+
if((source is null) || (action is null)) return;
18+
19+
foreach (var item in source)
20+
action(item);
21+
}
22+
23+
public static bool Implements<T>(this object x)
24+
{
25+
return x.GetType().GetInterfaces().Contains(typeof(T));
26+
}
27+
}
28+
}

src/Helpers/src/SharpApi.Helpers/ValueTypeExtensions/String/StringHelper.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,41 @@ public static bool IsValidUrl(this string str)
3636
}
3737

3838

39+
/// <summary>
40+
/// Returns a string from the left side with a fixed length of characters
41+
/// </summary>
42+
/// <param name="str"></param>
43+
/// <param name="length">The number of characters </param>
44+
public static string Left(this string str, int length)
45+
{
46+
if (length == 0 || str.Length == 0) return string.Empty;
47+
var result = str;
48+
if (length < str.Length)
49+
{
50+
result = str.Substring(0, length);
51+
}
52+
return result;
53+
}
54+
55+
/// <summary>
56+
/// Returns a string from the right side with a fixed length of characters
57+
/// </summary>
58+
/// <param name="str"></param>
59+
///<param name="length">The number of characters </param>
60+
public static string Right(this string str, int length)
61+
{
62+
if (length == 0 || str.Length == 0) return string.Empty;
63+
var result = str;
64+
if (length < str.Length)
65+
{
66+
result = str[^length..];
67+
}
68+
return result;
69+
}
70+
71+
public static bool IsNullOrEmpty(this string? s)
72+
{
73+
return string.IsNullOrEmpty(s);
74+
}
3975
}
4076
}

src/Helpers/src/SharpApi.Helpers/ValueTypeExtensions/String/StringTypeConversion.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,23 @@ public static long ToLong(this string value, CultureInfo? culture = null)
3939
/// Returns a Guid from an input string.
4040
/// </summary>
4141
/// <returns></returns>
42-
public static Guid ToGuid(this string value)
42+
public static Guid ToGuid(this string? value)
4343
{
44+
if (value.IsNullOrEmpty()) return Guid.Empty;
45+
4446
var isValidLong = Guid.TryParse(value, out var result);
4547

4648
return isValidLong ? result : default;
4749
}
50+
51+
/// <summary>
52+
///
53+
/// </summary>
54+
/// <returns></returns>
55+
public static T ToEnum<T>(this string value)
56+
where T : struct
57+
{
58+
return (T)Enum.Parse(typeof(T), value, true);
59+
}
4860
}
4961
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using SharpApi.Helpers.ObjectExtensions;
3+
using SharpApi.Helpers.test.MockUp;
4+
using Xunit;
5+
6+
namespace SharpApi.Helpers.test.ObjectExtensions
7+
{
8+
public class ObjectExtensionHelperTest
9+
{
10+
[Fact]
11+
public void IsDefaultTest()
12+
{
13+
Assert.True((default(string)).IsDefault());
14+
15+
Assert.True((default(DateTime)).IsDefault());
16+
17+
Assert.True((default(int)).IsDefault());
18+
19+
Assert.True((default(long)).IsDefault());
20+
21+
Assert.True((default(object)).IsDefault());
22+
23+
Assert.True((Guid.Empty).IsDefault());
24+
25+
Assert.False((new Foo()).IsDefault());
26+
27+
Assert.False((Guid.NewGuid()).IsDefault());
28+
}
29+
30+
[Fact]
31+
public void ImplementsTest()
32+
{
33+
var myObject = new Foo();
34+
35+
Assert.True(myObject.Implements<IEquatable<Foo>>());
36+
37+
Assert.False(myObject.Implements<IDisposable>());
38+
}
39+
}
40+
}

src/Helpers/test/SharpApi.Helpers.test/ObjectExtensions/DeserializationHelperTest.cs renamed to src/Helpers/test/SharpApi.Helpers.test/ObjectExtensions/Serialization/DeserializationHelperTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using SharpApi.Helpers.test.MockUp;
44
using Xunit;
55

6-
namespace SharpApi.Helpers.test.ObjectExtensions
6+
namespace SharpApi.Helpers.test.ObjectExtensions.Serialization
77
{
88
public class DeserializationHelperTest
99
{

src/Helpers/test/SharpApi.Helpers.test/ObjectExtensions/SerializationHelperTest.cs renamed to src/Helpers/test/SharpApi.Helpers.test/ObjectExtensions/Serialization/SerializationHelperTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using SharpApi.Helpers.test.MockUp;
44
using Xunit;
55

6-
namespace SharpApi.Helpers.test.ObjectExtensions
6+
namespace SharpApi.Helpers.test.ObjectExtensions.Serialization
77
{
88
public class SerializationHelperTest
99
{
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using SharpApi.Helpers.ValueTypeExtensions;
2+
using Xunit;
3+
4+
namespace SharpApi.Helpers.test.ValueTypeExtensions
5+
{
6+
public class StringExtensionsTest
7+
{
8+
[Fact]
9+
public void IsNullOrEmptyTest()
10+
{
11+
Assert.False("test string".IsNullOrEmpty());
12+
13+
Assert.True(default(string).IsNullOrEmpty());
14+
15+
Assert.True(string.Empty.IsNullOrEmpty());
16+
}
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace SharpApi.Utility
6+
{
7+
public static class CollectionsHelper
8+
{
9+
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> list)
10+
{
11+
if (list == null) return new List<T>();
12+
13+
var shuffledList = list
14+
.Select(x => new {Number = RandomGenerator.GetInt(), Item = x})
15+
.OrderBy(x => x.Number).Select(x => x.Item);
16+
return shuffledList.ToList();
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)