TokenHandler 1.0.1

dotnet add package TokenHandler --version 1.0.1
                    
NuGet\Install-Package TokenHandler -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TokenHandler" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TokenHandler" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="TokenHandler" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TokenHandler --version 1.0.1
                    
#r "nuget: TokenHandler, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TokenHandler@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TokenHandler&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=TokenHandler&version=1.0.1
                    
Install as a Cake Tool

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

  1. Extract Tokens: Identify and retrieve all tokens formatted as {{TokenName}} from a given string.
  2. 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}}).
  • loopSource (optional): A JSON string representing a list of templates for handling tokens with multiple values.
Functionality
  1. Single Value Replacement:

    • Replaces tokens with their respective single value from tokenValues.
  2. Multiple Value Replacement:

    • Splits comma-separated values for tokens.
    • Uses the loopSource parameter 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 loopSource JSON string.
  • 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 the ExtractToken method.
  • Ensure loopSource is 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last Updated
1.0.1 182 11/25/2024
1.0.0 167 6/2/2024