astral-api/Astral.Core/Extensions/StringExtensions.cs
2024-12-11 20:36:30 +00:00

36 lines
1 KiB
C#

// <copyright file="StringExtensions.cs" company="alveus.dev">
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
// </copyright>
namespace Astral.Core.Extensions;
/// <summary>
/// Extension methods for strings.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Mask an email address for logging purposes.
/// </summary>
/// <param name="emailAddress">The email address to obfuscate.</param>
/// <returns>The obfuscated email address, or the original if there's an issue with the email address.</returns>
public static string MaskEmailAddress(this string emailAddress)
{
if (string.IsNullOrWhiteSpace(emailAddress))
{
return string.Empty;
}
if (!emailAddress.Contains('@'))
{
return emailAddress;
}
if (emailAddress.StartsWith('@'))
{
return emailAddress;
}
return $"{emailAddress[0]}****{emailAddress[(emailAddress.IndexOf('@') - 1) ..]}";
}
}