Skip to content

Commit 98dadb9

Browse files
SeddryckAppVeyor bot
andauthored
feat: functions to retain specific set of chars into a text (#280)
* feat: functions to retain specific set of chars into a text * Update the automatically generated documentation related to the list of functions and predicates --------- Co-authored-by: AppVeyor bot <no-reply@nbiguity.io>
1 parent 6746695 commit 98dadb9

File tree

9 files changed

+455
-254
lines changed

9 files changed

+455
-254
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Expressif.Functions.Text;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Expressif.Testing.Functions.Text;
9+
10+
public class RetainFunctionsTest
11+
{
12+
[Test]
13+
[TestCase("abc12345abc6789", "123456789")]
14+
[TestCase("abc12345abc, 6789", "123456789")]
15+
[TestCase("12345", "12345")]
16+
[TestCase("-12,345.6", "123456")]
17+
[TestCase("abc", "(empty)")]
18+
[TestCase("(null)", "(null)")]
19+
[TestCase("(empty)", "(empty)")]
20+
[TestCase("(blank)", "(empty)")]
21+
public void RetainNumeric_Valid(string value, string expected)
22+
=> Assert.That(new RetainNumeric().Evaluate(value), Is.EqualTo(expected));
23+
24+
[Test]
25+
[TestCase("abc12345abc6789", "123456789")]
26+
[TestCase("abc12345abc, 6789", "12345,6789")]
27+
[TestCase("12345", "12345")]
28+
[TestCase("-12,345.6", "-12,345.6")]
29+
[TestCase("abc", "(empty)")]
30+
[TestCase("(null)", "(null)")]
31+
[TestCase("(empty)", "(empty)")]
32+
[TestCase("(blank)", "(empty)")]
33+
public void RetainNumericSymbol_Valid(string value, string expected)
34+
=> Assert.That(new RetainNumericSymbol().Evaluate(value), Is.EqualTo(expected));
35+
36+
[Test]
37+
[TestCase("abc12345abc6789", "abcabc")]
38+
[TestCase("abc12345abc, 6789", "abcabc")]
39+
[TestCase("12345", "(empty)")]
40+
[TestCase("-12,345.6", "(empty)")]
41+
[TestCase("abc", "abc")]
42+
[TestCase("(null)", "(null)")]
43+
[TestCase("(empty)", "(empty)")]
44+
[TestCase("(blank)", "(empty)")]
45+
public void RetainAlpha_Valid(string value, string expected)
46+
=> Assert.That(new RetainAlpha().Evaluate(value), Is.EqualTo(expected));
47+
48+
[Test]
49+
[TestCase("abc12345abc6789", "abc12345abc6789")]
50+
[TestCase("abc12345abc, 6789", "abc12345abc6789")]
51+
[TestCase("12345", "12345")]
52+
[TestCase("-12,345.6", "123456")]
53+
[TestCase("abc", "abc")]
54+
[TestCase("(null)", "(null)")]
55+
[TestCase("(empty)", "(empty)")]
56+
[TestCase("(blank)", "(empty)")]
57+
public void RetainAlphaNumeric_Valid(string value, string expected)
58+
=> Assert.That(new RetainAlphaNumeric().Evaluate(value), Is.EqualTo(expected));
59+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Expressif.Values.Special;
8+
using Sprache;
9+
10+
namespace Expressif.Functions.Text;
11+
public abstract class BaseTextRetain : BaseTextFunction
12+
{
13+
protected override object? EvaluateBlank() => new Empty().Keyword;
14+
15+
protected override object? EvaluateString(string value)
16+
17+
{
18+
Span<char> span = value.ToCharArray();
19+
Span<char> result = stackalloc char[value.Length];
20+
int index = 0;
21+
22+
foreach (var c in span)
23+
if (IsRetainable(c))
24+
result[index++] = c;
25+
26+
if (index == 0)
27+
return new Empty().Keyword;
28+
29+
return new string(result.Slice(0, index));
30+
}
31+
32+
protected abstract bool IsRetainable(char c);
33+
}
34+
35+
/// <summary>
36+
/// Returns the input string with all non-numeric characters removed, leaving only digits (0-9).. If the argument is `null`, it returns `null`.
37+
/// </summary>
38+
public class RetainNumeric : BaseTextRetain
39+
{
40+
protected override bool IsRetainable(char c)
41+
=> char.IsDigit(c);
42+
}
43+
44+
/// <summary>
45+
/// Returns the input string with all characters removed except for digits (0-9) and the symbols `+`, `-`, `,` and `.` If the argument is `null`, it returns `null`.
46+
/// </summary>
47+
public class RetainNumericSymbol : RetainNumeric
48+
{
49+
protected override bool IsRetainable(char c)
50+
=> base.IsRetainable(c) || c.Equals('+') || c.Equals('-') || c.Equals('.') || c.Equals(',');
51+
}
52+
53+
/// <summary>
54+
/// Returns the input string with all characters removed except for letters (A-Z, a-z). If the argument is `null`, it returns `null`.
55+
/// </summary>
56+
public class RetainAlpha : BaseTextRetain
57+
{
58+
protected override bool IsRetainable(char c)
59+
=> char.IsLetter(c);
60+
}
61+
62+
63+
/// <summary>
64+
/// Returns the input string with all characters removed except for letters (A-Z, a-z) and digits (0-9). If the argument is `null`, it returns `null`.
65+
/// </summary>
66+
public class RetainAlphaNumeric : BaseTextRetain
67+
{
68+
protected override bool IsRetainable(char c)
69+
=> char.IsLetterOrDigit(c);
70+
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ Install-Package Expressif
314314
|Text | remove-chars | text-to-remove-chars |
315315
|Text | replace-chars | text-to-replace-chars |
316316
|Text | replace-slice | text-to-replace-slice |
317+
|Text | retain-alpha | text-to-retain-alpha |
318+
|Text | retain-alpha-numeric | text-to-retain-alpha-numeric |
319+
|Text | retain-numeric | text-to-retain-numeric |
320+
|Text | retain-numeric-symbol | text-to-retain-numeric-symbol |
317321
|Text | skip-first-chars | text-to-skip-first-chars |
318322
|Text | skip-last-chars | text-to-skip-last-chars |
319323
|Text | suffix | text-to-suffix |

codecov.exe

47 KB
Binary file not shown.

0 commit comments

Comments
 (0)