Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernizing codebase #6

Open
wants to merge 13 commits into
base: v2/main
Choose a base branch
from
Open
262 changes: 128 additions & 134 deletions src/Rhythm.Core/CollectionExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -1,155 +1,149 @@
namespace Rhythm.Core
namespace Rhythm.Core;

using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// Extension methods for collections.
/// </summary>
public static class CollectionExtensionMethods
{

// The namespaces.
using System;
using System.Collections.Generic;
using System.Linq;
#region Properties

/// <summary>
/// Extension methods for collections.
/// Used to generate random numbers.
/// </summary>
public static class CollectionExtensionMethods
{
private static Random Rnd { get; set; }

#region Properties
#endregion

/// <summary>
/// Used to generate random numbers.
/// </summary>
private static Random Rnd { get; set; }
#region Constructors

#endregion
/// <summary>
/// Static constructor.
/// </summary>
static CollectionExtensionMethods()
{
Rnd = new Random();
}

#region Constructors
#endregion

/// <summary>
/// Static constructor.
/// </summary>
static CollectionExtensionMethods()
{
Rnd = new Random();
}
#region Extension Methods

#endregion

#region Extension Methods

/// <summary>
/// Converts a null collection into an empty collection.
/// </summary>
/// <typeparam name="T">
/// The type of item stored by the collection.
/// </typeparam>
/// <param name="items">
/// The collection of items.
/// </param>
/// <returns>
/// An empty list, if the supplied collection is null; otherwise, the supplied collection.
/// </returns>
public static IEnumerable<T> MakeSafe<T>(this IEnumerable<T> items)
{
return items == null
? Enumerable.Empty<T>()
: items;
}
/// <summary>
/// Converts a null collection into an empty collection.
/// </summary>
/// <typeparam name="T">
/// The type of item stored by the collection.
/// </typeparam>
/// <param name="items">
/// The collection of items.
/// </param>
/// <returns>
/// An empty list, if the supplied collection is null; otherwise, the supplied collection.
/// </returns>
public static IEnumerable<T> MakeSafe<T>(this IEnumerable<T> items)
{
return items ?? Enumerable.Empty<T>();
}

/// <summary>
/// Returns the collection of items without any nulls.
/// </summary>
/// <typeparam name="T">
/// The type of item stored by the collection.
/// </typeparam>
/// <param name="items">
/// The collection of items.
/// </param>
/// <returns>
/// The collection without null items.
/// </returns>
public static IEnumerable<T> WithoutNulls<T>(this IEnumerable<T> items)
{
return items.Where(x => x != null);
}
/// <summary>
/// Returns the collection of items without any nulls.
/// </summary>
/// <typeparam name="T">
/// The type of item stored by the collection.
/// </typeparam>
/// <param name="items">
/// The collection of items.
/// </param>
/// <returns>
/// The collection without null items.
/// </returns>
public static IEnumerable<T> WithoutNulls<T>(this IEnumerable<T> items)
{
return items.Where(x => x != null);
}

/// <summary>
/// Generates a sequence that conains the specified value the specified number of times.
/// </summary>
/// <typeparam name="T">
/// The type of the value to be repeated in the result sequence.
/// </typeparam>
/// <param name="element">
/// The value to be repeated.
/// </param>
/// <param name="count">
/// The number of times to repeat the value in the generated sequence.
/// </param>
/// <returns>
/// A collection containing the specified element the specified number of times.
/// </returns>
public static IEnumerable<T> Repeat<T>(this T element, int count)
{
return Enumerable.Repeat(element, count);
}
/// <summary>
/// Generates a sequence that conains the specified value the specified number of times.
/// </summary>
/// <typeparam name="T">
/// The type of the value to be repeated in the result sequence.
/// </typeparam>
/// <param name="element">
/// The value to be repeated.
/// </param>
/// <param name="count">
/// The number of times to repeat the value in the generated sequence.
/// </param>
/// <returns>
/// A collection containing the specified element the specified number of times.
/// </returns>
public static IEnumerable<T> Repeat<T>(this T element, int count)
{
return Enumerable.Repeat(element, count);
}

/// <summary>
/// Returns the specified collection of items in random order.
/// </summary>
/// <typeparam name="T">
/// The type of item stored by the collection.
/// </typeparam>
/// <param name="items">
/// The collection of items.
/// </param>
/// <returns>
/// The collection, in random order.
/// </returns>
public static IEnumerable<T> RandomOrder<T>(this IEnumerable<T> items)
/// <summary>
/// Returns the specified collection of items in random order.
/// </summary>
/// <typeparam name="T">
/// The type of item stored by the collection.
/// </typeparam>
/// <param name="items">
/// The collection of items.
/// </param>
/// <returns>
/// The collection, in random order.
/// </returns>
public static IEnumerable<T> RandomOrder<T>(this IEnumerable<T> items)
{
var itemsList = items.MakeSafe().ToList();
var randomized = new List<T>();
var count = itemsList.Count;
for(var i = 0; i < count; i++)
{
var itemsList = items.MakeSafe().ToList();
var randomized = new List<T>();
var count = itemsList.Count;
for(var i = 0; i < count; i++)
{
var index = Rnd.Next(itemsList.Count);
var item = itemsList[index];
randomized.Add(item);
itemsList[index] = itemsList.Last();
itemsList.RemoveAt(itemsList.Count - 1);
}
return randomized;
var index = Rnd.Next(itemsList.Count);
var item = itemsList[index];
randomized.Add(item);
itemsList[index] = itemsList.Last();
itemsList.RemoveAt(itemsList.Count - 1);
}
return randomized;
}

/// <summary>
/// Returns a list for the specified collection. Attempts to return the
/// underlying collection if it is already a list; otherwise, it converts
/// the collection to a list.
/// </summary>
/// <typeparam name="T">
/// The type of item stored by the collection.
/// </typeparam>
/// <param name="items">
/// The collection of items.
/// </param>
/// <returns>
/// An empty list, if the supplied collection is null; otherwise, the
/// supplied collection as a list.
/// </returns>
/// <remarks>
/// This will never return a null (i.e., it will return an empty list
/// rather than a null).
/// </remarks>
public static List<T> AsList<T>(this IEnumerable<T> items)
/// <summary>
/// Returns a list for the specified collection. Attempts to return the
/// underlying collection if it is already a list; otherwise, it converts
/// the collection to a list.
/// </summary>
/// <typeparam name="T">
/// The type of item stored by the collection.
/// </typeparam>
/// <param name="items">
/// The collection of items.
/// </param>
/// <returns>
/// An empty list, if the supplied collection is null; otherwise, the
/// supplied collection as a list.
/// </returns>
/// <remarks>
/// This will never return a null (i.e., it will return an empty list
/// rather than a null).
/// </remarks>
public static List<T> AsList<T>(this IEnumerable<T> items)
{
var list = items as List<T>;
if (list == null)
{
var list = items as List<T>;
if (list == null)
{
list = items.MakeSafe().ToList();
}
return list;
list = items.MakeSafe().ToList();
}

#endregion

return list;
}

}
#endregion

}
77 changes: 37 additions & 40 deletions src/Rhythm.Core/Enums/StringSplitDelimiters.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
namespace Rhythm.Core.Enums
namespace Rhythm.Core.Enums;

