Files
Flypaper/src/Flypaper.Web/Components/Shared/Formatting.cs
T

48 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace Flypaper.Web.Components.Shared;
public static class Formatting
{
public static string Duration(TimeSpan duration) => duration switch
{
{ TotalDays: >= 1 } => $"{(int)duration.TotalDays}\u00a0d {duration.Hours}\u00a0h",
{ TotalHours: >= 1 } => $"{duration.Hours}\u00a0h {duration.Minutes}\u00a0min",
{ TotalMinutes: >= 1 } => $"{duration.Minutes}\u00a0min {duration.Seconds}\u00a0s",
_ => $"{(int)duration.TotalSeconds}\u00a0s"
};
public static string Flag(string? countryCode) =>
countryCode is { Length: 2 } && char.IsAsciiLetter(countryCode[0]) && char.IsAsciiLetter(countryCode[1])
? string.Concat(countryCode.ToUpperInvariant().Select(c => char.ConvertFromUtf32(0x1F1E6 + c - 'A')))
: "";
public static string Bytes(long? bytes) => bytes switch
{
null => "–",
>= 1024 * 1024 * 1024 => $"{bytes / (1024d * 1024 * 1024):0.0} GB",
>= 1024 * 1024 => $"{bytes / (1024d * 1024):0.0} MB",
>= 1024 => $"{bytes / 1024d:0} KB",
_ => $"{bytes} B"
};
public static string? Target(string? target)
{
var space = target?.IndexOf(' ') ?? -1;
if (space < 0)
{
return target;
}
var hostAndPath = target![(space + 1)..];
if (hostAndPath.StartsWith('/'))
{
return $"{target[..space]} (ohne Host){hostAndPath}";
}
return hostAndPath == "_" || hostAndPath.StartsWith("_/", StringComparison.Ordinal)
? $"{target[..space]} (ohne Host){hostAndPath[1..]}"
: target;
}
public static string? Provider(long? asn, string? provider) => asn is null ? provider : $"AS{asn} {provider}".TrimEnd();
}