Skip to content

Commit

Permalink
Merge pull request #251 from MADE-Apps/feature/archived-port
Browse files Browse the repository at this point in the history
Port of usable components from personal archived projects
  • Loading branch information
tom-made authored May 24, 2022
2 parents 3f879e1 + a9926de commit 4c54e00
Show file tree
Hide file tree
Showing 7 changed files with 379 additions and 6 deletions.
60 changes: 60 additions & 0 deletions src/MADE.Collections/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace MADE.Collections
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

/// <summary>
Expand Down Expand Up @@ -396,5 +397,64 @@ public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
{
return source.OrderBy(x => Guid.NewGuid());
}

/// <summary>Sorts the elements in the entire <see cref="ObservableCollection{T}"/> using the specified comparer.</summary>
/// <param name="source">The source collection to sort.</param>
/// <param name="comparer">The implementation to use when comparing elements.</param>
/// <typeparam name="T">The type of item in the collection.</typeparam>
/// <typeparam name="TKey">The key value of the item to sort on.</typeparam>
public static void Sort<T, TKey>(this ObservableCollection<T> source, Func<T, TKey> comparer)
{
if (source is not { Count: > 1 })
{
return;
}

var idx = 0;
foreach (var originalIdx in source.OrderBy(comparer).Select(source.IndexOf))
{
if (originalIdx != idx)
{
source.Move(originalIdx, idx);
}

idx++;
}
}

/// <summary>Sorts the elements in the entire <see cref="ObservableCollection{T}"/> using the specified comparer in descending order.</summary>
/// <param name="source">The source collection to sort.</param>
/// <param name="comparer">The implementation to use when comparing elements.</param>
/// <typeparam name="T">The type of item in the collection.</typeparam>
/// <typeparam name="TKey">The key value of the item to sort on.</typeparam>
public static void SortDescending<T, TKey>(this ObservableCollection<T> source, Func<T, TKey> comparer)
{
if (source is not { Count: > 1 })
{
return;
}

var idx = 0;
foreach (var originalIdx in source.OrderByDescending(comparer).Select(source.IndexOf))
{
if (originalIdx != idx)
{
source.Move(originalIdx, idx);
}

idx++;
}
}

/// <summary>Indicates whether the specified collection is <see langword="null" /> or empty (containing no items).</summary>
/// <param name="source">The collection to test.</param>
/// <typeparam name="T">The type of item in the collection.</typeparam>
/// <returns>
/// <see langword="true" /> if the <paramref name="source" /> parameter is <see langword="null" /> or empty (containing no items); otherwise, <see langword="false" />.
/// </returns>
public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
{
return source is null || !source.Any();
}
}
}
24 changes: 24 additions & 0 deletions src/MADE.Collections/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,29 @@ public static void AddOrUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dicti

dictionary.Add(key, value);
}

/// <summary>
/// Gets a value from a dictionary by the specified key, or returns a default value.
/// </summary>
/// <typeparam name="TKey">The type of key item within the dictionary.</typeparam>
/// <typeparam name="TValue">The type of value item within the dictionary.</typeparam>
/// <param name="dictionary">The dictionary to get a value from.</param>
/// <param name="key">The key to get a value for.</param>
/// <param name="defaultValue">The default value to return if not exists. Default, null.</param>
/// <returns>The value if it exists for the key; otherwise, null.</returns>
public static TValue GetValueOrDefault<TKey, TValue>(
this Dictionary<TKey, TValue> dictionary,
TKey key,
TValue defaultValue = default)
{
var result = defaultValue;

if (dictionary != null && dictionary.ContainsKey(key))
{
result = dictionary[key];
}

return result;
}
}
}
25 changes: 25 additions & 0 deletions src/MADE.Data.Converters/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// MADE Apps licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace MADE.Data.Converters.Extensions
{
using System.Collections.Generic;

/// <summary>
/// Defines a collection of extensions for collection objects.
/// </summary>
public static class CollectionExtensions
{
/// <summary>
/// Converts a collection of items to a string separated by a delimiter.
/// </summary>
/// <typeparam name="T">The type of item within the collection.</typeparam>
/// <param name="source">The source collection to convert.</param>
/// <param name="delimiter">The delimiter to separate items by in the string. Default, comma.</param>
/// <returns>A delimited string representing the collection.</returns>
public static string ToDelimitedString<T>(this IEnumerable<T> source, string delimiter = ",")
{
return string.Join(delimiter, source);
}
}
}
28 changes: 28 additions & 0 deletions src/MADE.Data.Converters/Extensions/LengthExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace MADE.Data.Converters.Extensions
{
/// <summary>
/// Defines a collection of extensions for converting length measurements.
/// </summary>
public static class LengthExtensions
{
/// <summary>
/// Converts a distance measured in miles to a distance measured in meters.
/// </summary>
/// <param name="miles">The miles to convert to meters.</param>
/// <returns>The meters that represent the miles.</returns>
public static double ToMeters(this double miles)
{
return miles * 1609.344;
}

/// <summary>
/// Converts a distance measured in meters to a distance measured in miles.
/// </summary>
/// <param name="meters">The meters to convert to miles.</param>
/// <returns>The miles that represent the meters.</returns>
public static double ToMiles(this double meters)
{
return meters / 1609.344;
}
}
}
15 changes: 15 additions & 0 deletions src/MADE.Runtime/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace MADE.Runtime.Extensions
{
using System;
using System.Collections.Generic;
using System.Reflection;

/// <summary>
Expand All @@ -23,5 +24,19 @@ public static T GetPropertyValue<T>(this object obj, string property)
PropertyInfo prop = type.GetProperty(property);
return prop?.GetValue(obj) as T;
}

/// <summary>
/// Gets all the property names declared for the specified object.
/// </summary>
/// <param name="obj">The object to retrieve property names from.</param>
/// <returns>A collection of object property names as a string.</returns>
public static IEnumerable<string> GetPropertyNames(this object obj)
{
Type type = obj.GetType();
foreach (PropertyInfo property in type.GetTypeInfo().DeclaredProperties)
{
yield return property.Name;
}
}
}
}
Loading

0 comments on commit 4c54e00

Please sign in to comment.