/// <summary>
/// The types of delimiters that can be used to split strings.
/// </summary>
public enum StringSplitDelimiters
{

/// <summary>
/// The types of delimiters that can be used to split strings.
/// Default will split by common delimiters (e.g., commas, line breaks, semicolons).
/// </summary>
public enum StringSplitDelimiters
{

/// <summary>
/// Default will split by common delimiters (e.g., commas, line breaks, semicolons).
/// </summary>
Default,

/// <summary>
/// Split by line breaks.
/// </summary>
LineBreak,
Default,

/// <summary>
/// Split by commas.
/// </summary>
Comma,
/// <summary>
/// Split by line breaks.
/// </summary>
LineBreak,

/// <summary>
/// Split by semicolon.
/// </summary>
Semicolon,
/// <summary>
/// Split by commas.
/// </summary>
Comma,

/// <summary>
/// Split by tabs.
/// </summary>
Tab,
/// <summary>
/// Split by semicolon.
/// </summary>
Semicolon,

/// <summary>
/// Split by equals signs.
/// </summary>
Equals,
/// <summary>
/// Split by tabs.
/// </summary>
Tab,

/// <summary>
/// Split by \.
/// </summary>
BackSlash,
/// <summary>
/// Split by equals signs.
/// </summary>
Equals,

/// <summary>
/// Split by /.
/// </summary>
ForwardSlash,
}
/// <summary>
/// Split by \.
/// </summary>
BackSlash,

}
/// <summary>
/// Split by /.
/// </summary>
ForwardSlash,
}
Loading