90 lines
3.1 KiB
C#
90 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace gehGassiApp.Core.Helper
|
|
{
|
|
/// <summary>
|
|
/// Erweiterungsmethoden
|
|
/// </summary>
|
|
public static class Extensions
|
|
{
|
|
/// <summary>
|
|
/// Gibt einen Text gekürzt auf eine max. Zeichenzahl zurück.
|
|
/// Wenn der Text länger als maxLength war, wird "..." angehängt.
|
|
/// </summary>
|
|
/// <param name="text">Text</param>
|
|
/// <param name="maxLength">max. Länge</param>
|
|
/// <returns>Text gekürzt</returns>
|
|
public static string Ellipsis(this string text, int maxLength)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
return "";
|
|
if (maxLength > 0 && text.Length > maxLength)
|
|
{
|
|
return text.Substring(0, maxLength) + "...";
|
|
}
|
|
return text;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gibt zurück ob ein String mit "http" startet.
|
|
/// Wird bei Fotos verwendet um zwischen lokalen und online Fotos zu unterscheiden
|
|
/// </summary>
|
|
/// <param name="text">Text der geprüft werden soll</param>
|
|
/// <returns>true wenn mit http startet, false sonst</returns>
|
|
public static bool StartsWithHttp(this string text)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(text))
|
|
{
|
|
return text.ToLower().StartsWith("http");
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Vergleichen von 2 Objekten mit Hilfe von Json Serialization
|
|
/// </summary>
|
|
/// <typeparam name="T">Typ der Objekte</typeparam>
|
|
/// <param name="first">Erstes Objekt</param>
|
|
/// <param name="second">Zweites Objekt</param>
|
|
/// <returns>true wenn gleich, false sonst</returns>
|
|
public static bool JsonEquals<T>(this T first, T second) where T : class
|
|
{
|
|
if (ReferenceEquals(first, second)) return true;
|
|
if ((first == null) || (second == null)) return false;
|
|
|
|
try
|
|
{
|
|
var firstJson = JsonSerializer.Serialize(first, new JsonSerializerOptions(JsonSerializerDefaults.Web));
|
|
var secondJson = JsonSerializer.Serialize(second, new JsonSerializerOptions(JsonSerializerDefaults.Web));
|
|
|
|
var result = firstJson.Equals(secondJson, StringComparison.InvariantCultureIgnoreCase);
|
|
return result;
|
|
}
|
|
catch{}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Konvertieren eines DateTimeOffeset in ein DateTime
|
|
/// </summary>
|
|
/// <param name="dateTime"></param>
|
|
/// <returns></returns>
|
|
public static DateTime ToDateTime(this DateTimeOffset dateTime)
|
|
{
|
|
if (dateTime.Offset.Equals(TimeSpan.Zero))
|
|
return dateTime.UtcDateTime;
|
|
else if (dateTime.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(dateTime.DateTime)))
|
|
return DateTime.SpecifyKind(dateTime.DateTime, DateTimeKind.Local);
|
|
else
|
|
return dateTime.DateTime;
|
|
}
|
|
}
|
|
}
|