Skip to content

Commit 0bde84a

Browse files
authored
Merge pull request #232 from MADE-Apps/feature/improvements
Added new extensions and helpers across packages
2 parents 71d0ae3 + 6e0e952 commit 0bde84a

File tree

58 files changed

+933
-115
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+933
-115
lines changed

.gitignore

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Ignore Visual Studio temporary files, build results, and
22
## files generated by popular Visual Studio add-ons.
33
##
4-
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
4+
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
55

66
# User-specific files
77
*.rsuser
@@ -23,6 +23,7 @@ mono_crash.*
2323
[Rr]eleases/
2424
x64/
2525
x86/
26+
[Ww][Ii][Nn]32/
2627
[Aa][Rr][Mm]/
2728
[Aa][Rr][Mm]64/
2829
bld/
@@ -61,6 +62,9 @@ project.lock.json
6162
project.fragment.lock.json
6263
artifacts/
6364

65+
# ASP.NET Scaffolding
66+
ScaffoldingReadMe.txt
67+
6468
# StyleCop
6569
StyleCopReport.xml
6670

@@ -86,6 +90,7 @@ StyleCopReport.xml
8690
*.tmp_proj
8791
*_wpftmp.csproj
8892
*.log
93+
*.tlog
8994
*.vspscc
9095
*.vssscc
9196
.builds
@@ -137,6 +142,11 @@ _TeamCity*
137142
.axoCover/*
138143
!.axoCover/settings.json
139144

145+
# Coverlet is a free, cross platform Code Coverage Tool
146+
coverage*.json
147+
coverage*.xml
148+
coverage*.info
149+
140150
# Visual Studio code coverage results
141151
*.coverage
142152
*.coveragexml
@@ -284,6 +294,17 @@ node_modules/
284294
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
285295
*.vbw
286296

297+
# Visual Studio 6 auto-generated project file (contains which files were open etc.)
298+
*.vbp
299+
300+
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
301+
*.dsw
302+
*.dsp
303+
304+
# Visual Studio 6 technical files
305+
*.ncb
306+
*.aps
307+
287308
# Visual Studio LightSwitch build output
288309
**/*.HTMLClient/GeneratedArtifacts
289310
**/*.DesktopClient/GeneratedArtifacts
@@ -340,6 +361,9 @@ ASALocalRun/
340361
# Local History for Visual Studio
341362
.localhistory/
342363

364+
# Visual Studio History (VSHistory) files
365+
.vshistory/
366+
343367
# BeatPulse healthcheck temp database
344368
healthchecksdb
345369

@@ -348,3 +372,28 @@ MigrationBackup/
348372

349373
# Ionide (cross platform F# VS Code tools) working folder
350374
.ionide/
375+
376+
# Fody - auto-generated XML schema
377+
FodyWeavers.xsd
378+
379+
# VS Code files for those working on multiple tools
380+
.vscode/*
381+
!.vscode/settings.json
382+
!.vscode/tasks.json
383+
!.vscode/launch.json
384+
!.vscode/extensions.json
385+
*.code-workspace
386+
387+
# Local History for Visual Studio Code
388+
.history/
389+
390+
# Windows Installer files from build outputs
391+
*.cab
392+
*.msi
393+
*.msix
394+
*.msm
395+
*.msp
396+
397+
# JetBrains Rider
398+
*.sln.iml
399+
.idea/

src/MADE.Collections/CollectionExtensions.cs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,60 @@ namespace MADE.Collections
1212
/// </summary>
1313
public static class CollectionExtensions
1414
{
15+
/// <summary>
16+
/// Adds the specified item to the collection based on the specified condition being true.
17+
/// </summary>
18+
/// <param name="collection">The collection to add the item to.</param>
19+
/// <param name="item">The item to add.</param>
20+
/// <param name="condition">The condition required to add the item.</param>
21+
/// <typeparam name="T">The type of item within the collection.</typeparam>
22+
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="collection"/> or <paramref name="condition"/> is <see langword="null"/>.</exception>
23+
/// <exception cref="Exception">Potentially thrown by the delegate callback.</exception>
24+
public static void AddIf<T>(this IList<T> collection, T item, Func<bool> condition)
25+
{
26+
if (collection == null)
27+
{
28+
throw new ArgumentNullException(nameof(collection));
29+
}
30+
31+
if (condition == null)
32+
{
33+
throw new ArgumentNullException(nameof(condition));
34+
}
35+
36+
if (condition())
37+
{
38+
collection.Add(item);
39+
}
40+
}
41+
42+
/// <summary>
43+
/// Removes the specified item from the collection based on the specified condition being true.
44+
/// </summary>
45+
/// <param name="collection">The collection to remove the item from.</param>
46+
/// <param name="item">The item to remove.</param>
47+
/// <param name="condition">The condition required to remove the item.</param>
48+
/// <typeparam name="T">The type of item within the collection.</typeparam>
49+
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="collection"/> or <paramref name="condition"/> is <see langword="null"/>.</exception>
50+
/// <exception cref="Exception">Potentially thrown by the delegate callback.</exception>
51+
public static void RemoveIf<T>(this IList<T> collection, T item, Func<bool> condition)
52+
{
53+
if (collection == null)
54+
{
55+
throw new ArgumentNullException(nameof(collection));
56+
}
57+
58+
if (condition == null)
59+
{
60+
throw new ArgumentNullException(nameof(condition));
61+
}
62+
63+
if (condition())
64+
{
65+
collection.Remove(item);
66+
}
67+
}
68+
1569
/// <summary>
1670
/// Updates an item within the collection.
1771
/// </summary>
@@ -114,6 +168,31 @@ public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> it
114168
}
115169
}
116170

171+
/// <summary>
172+
/// Adds the specified collection of items to the collection based on the specified condition being true.
173+
/// </summary>
174+
/// <param name="collection">The collection to add the items to.</param>
175+
/// <param name="itemsToAdd">The items to add.</param>
176+
/// <param name="condition">The condition required to add the items.</param>
177+
/// <typeparam name="T">The type of item within the collection.</typeparam>
178+
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="collection"/>, <paramref name="itemsToAdd"/> or <paramref name="condition"/> is <see langword="null"/>.</exception>
179+
/// <exception cref="Exception">Potentially thrown by the delegate callback.</exception>
180+
public static void AddRangeIf<T>(
181+
this ICollection<T> collection,
182+
IEnumerable<T> itemsToAdd,
183+
Func<bool> condition)
184+
{
185+
if (condition == null)
186+
{
187+
throw new ArgumentNullException(nameof(condition));
188+
}
189+
190+
if (condition())
191+
{
192+
collection.AddRange(itemsToAdd);
193+
}
194+
}
195+
117196
/// <summary>
118197
/// Removes a collection of items from another.
119198
/// </summary>
@@ -148,6 +227,31 @@ public static void RemoveRange<T>(this ICollection<T> collection, IEnumerable<T>
148227
}
149228
}
150229

230+
/// <summary>
231+
/// Removes the specified collection of items from the collection based on the specified condition being true.
232+
/// </summary>
233+
/// <param name="collection">The collection to remove the items from.</param>
234+
/// <param name="itemsToRemove">The items to remove.</param>
235+
/// <param name="condition">The condition required to remove the items.</param>
236+
/// <typeparam name="T">The type of item within the collection.</typeparam>
237+
/// <exception cref="ArgumentNullException">Thrown if the <paramref name="collection"/>, <paramref name="itemsToRemove"/> or <paramref name="condition"/> is <see langword="null"/>.</exception>
238+
/// <exception cref="Exception">Potentially thrown by the delegate callback.</exception>
239+
public static void RemoveRangeIf<T>(
240+
this ICollection<T> collection,
241+
IEnumerable<T> itemsToRemove,
242+
Func<bool> condition)
243+
{
244+
if (condition == null)
245+
{
246+
throw new ArgumentNullException(nameof(condition));
247+
}
248+
249+
if (condition())
250+
{
251+
collection.RemoveRange(itemsToRemove);
252+
}
253+
}
254+
151255
/// <summary>
152256
/// Determines whether two collections are equivalent, containing all the same items with no regard to order.
153257
/// </summary>
@@ -219,6 +323,7 @@ public static IEnumerable<T> TakeFrom<T>(this List<T> list, int startingIndex, i
219323
/// <param name="action">
220324
/// The action to perform.
221325
/// </param>
326+
/// <exception cref="Exception">Potentially thrown by the delegate callback.</exception>
222327
public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action)
223328
{
224329
foreach (T item in collection)
@@ -262,6 +367,7 @@ public static int InsertAtPotentialIndex<T>(this IList<T> source, T value, Func<
262367
/// <param name="predicate">The action to run to determine the position of the item based on the provided <paramref name="value"/> and an item in the collection.</param>
263368
/// <typeparam name="T">The type of items in the collection.</typeparam>
264369
/// <returns>The potential index of the item.</returns>
370+
/// <exception cref="Exception">Potentially thrown by the delegate callback.</exception>
265371
public static int PotentialIndexOf<T>(this IList<T> source, T value, Func<T, T, bool> predicate)
266372
{
267373
var result = 0;
@@ -279,5 +385,16 @@ public static int PotentialIndexOf<T>(this IList<T> source, T value, Func<T, T,
279385

280386
return result;
281387
}
388+
389+
/// <summary>
390+
/// Shuffles the elements of a sequence randomly.
391+
/// </summary>
392+
/// <param name="source">The collection to shuffle.</param>
393+
/// <typeparam name="T">The type of item in the collection.</typeparam>
394+
/// <returns>The shuffled collection of items.</returns>
395+
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
396+
{
397+
return source.OrderBy(x => Guid.NewGuid());
398+
}
282399
}
283400
}

src/MADE.Collections/ObjectModel/ObservableItemCollection{T}.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class ObservableItemCollection<T> : ObservableCollection<T>, IDisposable
2727
/// <summary>
2828
/// Initializes a new instance of the <see cref="ObservableItemCollection{T}"/> class that is empty and has a default initial capacity.
2929
/// </summary>
30+
/// <exception cref="Exception">Potentially thrown by the <see cref="CollectionChanged"/> callback.</exception>
3031
public ObservableItemCollection()
3132
{
3233
base.CollectionChanged += (s, e) =>
@@ -46,6 +47,7 @@ public ObservableItemCollection()
4647
/// The collection whose elements are copied to the new list.
4748
/// </param>
4849
/// <exception cref="T:System.ArgumentNullException">The <paramref name="collection">collection</paramref> parameter cannot be null.</exception>
50+
/// <exception cref="Exception">Potentially thrown by the <see cref="CollectionChanged"/> callback.</exception>
4951
public ObservableItemCollection(IEnumerable<T> collection)
5052
: base(collection)
5153
{
@@ -65,6 +67,7 @@ public ObservableItemCollection(IEnumerable<T> collection)
6567
/// The list whose elements are copied to the new list.
6668
/// </param>
6769
/// <exception cref="T:System.ArgumentNullException">The <paramref name="list">list</paramref> parameter cannot be null.</exception>
70+
/// <exception cref="Exception">Potentially thrown by the <see cref="CollectionChanged"/> callback.</exception>
6871
public ObservableItemCollection(List<T> list)
6972
: base(list)
7073
{
@@ -93,6 +96,7 @@ public ObservableItemCollection(List<T> list)
9396
/// <param name="items">
9497
/// The objects to add to the end of the collection.
9598
/// </param>
99+
/// <exception cref="Exception">Potentially thrown by the <see cref="CollectionChanged"/> callback.</exception>
96100
public void AddRange(IEnumerable<T> items)
97101
{
98102
this.CheckDisposed();
@@ -117,6 +121,7 @@ public void AddRange(IEnumerable<T> items)
117121
/// <param name="items">
118122
/// The objects to remove from the collection.
119123
/// </param>
124+
/// <exception cref="Exception">Potentially thrown by the <see cref="CollectionChanged"/> callback.</exception>
120125
public void RemoveRange(IEnumerable<T> items)
121126
{
122127
this.CheckDisposed();

src/MADE.Data.Converters/BooleanToStringValueConverter.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public object Convert(object value, Type targetType, object parameter, string la
8080
/// <returns>The converted <see cref="bool"/> object.</returns>
8181
public object ConvertBack(object value, Type targetType, object parameter, string language)
8282
{
83-
if (!(value is string b))
83+
if (value is not string b)
8484
{
8585
return value;
8686
}

src/MADE.Data.Converters/Constants/DateTimeConstants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ namespace MADE.Data.Converters.Constants
77
/// </summary>
88
public static class DateTimeConstants
99
{
10+
/// <summary>
11+
/// Defines the minimum value for a <see cref="DateTime"/> object determined by Unix.
12+
/// </summary>
13+
public static readonly DateTime UnixEpoch = new(1970, 1, 1, 0, 0, 0);
14+
1015
/// <summary>
1116
/// Defines the time at the end of a day.
1217
/// </summary>

src/MADE.Data.Converters/Extensions/DateTimeExtensions.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@ namespace MADE.Data.Converters.Extensions
1010
/// </summary>
1111
public static class DateTimeExtensions
1212
{
13+
/// <summary>
14+
/// Gets the day suffix for the specified date, i.e. st, nd, rd, or th.
15+
/// </summary>
16+
/// <param name="dateTime">The date to get a day suffix for.</param>
17+
/// <returns>The day suffix as a string.</returns>
18+
public static string ToDaySuffix(this DateTime dateTime)
19+
{
20+
switch (dateTime.Day)
21+
{
22+
case 1:
23+
case 21:
24+
case 31:
25+
return "st";
26+
case 2:
27+
case 22:
28+
return "nd";
29+
case 3:
30+
case 23:
31+
return "rd";
32+
default:
33+
return "th";
34+
}
35+
}
36+
1337
/// <summary>
1438
/// Gets the current age in years based on the specified starting date and today's date.
1539
/// </summary>
@@ -206,7 +230,7 @@ public static DateTime EndOfYear(this DateTime dateTime)
206230
/// </returns>
207231
public static DateTime? SetTime(this DateTime? dateTime, int hours, int minutes, int seconds, int milliseconds)
208232
{
209-
return dateTime == null ? (DateTime?)null : SetTime(dateTime.Value, hours, minutes, seconds, milliseconds);
233+
return dateTime == null ? null : SetTime(dateTime.Value, hours, minutes, seconds, milliseconds);
210234
}
211235

212236
/// <summary>

0 commit comments

Comments
 (0)