Files
Berufsschule_HAM/src/Helpers/UsersHelper.cs

44 lines
1.4 KiB
C#

using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
namespace Berufsschule_HAM.Helpers;
public static partial class UsersHelper
{
public static string CreateUsername(string givenName, string surname)
{
if (string.IsNullOrWhiteSpace(givenName) || string.IsNullOrWhiteSpace(surname))
throw new ArgumentException("Given name and surname must not be empty.");
string combined = (surname + givenName).ToLowerInvariant();
combined = combined.Replace("ä", "ae").Replace("ö", "oe").Replace("ü", "ue");
// Normalize to decompose accents (e.g., é -> e + ́)
string normalized = combined.Normalize(NormalizationForm.FormD);
// Remove diacritics
var sb = new StringBuilder();
foreach (var c in normalized)
{
var category = CharUnicodeInfo.GetUnicodeCategory(c);
if (category != UnicodeCategory.NonSpacingMark)
sb.Append(c);
}
string cleaned = sb.ToString();
// Replace German ligatures etc.
cleaned = cleaned
.Replace("ß", "ss")
.Replace("æ", "ae")
.Replace("œ", "oe")
.Replace("ø", "o");
// Remove everything not a-z
cleaned = AtoZ().Replace(cleaned, "");
return cleaned;
}
[GeneratedRegex("[^a-z]")]
private static partial Regex AtoZ();
}