TokenHandler 1.0.1
dotnet add package TokenHandler --version 1.0.1
NuGet\Install-Package TokenHandler -Version 1.0.1
<PackageReference Include="TokenHandler" Version="1.0.1" />
<PackageVersion Include="TokenHandler" Version="1.0.1" />
<PackageReference Include="TokenHandler" />
paket add TokenHandler --version 1.0.1
#r "nuget: TokenHandler, 1.0.1"
#:package TokenHandler@1.0.1
#addin nuget:?package=TokenHandler&version=1.0.1
#tool nuget:?package=TokenHandler&version=1.0.1
TokenUtility Class
The TokenUtility class, implemented within the TokenHandler.Utilities namespace, provides utility methods for working with tokens embedded in strings. It supports extracting tokens in the format {{TokenName}} and replacing them with values provided in a dictionary.
Features
- Extract Tokens: Identify and retrieve all tokens formatted as
{{TokenName}}from a given string. - Replace Tokens: Dynamically replace tokens in a string with single or multiple values, with optional support for looping through templates using
loopSource.
Class Overview: TokenUtility
Namespace
namespace TokenHandler.Utilities;
Implements
ITokenHandler
The class adheres to the ITokenHandler interface, ensuring flexibility and easy integration.
Methods
1. ExtractToken
Extracts all tokens from a given string.
Signature
public List<string> ExtractToken(string body)
Parameters
body: The input string containing tokens to extract.
Functionality
- Uses a regex pattern
@"\{\{([^\}]+)\}\}"to identify tokens enclosed in{{ }}. - Returns a list of tokens, including the enclosing braces.
Example
string body = "Hello {{FirstName}}, your balance is {{Balance}}.";
TokenUtility utility = new TokenUtility();
List<string> tokens = utility.ExtractToken(body);
// tokens = ["{{FirstName}}", "{{Balance}}"]
2. ReplaceTokens
Replaces tokens in a string with their corresponding values from a dictionary.
Signature
public string ReplaceTokens(string body, Dictionary<string, string> tokenValues, string? loopSource = null)
Parameters
body: The string containing tokens to replace.tokenValues: A dictionary where:- Keys are tokens (e.g.,
{{TokenName}}). - Values are replacements (e.g.,
"John"for{{FirstName}}).
- Keys are tokens (e.g.,
loopSource(optional): A JSON string representing a list of templates for handling tokens with multiple values.
Functionality
Single Value Replacement:
- Replaces tokens with their respective single value from
tokenValues.
- Replaces tokens with their respective single value from
Multiple Value Replacement:
- Splits comma-separated values for tokens.
- Uses the
loopSourceparameter to format replacements dynamically. - Appends the results for all values into a single string.
Return Value
A string with all tokens replaced by their respective values.
Examples
Single Token Replacement
string body = "Welcome, {{UserName}}!";
var tokenValues = new Dictionary<string, string> { { "{{UserName}}", "Alice" } };
TokenUtility utility = new TokenUtility();
string result = utility.ReplaceTokens(body, tokenValues);
// Output: "Welcome, Alice!"
Multiple Token Values
string body = "<table>{{RowTemplate}}</table>";
var tokenValues = new Dictionary<string, string>
{
{ "{{RowTemplate}}", "Row1,Row2,Row3" }
};
string loopSource = "[\"<tr>{{RowTemplate}}</tr>\"]";
TokenUtility utility = new TokenUtility();
string result = utility.ReplaceTokens(body, tokenValues, loopSource);
// Output: "<table><tr>Row1</tr><tr>Row2</tr><tr>Row3</tr></table>"
Implementation Details
Dependencies
System.Text.Json:- Used for deserializing the
loopSourceJSON string.
- Used for deserializing the
System.Text.RegularExpressions:- Used for regex matching of token patterns.
Requirements
- Language: C#
- Framework: .NET Core or later.
Notes
- Tokens must be enclosed in
{{ }}to be recognized by theExtractTokenmethod. - Ensure
loopSourceis a valid JSON-encoded string representing a list of templates for multiple-value replacements. - Handles both single and multiple values seamlessly, with support for dynamic templates.
License
The TokenUtility class is open for use in personal and professional projects. Contributions and enhancements are welcome.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.