Skip to content

Commit 4c54e00

Browse files
authored
Merge pull request #251 from MADE-Apps/feature/archived-port
Port of usable components from personal archived projects
2 parents 3f879e1 + a9926de commit 4c54e00

File tree

7 files changed

+379
-6
lines changed

7 files changed

+379
-6
lines changed

src/MADE.Collections/CollectionExtensions.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace MADE.Collections
55
{
66
using System;
77
using System.Collections.Generic;
8+
using System.Collections.ObjectModel;
89
using System.Linq;
910

1011
/// <summary>
@@ -396,5 +397,64 @@ public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
396397
{
397398
return source.OrderBy(x => Guid.NewGuid());
398399
}
400+
401+
/// <summary>Sorts the elements in the entire <see cref="ObservableCollection{T}"/> using the specified comparer.</summary>
402+
/// <param name="source">The source collection to sort.</param>
403+
/// <param name="comparer">The implementation to use when comparing elements.</param>
404+
/// <typeparam name="T">The type of item in the collection.</typeparam>
405+
/// <typeparam name="TKey">The key value of the item to sort on.</typeparam>
406+
public static void Sort<T, TKey>(this ObservableCollection<T> source, Func<T, TKey> comparer)
407+
{
408+
if (source is not { Count: > 1 })
409+
{
410+
return;
411+
}
412+
413+
var idx = 0;
414+
foreach (var originalIdx in source.OrderBy(comparer).Select(source.IndexOf))
415+
{
416+
if (originalIdx != idx)
417+
{
418+
source.Move(originalIdx, idx);
419+
}
420+
421+
idx++;
422+
}
423+
}
424+
425+
/// <summary>Sorts the elements in the entire <see cref="ObservableCollection{T}"/> using the specified comparer in descending order.</summary>
426+
/// <param name="source">The source collection to sort.</param>
427+
/// <param name="comparer">The implementation to use when comparing elements.</param>
428+
/// <typeparam name="T">The type of item in the collection.</typeparam>
429+
/// <typeparam name="TKey">The key value of the item to sort on.</typeparam>
430+
public static void SortDescending<T, TKey>(this ObservableCollection<T> source, Func<T, TKey> comparer)
431+
{
432+
if (source is not { Count: > 1 })
433+
{
434+
return;
435+
}
436+
437+
var idx = 0;
438+
foreach (var originalIdx in source.OrderByDescending(comparer).Select(source.IndexOf))
439+
{
440+
if (originalIdx != idx)
441+
{
442+
source.Move(originalIdx, idx);
443+
}
444+
445+
idx++;
446+
}
447+
}
448+
449+
/// <summary>Indicates whether the specified collection is <see langword="null" /> or empty (containing no items).</summary>
450+
/// <param name="source">The collection to test.</param>
451+
/// <typeparam name="T">The type of item in the collection.</typeparam>
452+
/// <returns>
453+
/// <see langword="true" /> if the <paramref name="source" /> parameter is <see langword="null" /> or empty (containing no items); otherwise, <see langword="false" />.
454+
/// </returns>
455+
public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
456+
{
457+
return source is null || !source.Any();
458+
}
399459
}
400460
}

src/MADE.Collections/DictionaryExtensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,29 @@ public static void AddOrUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dicti
4949

5050
dictionary.Add(key, value);
5151
}
52+
53+
/// <summary>
54+
/// Gets a value from a dictionary by the specified key, or returns a default value.
55+
/// </summary>
56+
/// <typeparam name="TKey">The type of key item within the dictionary.</typeparam>
57+
/// <typeparam name="TValue">The type of value item within the dictionary.</typeparam>
58+
/// <param name="dictionary">The dictionary to get a value from.</param>
59+
/// <param name="key">The key to get a value for.</param>
60+
/// <param name="defaultValue">The default value to return if not exists. Default, null.</param>
61+
/// <returns>The value if it exists for the key; otherwise, null.</returns>
62+
public static TValue GetValueOrDefault<TKey, TValue>(
63+
this Dictionary<TKey, TValue> dictionary,
64+
TKey key,
65+
TValue defaultValue = default)
66+
{
67+
var result = defaultValue;
68+
69+
if (dictionary != null && dictionary.ContainsKey(key))
70+
{
71+
result = dictionary[key];
72+
}
73+
74+
return result;
75+
}
5276
}
5377
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// MADE Apps licenses this file to you under the MIT license.
2+
// See the LICENSE file in the project root for more information.
3+
4+
namespace MADE.Data.Converters.Extensions
5+
{
6+
using System.Collections.Generic;
7+
8+
/// <summary>
9+
/// Defines a collection of extensions for collection objects.
10+
/// </summary>
11+
public static class CollectionExtensions
12+
{
13+
/// <summary>
14+
/// Converts a collection of items to a string separated by a delimiter.
15+
/// </summary>
16+
/// <typeparam name="T">The type of item within the collection.</typeparam>
17+
/// <param name="source">The source collection to convert.</param>
18+
/// <param name="delimiter">The delimiter to separate items by in the string. Default, comma.</param>
19+
/// <returns>A delimited string representing the collection.</returns>
20+
public static string ToDelimitedString<T>(this IEnumerable<T> source, string delimiter = ",")
21+
{
22+
return string.Join(delimiter, source);
23+
}
24+
}
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace MADE.Data.Converters.Extensions
2+
{
3+
/// <summary>
4+
/// Defines a collection of extensions for converting length measurements.
5+
/// </summary>
6+
public static class LengthExtensions
7+
{
8+
/// <summary>
9+
/// Converts a distance measured in miles to a distance measured in meters.
10+
/// </summary>
11+
/// <param name="miles">The miles to convert to meters.</param>
12+
/// <returns>The meters that represent the miles.</returns>
13+
public static double ToMeters(this double miles)
14+
{
15+
return miles * 1609.344;
16+
}
17+
18+
/// <summary>
19+
/// Converts a distance measured in meters to a distance measured in miles.
20+
/// </summary>
21+
/// <param name="meters">The meters to convert to miles.</param>
22+
/// <returns>The miles that represent the meters.</returns>
23+
public static double ToMiles(this double meters)
24+
{
25+
return meters / 1609.344;
26+
}
27+
}
28+
}

src/MADE.Runtime/Extensions/ReflectionExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace MADE.Runtime.Extensions
22
{
33
using System;
4+
using System.Collections.Generic;
45
using System.Reflection;
56

67
/// <summary>
@@ -23,5 +24,19 @@ public static T GetPropertyValue<T>(this object obj, string property)
2324
PropertyInfo prop = type.GetProperty(property);
2425
return prop?.GetValue(obj) as T;
2526
}
27+
28+
/// <summary>
29+
/// Gets all the property names declared for the specified object.
30+
/// </summary>
31+
/// <param name="obj">The object to retrieve property names from.</param>
32+
/// <returns>A collection of object property names as a string.</returns>
33+
public static IEnumerable<string> GetPropertyNames(this object obj)
34+
{
35+
Type type = obj.GetType();
36+
foreach (PropertyInfo property in type.GetTypeInfo().DeclaredProperties)
37+
{
38+
yield return property.Name;
39+
}
40+
}
2641
}
2742
}

0 commit comments

Comments
 (0)