Skip to content

Commit 59392d4

Browse files
committed
chore: Move all the system type extensions/polyfills together
1 parent 0401b75 commit 59392d4

11 files changed

Lines changed: 93 additions & 89 deletions

src/Hyphen.Sdk/Extensions/HyphenSdkColorExtensions.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Hyphen.Sdk/Extensions/HyphenSdkDateTimeOffsetExtensions.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Hyphen.Sdk/Extensions/HyphenSdkEnumExtensions.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Hyphen.Sdk/Extensions/HyphenSdkHttpClientExtensions.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System.Drawing;
2+
using System.Globalization;
3+
using Hyphen.Sdk;
4+
5+
namespace System;
6+
7+
internal static class HyphenSdkPolyfills
8+
{
9+
extension(Color color)
10+
{
11+
public string ToCss()
12+
{
13+
var result = "#"
14+
+ ToHexChar(color.R >> 4) + ToHexChar(color.R & 0xF)
15+
+ ToHexChar(color.G >> 4) + ToHexChar(color.G & 0xF)
16+
+ ToHexChar(color.B >> 4) + ToHexChar(color.B & 0xF);
17+
18+
if (color.A != 255)
19+
result = result + ToHexChar(color.A >> 4) + ToHexChar(color.A & 0xF);
20+
21+
return result;
22+
}
23+
}
24+
25+
extension(DateTimeOffset dateTimeOffset)
26+
{
27+
public string ToISO8601() =>
28+
dateTimeOffset.ToString(@"yyyy-MM-ddTHH\:mm\:ss.fff\Z", CultureInfo.InvariantCulture);
29+
}
30+
31+
extension(File)
32+
{
33+
#if NETSTANDARD
34+
public static Task WriteAllBytesAsync(string path, byte[] bytes, CancellationToken cancellationToken)
35+
{
36+
cancellationToken.ThrowIfCancellationRequested();
37+
File.WriteAllBytes(path, bytes);
38+
return Task.CompletedTask;
39+
}
40+
#endif
41+
}
42+
43+
extension(HttpClient client)
44+
{
45+
public void SetHyphenApiKey(string apiKey) =>
46+
client.DefaultRequestHeaders.Add("x-api-key", apiKey);
47+
}
48+
49+
extension(QrCodeSize size)
50+
{
51+
public string ToFormString() =>
52+
size switch
53+
{
54+
QrCodeSize.Small => "small",
55+
QrCodeSize.Medium => "medium",
56+
QrCodeSize.Large => "large",
57+
_ => throw new ArgumentException("Unknown QrCodeSize value: " + size.ToString()),
58+
};
59+
}
60+
61+
extension(string str)
62+
{
63+
#if NETSTANDARD
64+
public bool EndsWith(char value) => str.Length != 0 && str[str.Length - 1] == value;
65+
#endif
66+
67+
public string ReplaceInvariant(string oldValue, string newValue)
68+
#if NETSTANDARD
69+
=> str.Replace(oldValue, newValue);
70+
#else
71+
=> str.Replace(oldValue, newValue, StringComparison.InvariantCulture);
72+
#endif
73+
74+
#if NETSTANDARD
75+
public bool StartsWith(char value) => str.Length != 0 && str[0] == value;
76+
#endif
77+
}
78+
79+
extension(string? str)
80+
{
81+
public string Quoted => str is null ? "null" : $@"""{str}""";
82+
}
83+
84+
static char ToHexChar(int b) => (char)(b < 10 ? b + '0' : b - 10 + 'a');
85+
}

src/Hyphen.Sdk/Extensions/HyphenSdkResourcesExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static string Env_InvalidValue(string name, string? value) =>
2626
env_InvalidValueComposite,
2727
#endif
2828
name,
29-
value.Quoted()
29+
value.Quoted
3030
);
3131

3232
public static string Env_InvalidValueType(string name, string typeName, string? value) =>
@@ -39,7 +39,7 @@ public static string Env_InvalidValueType(string name, string typeName, string?
3939
#endif
4040
name,
4141
typeName,
42-
value.Quoted()
42+
value.Quoted
4343
);
4444

4545
public static string Http_StatusCodeError(HttpStatusCode statusCode) =>

src/Hyphen.Sdk/Extensions/HyphenSdkStringExtensions.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Hyphen.Sdk/Services/Env.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ void ParseEnvFile(string path)
218218
if (required)
219219
throw new ArgumentException(HyphenSdkResources.Env_InvalidValueType(name, typeName, value), nameof(name));
220220

221-
Logger.LogWarning(HyphenSdkResources.Env_InvalidValueTypeFmt, name, typeName, value.Quoted());
221+
Logger.LogWarning(HyphenSdkResources.Env_InvalidValueTypeFmt, name, typeName, value.Quoted);
222222
return null;
223223
}
224224

src/Hyphen.Sdk/Services/Link.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Drawing;
21
using System.Net;
32
using System.Net.Http.Headers;
43
using System.Net.Http.Json;

src/Hyphen.Sdk/Types/Link/QrCodeResult.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,6 @@ public Task SaveQrCode(string fileName) =>
7575
/// The QR code images are in PNG format, so it is recommended that the file name end with <c>".png"</c>.
7676
/// </remarks>
7777
[ExcludeFromCodeCoverage] // Not testing due to file I/O (and simple implementation)
78-
public Task SaveQrCode(string fileName, CancellationToken cancellationToken)
79-
{
80-
#if NETSTANDARD
81-
cancellationToken.ThrowIfCancellationRequested();
82-
File.WriteAllBytes(fileName, GetQrCodeBytes());
83-
return Task.CompletedTask;
84-
#else
85-
return File.WriteAllBytesAsync(fileName, GetQrCodeBytes(), cancellationToken);
86-
#endif
87-
}
78+
public Task SaveQrCode(string fileName, CancellationToken cancellationToken) =>
79+
File.WriteAllBytesAsync(fileName, GetQrCodeBytes(), cancellationToken);
8880
}

0 commit comments

Comments
 (0)