Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 106 additions & 109 deletions shesha-core/src/Shesha.Framework/Utilities/StringHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -864,16 +864,13 @@ public static string SplitUpperCaseToString(this string source)
return String.Join(" ", SplitUpperCase(source));
}

private static readonly Regex ValidEmailExpression = new Regex(
@"^([a-zA-Z0-9_\-\.\+]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,}|[0-9]{1,3})(\]?)$",
RegexOptions.Compiled);

public static bool IsValidEmail(this string inputEmail)
{
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex re = new Regex(strRegex);
if (re.IsMatch(inputEmail))
return (true);
else
return (false);
return !string.IsNullOrWhiteSpace(inputEmail) && ValidEmailExpression.IsMatch(inputEmail);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

[DebuggerStepThrough]
Expand All @@ -895,25 +892,25 @@ public static string StripHtml(this string target)
.Replace("&lt;", "<");

return result;
}
/// <summary>
/// Wrap <paramref name="html"/> into the html and body tags if not wrapped
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string WrapAsHtmlDocument(this string html)
{
if (html == null)
return html;
if (!Regex.IsMatch(html, @"\</body\>"))
html = "<body>" + html + "</body>";
if (!Regex.IsMatch(html, @"\</html\>"))
html = "<html>" + html + "</html>";
return html;
}

/// <summary>
/// Wrap <paramref name="html"/> into the html and body tags if not wrapped
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string WrapAsHtmlDocument(this string html)
{
if (html == null)
return html;

if (!Regex.IsMatch(html, @"\</body\>"))
html = "<body>" + html + "</body>";

if (!Regex.IsMatch(html, @"\</html\>"))
html = "<html>" + html + "</html>";

return html;
}

private static string HtmlTrim(string html, params char[] trimChars)
Expand Down Expand Up @@ -959,92 +956,92 @@ public static string RemoveNonASCII(this string value)
return cleanStr;
}

/// <summary>
/// Adds double quotes to the specified <paramref name="value"/>
/// </summary>
/// <summary>
/// Adds double quotes to the specified <paramref name="value"/>
/// </summary>
public static string DoubleQuote(this string value)
{
{
return !string.IsNullOrWhiteSpace(value)
? "\"" + value + "\""
: value;
}
/// <summary>
/// Converts string to the snake_case format
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string ToSnakeCase(this string input)
{
if (string.IsNullOrEmpty(input)) { return input; }
return Regex.Replace(Regex.Replace(input, "(.)([A-Z][a-z]+)", "$1_$2"), "([a-z0-9])([A-Z])", "$1_$2").ToLower().RemoveDoubleUndescores();
}
/// <summary>
/// Remove multiple underacore `_` characters from string
/// </summary>
/// <returns></returns>
public static string RemoveDoubleUndescores(this string input)
{
return Regex.Replace(input, @"[_]{2,}", "_");
}
/// <summary>
/// Trim each part of the snake_case text to match the length
/// </summary>
/// <param name="text">snake_case text</param>
/// <param name="length">Max length</param>
/// <returns></returns>
public static string SnakeCaseTrim(this string text, int length)
{
if (text.IsNullOrEmpty())
return text;
var delta = text?.Length - length;
if (delta < 1)
return text;
var parts = text.Split('_');
if (parts.Length < 2)
return text.Substring(0, length);
while (delta > 0)
{
for (var i = 0; i < parts.Length; i++)
{
parts[i] = parts[i].Substring(0, parts[i].Length - 1);
if (delta-- < 1)
break;
}
}
return string.Join("_", parts);
}
/// <summary>
/// Trim each part of the snake_case text to match the length
/// </summary>
/// <param name="parts">parts to compile snake_case text</param>
/// <param name="length">Max length</param>
/// <returns></returns>
public static string SnakeCaseTrim(this string[] parts, int length)
{
var text = string.Join("_", parts).ToSnakeCase();
if (text.IsNullOrEmpty())
return text;
var delta = text?.Length - length;
if (delta < 1)
return text;
if (parts.Length < 2)
return text.Substring(0, length);
while (delta > 0)
{
for (var i = 0; i < parts.Length; i++)
{
parts[i] = parts[i].Substring(0, parts[i].Length - 1);
if (delta-- < 1)
break;
}
}
return string.Join("_", parts).ToSnakeCase();
}

/// <summary>
/// Converts string to the snake_case format
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string ToSnakeCase(this string input)
{
if (string.IsNullOrEmpty(input)) { return input; }

return Regex.Replace(Regex.Replace(input, "(.)([A-Z][a-z]+)", "$1_$2"), "([a-z0-9])([A-Z])", "$1_$2").ToLower().RemoveDoubleUndescores();
}

/// <summary>
/// Remove multiple underacore `_` characters from string
/// </summary>
/// <returns></returns>
public static string RemoveDoubleUndescores(this string input)
{
return Regex.Replace(input, @"[_]{2,}", "_");
}

/// <summary>
/// Trim each part of the snake_case text to match the length
/// </summary>
/// <param name="text">snake_case text</param>
/// <param name="length">Max length</param>
/// <returns></returns>
public static string SnakeCaseTrim(this string text, int length)
{
if (text.IsNullOrEmpty())
return text;
var delta = text?.Length - length;
if (delta < 1)
return text;
var parts = text.Split('_');
if (parts.Length < 2)
return text.Substring(0, length);
while (delta > 0)
{
for (var i = 0; i < parts.Length; i++)
{
parts[i] = parts[i].Substring(0, parts[i].Length - 1);
if (delta-- < 1)
break;
}
}
return string.Join("_", parts);
}
Comment thread
MarshallRJ marked this conversation as resolved.

/// <summary>
/// Trim each part of the snake_case text to match the length
/// </summary>
/// <param name="parts">parts to compile snake_case text</param>
/// <param name="length">Max length</param>
/// <returns></returns>
public static string SnakeCaseTrim(this string[] parts, int length)
{
var text = string.Join("_", parts).ToSnakeCase();

if (text.IsNullOrEmpty())
return text;
var delta = text?.Length - length;
if (delta < 1)
return text;
if (parts.Length < 2)
return text.Substring(0, length);
while (delta > 0)
{
for (var i = 0; i < parts.Length; i++)
{
parts[i] = parts[i].Substring(0, parts[i].Length - 1);
if (delta-- < 1)
break;
}
}
return string.Join("_", parts).ToSnakeCase();
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Shesha.Tests.StringHelper
{
public class StringHelperIsValidEmailTests
{
[Theory]
[InlineData("rob@test.com", true)]
[InlineData("rob@test.africa", true)]
[InlineData("rob+dep@test.africa", true)]
//Invalid email addresses
[InlineData("rob", false)]
[InlineData("rob@", false)]
[InlineData("rob@.com", false)]
[InlineData("rob@com", false)]
[InlineData("@test.com", false)]
[InlineData("rob@test.com ", false)]
[InlineData(" rob@test.com", false)]
[InlineData("rob@test .com", false)]
[InlineData("", false)]
[InlineData(" ", false)]
[InlineData(null, false)]

public void Should_Validate_Email_Addresses(string email, bool expected)
{
var result = Shesha.Utilities.StringHelper.IsValidEmail(email);

Assert.Equal(expected, result);
}
}
}