Skip to content

Commit db67958

Browse files
committed
removed utils dependency
1 parent de0cab4 commit db67958

File tree

12 files changed

+200
-14
lines changed

12 files changed

+200
-14
lines changed

src/Extensions/DateTimeExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,27 @@ public static int ToAgeAtDate(this DateTime birthDay, DateTime atDate)
5353
/// <returns></returns>
5454
public static DateTime ToSimpleDateTime(this DateTime dt) =>
5555
new(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, DateTimeKind.Unspecified);
56+
57+
/// <summary>
58+
/// Add "working day" to a date, where working day means from Monday to Friday.
59+
/// </summary>
60+
/// <param name="d"></param>
61+
/// <param name="days">Number of days to add</param>
62+
/// <returns></returns>
63+
public static DateTime AddWorkDays(this DateTime d, int days)
64+
{
65+
for (var i = 0; i < days; ++i)
66+
{
67+
if (d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday)
68+
{
69+
d = d.AddDays(1.0);
70+
days++;
71+
continue;
72+
}
73+
74+
d = d.AddDays(1.0);
75+
}
76+
return d;
77+
}
5678
}
5779
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Utils.Enums
1+
namespace Extensions.Enums
22
{
33
public enum KeyEncoding
44
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Utils.Enums
1+
namespace Extensions.Enums
22
{
33
public enum SortingOptions
44
{

src/Extensions/Extensions.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.3" />
1111
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.3" />
1212
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
13+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
1314
<PackageReference Include="Polly" Version="7.2.3" />
1415
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
1516
</ItemGroup>
1617

17-
<ItemGroup>
18-
<ProjectReference Include="..\Utils\Utils.csproj" />
19-
</ItemGroup>
20-
2118
</Project>

src/Extensions/GenericExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ public static bool IsEqualTo(this object? a, object? b)
5858
}
5959
#nullable disable
6060

61+
public static bool Between<T>(this T item, T start, T end, bool includeStart = true, bool includeEnd = true)
62+
{
63+
return
64+
(
65+
(includeStart && Comparer<T>.Default.Compare(item, start) >= 0)
66+
||
67+
(!includeStart && Comparer<T>.Default.Compare(item, start) > 0)
68+
)
69+
&&
70+
(
71+
(includeEnd && Comparer<T>.Default.Compare(item, end) <= 0)
72+
||
73+
(!includeEnd && Comparer<T>.Default.Compare(item, end) < 0)
74+
);
75+
}
76+
6177
public static List<Variance> DetailedCompare<T>(this T val1, T val2)
6278
{
6379
var variances = new List<Variance>();

src/Extensions/IEnumerableExtensions.cs

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
45
using System.Linq;
5-
using Utils;
6+
using Extensions.Types;
67

78
namespace Extensions
89
{
910
public static class IEnumerableExtensions
1011
{
11-
public static void Each<T>(this IEnumerable<T> source, Action<T> action) => For.Each(source, action);
12+
//public static void Each<T>(this IEnumerable<T> source, Action<T> action) => For.Each(source, action);
1213

13-
public static void Each<T>(this IEnumerable source, Action<T> action) => For.Each(source, action);
14+
//public static void Each<T>(this IEnumerable source, Action<T> action) => For.Each(source, action);
1415

1516
public static IEnumerable<T> Except<T>(this IEnumerable<T> list, IEnumerable<T> except, Func<T, T, bool> comparer) =>
1617
list.Except(except, new KeyEqualityComparer<T>(comparer));
@@ -31,5 +32,81 @@ public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> enumerable, bool con
3132
public static IEnumerable<T> WhereIfElse<T>(this IEnumerable<T> enumerable, bool condition, Func<T, bool> @if, Func<T, bool> @else) =>
3233
condition ? enumerable.Where(@if) : enumerable.Where(@else);
3334

35+
/// <summary>
36+
/// Distinct by specific property.
37+
/// </summary>
38+
/// <param name="keySelector">Function to pass object for distinct to work.</param>
39+
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
40+
{
41+
var seenKeys = new HashSet<TKey>();
42+
foreach (var element in source)
43+
{
44+
if (seenKeys.Add(keySelector(element)))
45+
{
46+
yield return element;
47+
}
48+
}
49+
}
50+
51+
/// <summary>
52+
/// Checks whether all items in the enumerable are same (Uses <see cref="object.Equals(object)" /> to check for equality)
53+
/// </summary>
54+
/// <typeparam name="T"></typeparam>
55+
/// <param name="enumerable">The enumerable.</param>
56+
/// <returns>
57+
/// Returns true if there is 0 or 1 item in the enumerable or if all items in the enumerable are same (equal to
58+
/// each other) otherwise false.
59+
/// </returns>
60+
public static bool AreAllSame<T>(this IEnumerable<T> enumerable)
61+
{
62+
if (enumerable == null)
63+
throw new ArgumentNullException(nameof(enumerable));
64+
65+
using (var enumerator = enumerable.GetEnumerator())
66+
{
67+
var toCompare = default(T);
68+
if (enumerator.MoveNext())
69+
{
70+
toCompare = enumerator.Current;
71+
}
72+
73+
while (enumerator.MoveNext())
74+
{
75+
if (toCompare != null && !toCompare.Equals(enumerator.Current))
76+
{
77+
return false;
78+
}
79+
}
80+
}
81+
82+
return true;
83+
}
84+
85+
[DebuggerStepThrough]
86+
public static void Each<T>(this IEnumerable<T> source, Action<T> action)
87+
{
88+
foreach (var item in source)
89+
action(item);
90+
}
91+
92+
/// <summary>
93+
/// For shortcut, no side effects
94+
/// </summary>
95+
[DebuggerStepThrough]
96+
public static void Each<T>(this IEnumerable source, Action<T> action)
97+
{
98+
foreach (var item in source)
99+
action((T)item);
100+
}
101+
102+
/// <summary>
103+
/// For shortcut with index, no side effects
104+
/// </summary>
105+
[DebuggerStepThrough]
106+
public static void EachIndex<T>(this IEnumerable<T> source, Action<T, int> action)
107+
{
108+
var i = 0;
109+
Each(source, item => action(item, i++));
110+
}
34111
}
35112
}

src/Extensions/IQueryableExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
using System.Linq.Expressions;
55
using System.Reflection;
66
using System.Threading.Tasks;
7+
using Extensions.Enums;
78
using Microsoft.EntityFrameworkCore;
89
using Microsoft.EntityFrameworkCore.Query;
9-
using Utils.Enums;
1010

1111
namespace Extensions
1212
{

src/Extensions/StringExtensions.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Text;
77
using System.Text.RegularExpressions;
88
using System.Threading;
9-
using Utils.Enums;
9+
using Extensions.Enums;
1010
using static System.IO.Path;
1111

1212
namespace Extensions
@@ -53,6 +53,12 @@ public static string FromCamelCase(this string source)
5353
return returnValue;
5454
}
5555

56+
/// <summary>
57+
/// Replace a string with another string
58+
/// </summary>
59+
/// <param name="value"></param>
60+
/// <param name="newValue"></param>
61+
/// <returns>The replaced string</returns>
5662
public static string ReplaceWith(this string value, string newValue) =>
5763
newValue.IsNullOrEmpty() ? value : newValue;
5864

@@ -215,5 +221,45 @@ public static string ToTitleCase(this string title)
215221
var tmp = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(sanitized.ToLower());
216222
return Regex.Replace(tmp, "( [^a-zA-Z0-9] )", m => m.Groups[1].Value.Trim());
217223
}
224+
225+
/// <summary>
226+
/// Ensures that a string ends with a given suffix.
227+
/// </summary>
228+
/// <param name = "value">The string value to check.</param>
229+
/// <param name = "suffix">The suffix value to check for.</param>
230+
/// <returns>The string value including the suffix</returns>
231+
public static string EnsureEndsWith(this string value, string suffix)
232+
{
233+
return value?.EndsWith(suffix)==true ? value : value?.Insert(value.Length, suffix);
234+
}
235+
236+
/// <summary>
237+
/// Ensures that a string starts with a given prefix.
238+
/// </summary>
239+
/// <param name = "value">The string value to check.</param>
240+
/// <param name = "prefix">The prefix value to check for.</param>
241+
/// <returns>The string value including the prefix</returns>
242+
public static string EnsureStartsWith(this string value, string prefix)
243+
{
244+
return value?.StartsWith(prefix) ==true ? value : value?.Insert(0, prefix);
245+
}
246+
247+
/// <summary>
248+
/// Repeat a string n times
249+
/// </summary>
250+
/// <param name="str"></param>
251+
/// <param name="times">Times to be repeated</param>
252+
/// <returns>The string repeated n times</returns>
253+
public static string Repeat(this string str, int times)
254+
{
255+
var result = "";
256+
257+
if (str == default)
258+
return default;
259+
260+
for (var i = 0; i < times; i++)
261+
result += str;
262+
return result;
263+
}
218264
}
219265
}

src/Utils/KeyEqualityComparer.cs renamed to src/Extensions/Types/KeyEqualityComparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Collections.Generic;
55
using System.Linq;
66

7-
namespace Utils
7+
namespace Extensions.Types
88
{
99
public class KeyEqualityComparer<T> : IEqualityComparer<T>
1010
{

src/Infrastructure/Types/Paging/PagingOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Utils.Enums;
1+
using Extensions.Enums;
22

33
namespace Infrastructure.Types.Paging
44
{

0 commit comments

Comments
 (0)