API.Util.Extensions - JuDelCo/Core GitHub Wiki

Namespace: Ju.Extensions

IEnumerable Extensions

// Information
bool All<TSource>(Func<TSource, bool> predicate); // all elements satisfies the predicate?
bool Any<TSource>(Func<TSource, bool> predicate); // any element satisfies the predicate?
bool Contains<TSource>(TSource value);
int Count<TSource>();
bool IsNullOrEmpty<TSource>();
bool Some<TSource>(Func<TSource, bool> predicate); // equivalent to .Any()

// Filtering
TSource ElementAt<TSource>(int index);
IEnumerable<TSource> Filter<TSource>(Func<TSource, bool> predicate);
TSource Find<TSource>(Func<TSource, bool> predicate); // throws error if not found
TSource Find<TSource>(TSource defaultValue, Func<TSource, bool> predicate); // returns default value if not found
TSource FindUnique<TSource>(Func<TSource, bool> predicate); // throws error if not found or more than one element found
TSource FindUnique<TSource>(TSource defaultValue, Func<TSource, bool> predicate); // throws error if more than one element found
TSource First<TSource>();
TSource Last<TSource>();

// Iteration
void ForEach<TSource>(Action<TSource> action);
void ForEach<TSource>(Func<TSource, bool> action); // return false to break the loop
void ForEachReverse<TSource>(Action<TSource> action); // allows to remove the item in the loop
void ForEachReverse<TSource>(Func<TSource, bool> action); // allows to remove the item in the loop, return false to break the loop

// Transformation
IEnumerable<TResult> Cast<TResult>();
IEnumerable<TSource> Concatenate<TSource>(params IEnumerable<TSource>[] others);
IEnumerable<TResult> Flatten<TSource, TResult>(Func<TSource, IEnumerable<TResult>> selector);
IEnumerable<TResult> Map<TSource, TResult>(Func<TSource, TResult> selector); // new enumerator of another type
IEnumerable<TSource> OrderBy<TSource, TKey>(Func<TSource, TKey> keySelector);
TSource Reduce<TSource>(Func<TSource, TSource, TSource> function); // collapses the entire enumerator to a single object
TResult Reduce<TSource, TResult>(TResult startValue, Func<TResult, TSource, TResult> function);
IEnumerable<TSource> Reverse<TSource>();

// Casting
TSource[] ToArray<TSource>();
IEnumerable<TSource> ToEnumerable<TSource>();
List<TSource> ToList<TSource>();

ICollection Extension

void RemoveIf<TSource>(Func<TSource, bool> condition);

IDictionary Extensions

TValue GetOrDefault<TKey, TValue>(TKey key, TValue defaultValue = default(TValue));
TValue GetOrInsertDefault<TKey, TValue>(TKey key, TValue defaultValue = default(TValue));
TValue GetOrInsertNew<TKey, TValue>(TKey key) where TValue : new();

void ForEachKey<TKey, TValue>(Action<TKey> action);
void ForEachKeyReverse<TKey, TValue>(Action<TKey> action);
void ForEachValue<TKey, TValue>(Action<TValue> action);
void ForEachValueReverse<TKey, TValue>(Action<TValue> action);

String Extensions

bool ContainsCaseInsensitive(string value);

string Truncate(int maxLength);

string SanitizePath();
string RemoveInvisibleCharacters();

Bool Extensions

int ToInt();
uint ToUInt();

Int Extensions

bool IsWithin(float min, float max); // including min and max
bool IsBetween(float min, float max); // excluding min and max

Float Extensions

bool IsWithin(float min, float max); // including min and max
bool IsBetween(float min, float max); // excluding min and max

Type Extensions

string GetFriendlyName(bool aliasNullable = true, bool includeSpaceAfterComma = true);

DateTime Extensions

string AsString(string format = "yyyyMMdd_HHmmss");
double AsUnixTimeStamp();
string AsUnixTimeStampString();
⚠️ **GitHub.com Fallback** ⚠️