mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Refactor polyfills
This commit is contained in:
26
CliFx/Utils/Polyfills.Collections.cs
Normal file
26
CliFx/Utils/Polyfills.Collections.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
// ReSharper disable CheckNamespace
|
||||||
|
|
||||||
|
#if NETSTANDARD2_0
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
internal static class CollectionPolyfills
|
||||||
|
{
|
||||||
|
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> pair, out TKey key, out TValue value)
|
||||||
|
{
|
||||||
|
key = pair.Key;
|
||||||
|
value = pair.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dic, TKey key) =>
|
||||||
|
dic.TryGetValue(key!, out var result) ? result! : default!;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace System.Linq
|
||||||
|
{
|
||||||
|
internal static class LinqPolyfills
|
||||||
|
{
|
||||||
|
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source, IEqualityComparer<T> comparer) =>
|
||||||
|
new(source, comparer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
11
CliFx/Utils/Polyfills.Streams.cs
Normal file
11
CliFx/Utils/Polyfills.Streams.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
// ReSharper disable CheckNamespace
|
||||||
|
|
||||||
|
#if NETSTANDARD2_0
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
internal static class StreamPolyfills
|
||||||
|
{
|
||||||
|
public static void Write(this Stream stream, byte[] buffer) =>
|
||||||
|
stream.Write(buffer, 0, buffer.Length);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
12
CliFx/Utils/Polyfills.Strings.cs
Normal file
12
CliFx/Utils/Polyfills.Strings.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
// ReSharper disable CheckNamespace
|
||||||
|
|
||||||
|
#if NETSTANDARD2_0
|
||||||
|
internal static class StringPolyfills
|
||||||
|
{
|
||||||
|
public static bool StartsWith(this string str, char c) =>
|
||||||
|
str.Length > 0 && str[0] == c;
|
||||||
|
|
||||||
|
public static bool EndsWith(this string str, char c) =>
|
||||||
|
str.Length > 0 && str[str.Length - 1] == c;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
// ReSharper disable CheckNamespace
|
|
||||||
|
|
||||||
#if NETSTANDARD2_0
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
internal static partial class PolyfillExtensions
|
|
||||||
{
|
|
||||||
public static bool StartsWith(this string str, char c) =>
|
|
||||||
str.Length > 0 && str[0] == c;
|
|
||||||
|
|
||||||
public static bool EndsWith(this string str, char c) =>
|
|
||||||
str.Length > 0 && str[str.Length - 1] == c;
|
|
||||||
|
|
||||||
public static string[] Split(this string str, char separator, StringSplitOptions splitOptions) =>
|
|
||||||
str.Split(new[] {separator}, splitOptions);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static partial class PolyfillExtensions
|
|
||||||
{
|
|
||||||
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> pair, out TKey key, out TValue value)
|
|
||||||
{
|
|
||||||
key = pair.Key;
|
|
||||||
value = pair.Value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static partial class PolyfillExtensions
|
|
||||||
{
|
|
||||||
public static void Write(this Stream stream, byte[] buffer) =>
|
|
||||||
stream.Write(buffer, 0, buffer.Length);
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace System.Linq
|
|
||||||
{
|
|
||||||
internal static class PolyfillExtensions
|
|
||||||
{
|
|
||||||
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source, IEqualityComparer<T> comparer) =>
|
|
||||||
new(source, comparer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace System.Collections.Generic
|
|
||||||
{
|
|
||||||
internal static class PolyfillExtensions
|
|
||||||
{
|
|
||||||
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dic, TKey key) =>
|
|
||||||
dic.TryGetValue(key!, out var result) ? result! : default!;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
Reference in New Issue
Block a user