post-process option for numbers; add chunks method to string extensions

This commit is contained in:
2024-08-21 15:53:32 +02:00
parent 3bd4d047a4
commit a452522332
3 changed files with 37 additions and 6 deletions
+21
View File
@@ -250,4 +250,25 @@ public static class StringExtensions
return result.ToString().ToLowerInvariant();
}
public static IEnumerable<string> Chunks(this string text, int groupSize)
{
ArgumentException.ThrowIfNullOrEmpty(text);
if (groupSize < 1)
{
throw new ArgumentException();
}
for (int i = 0; i < text.Length; i += groupSize)
{
if (groupSize + i > text.Length)
{
groupSize = text.Length - i;
}
yield return text.Substring(i, groupSize);
}
}
}
+5
View File
@@ -44,6 +44,11 @@ public class Number : ZeroEntity
/// Store current counters
/// </summary>
public List<NumberCounter> Counters { get; set; } = new();
/// <summary>
/// Post process the compiled number
/// </summary>
public virtual string PostProcess(string compiled) => compiled;
}
+11 -6
View File
@@ -77,7 +77,7 @@ public class Numbers : INumbers
await Db.Update(number);
// compiles the template and returns the rendered result
return Compile(number.Template, value, number.MinLength, date);
return Compile(number.Template, value, number.MinLength, date, number.PostProcess);
}
@@ -121,14 +121,14 @@ public class Numbers : INumbers
/// <inheritdoc />
public string Compile(string template, long value, int minLength, DateTimeOffset? date = null)
public string Compile(string template, long value, int minLength, DateTimeOffset? date = null, Func<string, string> postProcess = null)
{
return Compile(template, value, minLength, date.HasValue ? DateOnly.FromDateTime(date.Value.Date) : null);
return Compile(template, value, minLength, date.HasValue ? DateOnly.FromDateTime(date.Value.Date) : null, postProcess);
}
/// <inheritdoc />
public string Compile(string template, long value, int minLength, DateOnly? date = null)
public string Compile(string template, long value, int minLength, DateOnly? date = null, Func<string, string> postProcess = null)
{
string output = template;
MatchCollection matches = templateRegex.Matches(output);
@@ -171,6 +171,11 @@ public class Numbers : INumbers
output = output.ReplaceFirst(original, result);
}
if (postProcess != null)
{
output = postProcess(output);
}
return output;
}
@@ -300,10 +305,10 @@ public interface INumbers
/// <summary>
/// Renders the final output from the template and the value
/// </summary>
string Compile(string template, long value, int minLength, DateTimeOffset? date = null);
string Compile(string template, long value, int minLength, DateTimeOffset? date = null, Func<string, string> postProcess = null);
/// <summary>
/// Renders the final output from the template and the value
/// </summary>
string Compile(string template, long value, int minLength, DateOnly? date = null);
string Compile(string template, long value, int minLength, DateOnly? date = null, Func<string, string> postProcess = null);
}