Skip to content

Commit 24dfdca

Browse files
committed
Update HideEmail to support multiple email addresses
The `HideEmail` method in the `StringHelpers` class has been modified to handle multiple email addresses in a single string. The parameter name has been changed from `email` to `emailString`, and the documentation has been updated accordingly. The logic now uses a regular expression to identify email patterns, processing each email address individually. A new test case has also been added to `StringHelperTests` to verify this functionality.
1 parent ebd6acd commit 24dfdca

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

Sources/EasyExtensions.Tests/StringHelperTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,13 @@ public void HideEmail_InvalidInput_ValidOutput()
7777
const string expected = "**@gmail.com";
7878
Assert.That(StringHelpers.HideEmail(actual), Is.EqualTo(expected));
7979
}
80+
81+
[Test]
82+
public void HideEmail_MultipleInput_ValidOutput()
83+
{
84+
const string actual = "ac@gmail.com <test@outlook.com>";
85+
const string expected = "**@gmail.com <t**t@outlook.com>";
86+
Assert.That(StringHelpers.HideEmail(actual), Is.EqualTo(expected));
87+
}
8088
}
8189
}

Sources/EasyExtensions/Helpers/StringHelpers.cs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Security.Cryptography;
3+
using System.Text.RegularExpressions;
34

45
namespace EasyExtensions.Helpers
56
{
@@ -73,33 +74,47 @@ public static int LevenshteinDistance(string left, string right)
7374
}
7475

7576
/// <summary>
76-
/// Hide email address.
77+
/// Hide email address(es).
7778
/// </summary>
78-
/// <param name="email">Email address.</param>
79+
/// <param name="emailString">One or more email addresses.</param>
7980
/// <param name="hiddenChar">Character used to hide email address.</param>
80-
/// <returns>Hidden email address, ex. t...a@test.com</returns>
81-
public static string HideEmail(string email, char hiddenChar = '*')
81+
/// <returns>Hidden email address(es), ex. t***a@test.com</returns>
82+
public static string HideEmail(string emailString, char hiddenChar = '*')
8283
{
83-
if (string.IsNullOrWhiteSpace(email))
84+
if (string.IsNullOrWhiteSpace(emailString))
8485
{
85-
return email;
86+
return emailString;
8687
}
87-
var atIndex = email.IndexOf('@');
88-
if (atIndex < 1)
89-
{
90-
return email;
91-
}
92-
// t....a@test.com
93-
string firstPart = email[..atIndex];
94-
string lastPart = email[atIndex..];
95-
if (firstPart.Length < 3)
88+
89+
// Regex pattern to match email addresses
90+
// This pattern looks for something@domain.com format
91+
string emailPattern = @"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}";
92+
93+
return Regex.Replace(emailString, emailPattern, match =>
9694
{
97-
return new string(hiddenChar, firstPart.Length) + lastPart;
98-
}
99-
string hiddenPart = firstPart[1..^1];
100-
string hidden = firstPart[0] + new string(hiddenChar, hiddenPart.Length) + firstPart[^1];
101-
return hidden + lastPart;
102-
}
95+
string email = match.Value;
96+
int atIndex = email.IndexOf('@');
97+
98+
if (atIndex < 1)
99+
{
100+
return email; // Not a valid email or @ is the first character
101+
}
102+
103+
string firstPart = email[..atIndex];
104+
string lastPart = email[atIndex..];
105+
106+
if (firstPart.Length < 3)
107+
{
108+
// If the local part is less than 3 characters, replace all with hidden char
109+
return new string(hiddenChar, firstPart.Length) + lastPart;
110+
}
111+
else
112+
{
113+
// Keep first and last character, replace middle with hidden char
114+
return firstPart[0] + new string(hiddenChar, firstPart.Length - 2) + firstPart[^1] + lastPart;
115+
}
116+
});
117+
}
103118

104119
/// <summary>
105120
/// Fast generate pseudo random string with <see cref="DefaultCharset"/> and string length.

0 commit comments

Comments
 (0)