Skip to content

Commit

Permalink
feat: functions to clean and collapse whitespaces (#237)
Browse files Browse the repository at this point in the history
* feat: functions to clean and collapse whitespaces
* Update the automatically generated documentation related to the list of functions and predicates

---------
Co-authored-by: AppVeyor bot <[email protected]>
  • Loading branch information
Seddryck authored Dec 30, 2023
1 parent e504197 commit c76ea4c
Show file tree
Hide file tree
Showing 7 changed files with 337 additions and 219 deletions.
46 changes: 42 additions & 4 deletions Expressif.Testing/Functions/Text/CharFunctionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ public class CharFunctionsTest
[TestCase("***123***456*78****", "*", "12345678")]
[TestCase("******", "*", "")]
[TestCase("(null)", "*", "(null)")]
[TestCase(null, "*", "(null)")]
[TestCase("(empty)", "*", "(empty)")]
[TestCase("(blank)", "*", "(blank)")]
[TestCase("(blank)", " ", "(empty)")]
public void RemoveChars_Valid(string value, char charToRemove, string expected)
public void RemoveChars_Valid(string? value, char charToRemove, string expected)
=> Assert.That(new RemoveChars(() => (charToRemove)).Evaluate(value)
, Is.EqualTo(expected));

Expand All @@ -26,10 +27,11 @@ public void RemoveChars_Valid(string value, char charToRemove, string expected)
[TestCase("***123***456*78****", "*", "-", "---123---456-78----")]
[TestCase("******", "*", "-", "------")]
[TestCase("(null)", "*", "-", "(null)")]
[TestCase(null, "*", "-", "(null)")]
[TestCase("(empty)", "*", "-", "(empty)")]
[TestCase("(blank)", "*", "-", "(blank)")]
[TestCase("(blank)", " ", "-", "(blank)")]
public void ReplaceChars_Valid(string value, char charToReplace, char replacingChar, string expected)
public void ReplaceChars_Valid(string? value, char charToReplace, char replacingChar, string expected)
=> Assert.That(new ReplaceChars(() => charToReplace, () => replacingChar).Evaluate(value)
, Is.EqualTo(expected));

Expand All @@ -38,21 +40,57 @@ public void ReplaceChars_Valid(string value, char charToReplace, char replacingC
[TestCase("12345678", new[] { '2', '8', '4' }, "248")]
[TestCase("12314561789", new[] { '2', '1', '4' }, "12141")]
[TestCase("(null)", new[] { '2', '1', '4' }, "(null)")]
[TestCase(null, new[] { '2', '1', '4' }, "(null)")]
[TestCase("(empty)", new[] { '2', '1', '4' }, "(empty)")]
[TestCase("(blank)", new[] { '2', '1', '4' }, "(blank)")]
[TestCase("(blank)", new[] { '2', '1', '4' }, "(blank)")]
public void FilterChars_Chars_Valid(string value, char[] filter, string expected)
public void FilterChars_Chars_Valid(string? value, char[] filter, string expected)
=> Assert.That(new FilterChars(() => filter).Evaluate(value)
, Is.EqualTo(expected));

[Test]
[TestCase("12345678", "284", "248")]
[TestCase("12314561789", "214", "12141")]
[TestCase("(null)", "214", "(null)")]
[TestCase(null, "214", "(null)")]
[TestCase("(empty)", "214", "(empty)")]
[TestCase("(blank)", "214", "(blank)")]
[TestCase("(blank)", "214", "(blank)")]
public void FilterChars_String_Valid(string value, string filter, string expected)
public void FilterChars_String_Valid(string? value, string filter, string expected)
=> Assert.That(new FilterChars(() => filter).Evaluate(value)
, Is.EqualTo(expected));

[Test]
[TestCase("foo", "foo")]
[TestCase("foo bar", "foo bar")]
[TestCase("foo bar", "foo bar")]
[TestCase("foo\tbar", "foo\tbar")]
[TestCase("foo\t bar", "foo\tbar")]
[TestCase("foo\r\nbar", "foo\r\nbar")]
[TestCase("\t\t\tfoo\t\tbar\t\t\t", "foo\tbar")]
[TestCase("(null)", "(null)")]
[TestCase(null, "(null)")]
[TestCase("(empty)", "(empty)")]
[TestCase("(blank)", "(empty)")]
public void CollapseWhitespace_Value_Valid(string? value, string expected)
=> Assert.That(new CollapseWhitespace().Evaluate(value)
, Is.EqualTo(expected));

[Test]
[TestCase("foo", "foo")]
[TestCase("foo bar", "foo bar")]
[TestCase("foo bar", "foo bar")]
[TestCase("foo\tbar", "foo bar")]
[TestCase("foo\t bar", "foo bar")]
[TestCase("foo\r\nbar", "foo bar")]
[TestCase("foo\rbar\n", "foo bar ")]
[TestCase("foo\r\n\r\nbar", "foo bar")]
[TestCase("\t\t\tfoo\t\tbar\t\t\t", " foo bar ")]
[TestCase("(null)", "(null)")]
[TestCase(null, "(null)")]
[TestCase("(empty)", "(empty)")]
[TestCase("(blank)", "(blank)")]
public void CleanWhitespace_Value_Valid(string? value, string expected)
=> Assert.That(new CleanWhitespace().Evaluate(value)
, Is.EqualTo(expected));
}
58 changes: 52 additions & 6 deletions Expressif/Functions/Text/CharFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class RemoveChars : BaseTextFunction
public RemoveChars(Func<char> charToRemove)
=> CharToRemove = charToRemove;

protected override object EvaluateString(string value)
protected override object? EvaluateString(string value)
{
var stringBuilder = new StringBuilder();
foreach (var c in value)
Expand All @@ -27,7 +27,7 @@ protected override object EvaluateString(string value)
return stringBuilder.ToString();
}

protected override object EvaluateBlank()
protected override object? EvaluateBlank()
{
if (char.IsWhiteSpace(CharToRemove.Invoke()))
return new Empty().Keyword;
Expand All @@ -49,7 +49,7 @@ public class ReplaceChars : BaseTextFunction
public ReplaceChars(Func<char> charToReplace, Func<char> charReplacing)
=> (CharToReplace, CharReplacing) = (charToReplace, charReplacing);

protected override object EvaluateString(string value)
protected override object? EvaluateString(string value)
{
var charToReplace = CharToReplace.Invoke();
var charReplacing = CharReplacing.Invoke();
Expand All @@ -63,7 +63,7 @@ protected override object EvaluateString(string value)
return stringBuilder.ToString();
}

protected override object EvaluateBlank()
protected override object? EvaluateBlank()
=> new Whitespace().Keyword;
}

Expand All @@ -86,7 +86,7 @@ public FilterChars(Func<string> filter)
Filter = toCharArray(filter.Invoke());
}

