mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-12 02:08:27 -04:00
Base for 4.3 work. Split Nadeko.Common into a separate project
This commit is contained in:
51
src/Nadeko.Common/Extensions/ArrayExtensions.cs
Normal file
51
src/Nadeko.Common/Extensions/ArrayExtensions.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
namespace Nadeko.Common;
|
||||
|
||||
// made for expressions because they almost never get added
|
||||
// and they get looped through constantly
|
||||
public static class ArrayExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a new array from the old array + new element at the end
|
||||
/// </summary>
|
||||
/// <param name="input">Input array</param>
|
||||
/// <param name="added">Item to add to the end of the output array</param>
|
||||
/// <typeparam name="T">Type of the array</typeparam>
|
||||
/// <returns>A new array with the new element at the end</returns>
|
||||
public static T[] With<T>(this T[] input, T added)
|
||||
{
|
||||
var newExprs = new T[input.Length + 1];
|
||||
Array.Copy(input, 0, newExprs, 0, input.Length);
|
||||
newExprs[input.Length] = added;
|
||||
return newExprs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new array by applying the specified function to every element in the input array
|
||||
/// </summary>
|
||||
/// <param name="arr">Array to modify</param>
|
||||
/// <param name="f">Function to apply</param>
|
||||
/// <typeparam name="TIn">Orignal type of the elements in the array</typeparam>
|
||||
/// <typeparam name="TOut">Output type of the elements of the array</typeparam>
|
||||
/// <returns>New array with updated elements</returns>
|
||||
public static TOut[] Map<TIn, TOut>(this TIn[] arr, Func<TIn, TOut> f)
|
||||
=> Array.ConvertAll(arr, x => f(x));
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new array by applying the specified function to every element in the input array
|
||||
/// </summary>
|
||||
/// <param name="col">Array to modify</param>
|
||||
/// <param name="f">Function to apply</param>
|
||||
/// <typeparam name="TIn">Orignal type of the elements in the array</typeparam>
|
||||
/// <typeparam name="TOut">Output type of the elements of the array</typeparam>
|
||||
/// <returns>New array with updated elements</returns>
|
||||
public static TOut[] Map<TIn, TOut>(this IReadOnlyCollection<TIn> col, Func<TIn, TOut> f)
|
||||
{
|
||||
var toReturn = new TOut[col.Count];
|
||||
|
||||
var i = 0;
|
||||
foreach (var item in col)
|
||||
toReturn[i++] = f(item);
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
}
|
107
src/Nadeko.Common/Extensions/EnumerableExtensions.cs
Normal file
107
src/Nadeko.Common/Extensions/EnumerableExtensions.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public static class EnumerableExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Concatenates the members of a collection, using the specified separator between each member.
|
||||
/// </summary>
|
||||
/// <param name="data">Collection to join</param>
|
||||
/// <param name="separator">
|
||||
/// The character to use as a separator. separator is included in the returned string only if
|
||||
/// values has more than one element.
|
||||
/// </param>
|
||||
/// <param name="func">Optional transformation to apply to each element before concatenation.</param>
|
||||
/// <typeparam name="T">The type of the members of values.</typeparam>
|
||||
/// <returns>
|
||||
/// A string that consists of the members of values delimited by the separator character. -or- Empty if values has
|
||||
/// no elements.
|
||||
/// </returns>
|
||||
public static string Join<T>(this IEnumerable<T> data, char separator, Func<T, string>? func = null)
|
||||
=> string.Join(separator, data.Select(func ?? (x => x?.ToString() ?? string.Empty)));
|
||||
|
||||
/// <summary>
|
||||
/// Concatenates the members of a collection, using the specified separator between each member.
|
||||
/// </summary>
|
||||
/// <param name="data">Collection to join</param>
|
||||
/// <param name="separator">
|
||||
/// The string to use as a separator.separator is included in the returned string only if values
|
||||
/// has more than one element.
|
||||
/// </param>
|
||||
/// <param name="func">Optional transformation to apply to each element before concatenation.</param>
|
||||
/// <typeparam name="T">The type of the members of values.</typeparam>
|
||||
/// <returns>
|
||||
/// A string that consists of the members of values delimited by the separator character. -or- Empty if values has
|
||||
/// no elements.
|
||||
/// </returns>
|
||||
public static string Join<T>(this IEnumerable<T> data, string separator, Func<T, string>? func = null)
|
||||
=> string.Join(separator, data.Select(func ?? (x => x?.ToString() ?? string.Empty)));
|
||||
|
||||
/// <summary>
|
||||
/// Randomize element order by performing the Fisher-Yates shuffle
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Item type</typeparam>
|
||||
/// <param name="items">Items to shuffle</param>
|
||||
public static IReadOnlyList<T> Shuffle<T>(this IEnumerable<T> items)
|
||||
{
|
||||
using var provider = RandomNumberGenerator.Create();
|
||||
var list = items.ToList();
|
||||
var n = list.Count;
|
||||
while (n > 1)
|
||||
{
|
||||
var box = new byte[(n / byte.MaxValue) + 1];
|
||||
int boxSum;
|
||||
do
|
||||
{
|
||||
provider.GetBytes(box);
|
||||
boxSum = box.Sum(b => b);
|
||||
} while (!(boxSum < n * (byte.MaxValue * box.Length / n)));
|
||||
|
||||
var k = boxSum % n;
|
||||
n--;
|
||||
(list[k], list[n]) = (list[n], list[k]);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConcurrentDictionary{TKey,TValue}" /> class
|
||||
/// that contains elements copied from the specified <see cref="IEnumerable{T}" />
|
||||
/// has the default concurrency level, has the default initial capacity,
|
||||
/// and uses the default comparer for the key type.
|
||||
/// </summary>
|
||||
/// <param name="dict">
|
||||
/// The <see cref="IEnumerable{T}" /> whose elements are copied to the new
|
||||
/// <see cref="ConcurrentDictionary{TKey,TValue}" />.
|
||||
/// </param>
|
||||
/// <returns>A new instance of the <see cref="ConcurrentDictionary{TKey,TValue}" /> class</returns>
|
||||
public static ConcurrentDictionary<TKey, TValue> ToConcurrent<TKey, TValue>(
|
||||
this IEnumerable<KeyValuePair<TKey, TValue>> dict)
|
||||
where TKey : notnull
|
||||
=> new(dict);
|
||||
|
||||
public static IndexedCollection<T> ToIndexed<T>(this IEnumerable<T> enumerable)
|
||||
where T : class, IIndexed
|
||||
=> new(enumerable);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a task that will complete when all of the <see cref="Task{TResult}" /> objects in an enumerable
|
||||
/// collection have completed
|
||||
/// </summary>
|
||||
/// <param name="tasks">The tasks to wait on for completion.</param>
|
||||
/// <typeparam name="TResult">The type of the completed task.</typeparam>
|
||||
/// <returns>A task that represents the completion of all of the supplied tasks.</returns>
|
||||
public static Task<TResult[]> WhenAll<TResult>(this IEnumerable<Task<TResult>> tasks)
|
||||
=> Task.WhenAll(tasks);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a task that will complete when all of the <see cref="Task" /> objects in an enumerable
|
||||
/// collection have completed
|
||||
/// </summary>
|
||||
/// <param name="tasks">The tasks to wait on for completion.</param>
|
||||
/// <returns>A task that represents the completion of all of the supplied tasks.</returns>
|
||||
public static Task WhenAll(this IEnumerable<Task> tasks)
|
||||
=> Task.WhenAll(tasks);
|
||||
}
|
35
src/Nadeko.Common/Extensions/HttpClientExtensions.cs
Normal file
35
src/Nadeko.Common/Extensions/HttpClientExtensions.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public static class HttpClientExtensions
|
||||
{
|
||||
public static HttpClient AddFakeHeaders(this HttpClient http)
|
||||
{
|
||||
AddFakeHeaders(http.DefaultRequestHeaders);
|
||||
return http;
|
||||
}
|
||||
|
||||
public static void AddFakeHeaders(this HttpHeaders dict)
|
||||
{
|
||||
dict.Clear();
|
||||
dict.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
|
||||
dict.Add("User-Agent",
|
||||
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1");
|
||||
}
|
||||
|
||||
public static bool IsImage(this HttpResponseMessage msg)
|
||||
=> IsImage(msg, out _);
|
||||
|
||||
public static bool IsImage(this HttpResponseMessage msg, out string? mimeType)
|
||||
{
|
||||
mimeType = msg.Content.Headers.ContentType?.MediaType;
|
||||
if (mimeType is "image/png" or "image/jpeg" or "image/gif")
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static long GetContentLength(this HttpResponseMessage msg)
|
||||
=> msg.Content.Headers.ContentLength ?? long.MaxValue;
|
||||
}
|
22
src/Nadeko.Common/Extensions/PipeExtensions.cs
Normal file
22
src/Nadeko.Common/Extensions/PipeExtensions.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
namespace Nadeko.Common;
|
||||
|
||||
public delegate TOut PipeFunc<TIn, out TOut>(in TIn a);
|
||||
public delegate TOut PipeFunc<TIn1, TIn2, out TOut>(in TIn1 a, in TIn2 b);
|
||||
|
||||
public static class PipeExtensions
|
||||
{
|
||||
public static TOut Pipe<TIn, TOut>(this TIn a, Func<TIn, TOut> fn)
|
||||
=> fn(a);
|
||||
|
||||
public static TOut Pipe<TIn, TOut>(this TIn a, PipeFunc<TIn, TOut> fn)
|
||||
=> fn(a);
|
||||
|
||||
public static TOut Pipe<TIn1, TIn2, TOut>(this (TIn1, TIn2) a, PipeFunc<TIn1, TIn2, TOut> fn)
|
||||
=> fn(a.Item1, a.Item2);
|
||||
|
||||
public static (TIn, TExtra) With<TIn, TExtra>(this TIn a, TExtra b)
|
||||
=> (a, b);
|
||||
|
||||
public static async Task<TOut> Pipe<TIn, TOut>(this Task<TIn> a, Func<TIn, TOut> fn)
|
||||
=> fn(await a);
|
||||
}
|
Reference in New Issue
Block a user