protected override object EvaluateString(string value)
protected override object? EvaluateString(string value)
{
var filter = Filter.Invoke();

Expand All @@ -97,6 +97,52 @@ protected override object EvaluateString(string value)
return stringBuilder.ToString();
}

protected override object EvaluateBlank()
protected override object? EvaluateBlank()
=> new Whitespace().Keyword;
}

/// <summary>
/// returns the argument with any two or more consecutive whitespaces replaced by the first whitespace in the sequence and trimming the result. `\r\n` is considered as a single character.
/// </summary>
public class CollapseWhitespace : BaseTextFunction
{
protected override object? EvaluateString(string value)
{
char? previousWithespace = null;
var stringBuilder = new StringBuilder();
foreach (var c in value)
{
if (previousWithespace == '\r' && c == '\n')
previousWithespace = null;
if (previousWithespace is null || !char.IsWhiteSpace(c))
stringBuilder.Append(c);
previousWithespace = char.IsWhiteSpace(c) ? c : null;
}
return stringBuilder.ToString().Trim();
}

protected override object? EvaluateBlank()
=> new Empty().Keyword;
}

/// <summary>
/// returns the argument with any whitespace replaced by a space character. `\r\n` is considered as a single character.
/// </summary>
public class CleanWhitespace : BaseTextFunction
{
protected override object? EvaluateString(string value)
{
char? previousWithespace = null;
var stringBuilder = new StringBuilder();
foreach (var c in value)
{
if (!(previousWithespace == '\r' && c == '\n'))
stringBuilder.Append(char.IsWhiteSpace(c) ? ' ' : c);
previousWithespace = char.IsWhiteSpace(c) ? c : null;
}

return stringBuilder.ToString();
}
protected override object? EvaluateBlank()
=> new Whitespace().Keyword;
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ Install-Package Expressif
|Text | append-new-line | text-to-append-new-line |
|Text | append-space | text-to-append-space |
|Text | before-substring | text-to-before-substring |
|Text | clean-whitespace | text-to-clean-whitespace |
|Text | collapse-whitespace | text-to-collapse-whitespace |
|Text | count-distinct-chars | text-to-count-distinct-chars |
|Text | count-substring | text-to-count-substring |
|Text | empty-to-null | |
Expand Down
Loading

0 comments on commit c76ea4c

Please sign in to comment.