kekyo - NamingFormatter 2.0.22

String format library with key-valued replacer.

PM> Install-Package NamingFormatter -Version 2.0.22 -Source https://www.myget.org/F/kekyo/api/v3/index.json

Copy to clipboard

> nuget.exe install NamingFormatter -Version 2.0.22 -Source https://www.myget.org/F/kekyo/api/v3/index.json

Copy to clipboard

> dotnet add package NamingFormatter --version 2.0.22 --source https://www.myget.org/F/kekyo/api/v3/index.json

Copy to clipboard
<PackageReference Include="NamingFormatter" Version="2.0.22" />
Copy to clipboard
source https://www.myget.org/F/kekyo/api/v3/index.json

nuget NamingFormatter  ~> 2.0.22
Copy to clipboard

> choco install NamingFormatter --version 2.0.22 --source https://www.myget.org/F/kekyo/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "kekyo" -SourceLocation "https://www.myget.org/F/kekyo/api/v2"
Install-Module -Name "NamingFormatter" -RequiredVersion "2.0.22" -Repository "kekyo" 
Copy to clipboard

NamingFormatter

NamingFormatter

Status

Project Status: Active – The project has reached a stable, usable state and is being actively developed.

Build NuGet
master NamingFormatter CI build (master) NuGet NamingFormatter (master)
devel NamingFormatter CI build (master) MyGet NamingFormatter (devel)

What is this?

  • NamingFormatter is extended System.String.Format method on .NET.
  • Standard Format method required numbering indexed place-holder.
    • You probably understand this:
// C# String interporation style:
// (These argument variables fixedup at compile time)
var formatted =
    $"Index0:{arg0}, Index1:{arg1}";

// Old school style:
var formatted = string.Format(
    "Index0:{0}, Index1:{1}",
    arg0,
    arg1);
  • NamingFormatter can use named key-value arguments, and will fixup at runtime. For example:
var keyValues = new Dictionary<string, object>
{
    { "lastName", "Matsui" },
    { "firstName", "Kouji" },
    { "foo", "bar" },
};
var formatted = Named.Format(
    "FirstName:{firstName}, LastName:{lastName}",
    keyValues);
  • Of course, we can use the format options.
var keyValues = new Dictionary<string, object>
{
    { "date", DateTime.Now },
    { "value", 123.456 },
    { "foo", "bar" },
};
var formatted = Named.Format(
    "Date:{date:R}, Value:{value:E}",
    keyValues);
  • We can use easier with tuple expression:
var formatted = Named.Format(
    "Date:{date:R}, Value:{value:E}",
    ( "date", DateTime.Now ),
    ( "value", 123.456 ),
    ( "foo", "bar" ));

Features

  • Easy standard replacement from System.String.Format method.
  • TextWriter version included (WriteFormat extension method). And has asynchronous method overloads (Task).
  • Many variation overloads (Dictionary, IReadOnlyDictionary, Predicate delegate, Selector delegate, IFormatProvider, KeyValuePair and ValueTuple with variable length parameters).
  • Can use structual-key, traverse both public properties and fields.
  • Applied C# nullable-reference type attribtues.

Benefits

  • Flexible argument matching. Useful dynamic interpretation.
  • Format string human-readable/customizable improvement.

Environments

  • .NET 8 to 5
  • .NET Standard 1.0, 2.0, 2.1 (Will effect .NET Core 1.0-3.1)
  • .NET Framework 3.5, 4.0 with client profile, 4.5 to 4.8.1

How to use

using NamingFormatter;

// Mostly standard key-value combination in manually.
// We can use with tuples (excepts net35-client and net40-client).
var formatted = Named.Format(
    "Date:{date:R}, Value:{value:E}, Name:{name}",
    ("value", 123.456),
    ("name", "Kouji"),
    ("date", DateTime.Now));
using NamingFormatter;

// All overloads have an optional fallback delegate.
// You can handle unknown key identity when the format string contains it.
// (Default behavior will throw exception)
var formatted = Named.Format(
    "Date:{date}, Value:{VALUE}, Name:{name}",
    key => "***",      // fallback delegate makes safer from exceptions.
    ("value", 123.456),
    ("name", "Kouji"),
    ("date", DateTime.Now));
using NamingFormatter;

// Structual-key (Traverse properties and fields by dot-notation)
var formatted = Named.Format(
    "TOD-Millisec:{date.TimeOfDay.TotalMilliseconds}",
    ("date", DateTime.Now));
using NamingFormatter;

// Format to TextWriter.
var sw = new StreamWriter(stream);
sw.WriteFormat(
    "Date:{date:R}, Value:{value:E}, Name:{name}",
    ("value", 123.456),
    ("name", "Kouji"),
    ("date", DateTime.Now));
sw.Flush();

// Format to TextWriter with async-await
var sw = new StreamWriter(stream);
await sw.WriteFormatAsync(
    "Date:{date:R}, Value:{value:E}, Name:{name}",
    ("value", 123.456),
    ("name", "Kouji"),
    ("date", DateTime.Now));
await sw.FlushAsync();
using NamingFormatter;

// Full-interactive (callback) format.
var formatted = Named.Format(
    "Date:{date:R}, Value:{value:E}, Name:{name}",
    key => key switch
    {
        "name" => "Kouji";
        "date" => DateTime.Now;
        "value" => 123.456;
        _ => throw new FormatException();
    });
using NamingFormatter;

// IFormatProvider supported.
var formatted = Named.Format(
    new CultureInfo("fr-FR"),
    "Date:{date:R}, Value:{value:E}, Name:{name}",
    ("value", 123.456),
    ("name", "Kouji"),
    ("date", DateTime.Now));
using NamingFormatter;

// Easy parametric helper
// (Named.Pair() method will generate KeyValuePair<string, object?>)
var formatted = Named.Format(
    "Date:{date:R}, Value:{value:E}, Name:{name}",
    Named.Pair("value", 123.456),
    Named.Pair("name", "Kouji"),
    Named.Pair("date", DateTime.Now));

TODO

  • F# friendly version.

License

  • Copyright (c) 2016-2020 Kouji Matsui
  • Under Apache v2

History

  • 2.3.0:
    • Added .NET 8.0 RC2 assembly.
  • 2.2.0:
    • Added field lookup ability.
    • Reduced package dependency.
  • 2.1.0:
    • Added .NET 6.0 assembly.
    • Added source link attributes.
  • 2.0.22:
    • Added fallback delegate features.
    • Included xml documents.
  • 2.0.18: Added net461 and net47 assemblies because reduce conflict between netstandard2.0.
  • 2.0.17:
    • Added ValueTuple overloads.
    • Added asynchronous overloads.
  • 2.0.16: Fixed not including net35 assembly.
  • 2.0.15:
    • Breaking change: Changed the NuGet package name from "NamingFormatter" to "NamingFormatter".
    • Breaking change: Changed namespace name from "CenterCLR" to "NamingFormatter".
    • Added some target frameworks.
    • Omitted strong-key signing.
    • Switched and aggregated CI to GitHub Actions.
  • 2.0.0: Upgraded new MSBuild format and omit PCL versions.
  • 1.1.1: Fixed via CI (AppVeyor, Fixed RelaxVersioner)
  • 1.1.0: Add support platform .NET Core (formally "dnxcore").
  • 1.0.0: Omit IFormatProvider method extension attribute.
  • 0.9.6: Versioning fixed.
  • 0.9.5: Add nuget package, Support structual-key, Support .NET 2/.NET 3.5.
  • 0.0.0: Initial commit.

Update readmes.

  • .NETFramework 3.5
  • .NETFramework 4.0 Client
  • .NETFramework 4.5
    • System.ValueTuple (>= 4.5.0)
  • .NETFramework 4.6.1
    • System.ValueTuple (>= 4.5.0)
  • .NETFramework 4.7
    • System.ValueTuple (>= 4.5.0)
  • .NETFramework 4.8
    • System.ValueTuple (>= 4.5.0)
  • .NETStandard 1.0
    • NETStandard.Library (>= 1.6.1)
    • System.ValueTuple (>= 4.5.0)
  • .NETFramework 5.0
    • System.ValueTuple (>= 4.5.0)
  • .NETStandard 2.0
    • System.ValueTuple (>= 4.5.0)
  • .NETStandard 2.1
    • System.ValueTuple (>= 4.5.0)
  • .NETFramework 3.5: 3.5.0.0
  • .NETFramework 4.0 Client: Client: 4.0.0.0
  • .NETFramework 4.5: 4.5.0.0
  • .NETFramework 4.6.1: 4.6.1.0
  • .NETFramework 4.7: 4.7.0.0
  • .NETFramework 4.8: 4.8.0.0
  • .NETFramework 5.0: 5.0.0.0
  • .NETStandard 1.0: 1.0.0.0
  • .NETStandard 2.0: 2.0.0.0
  • .NETStandard 2.1: 2.1.0.0

Owners

Kouji Matsui

Authors

Kouji Matsui (@kozy_kekyo)

Project URL

https://github.com/kekyo/CenterCLR.NamingFormatter

License

Unknown

Tags

string formatter extensible key-value replace same option structural

Info

28 total downloads
28 downloads for version 2.0.22
Download (497.33 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
2.0.22 497.33 KB Wed, 20 Jan 2021 15:18:37 GMT 28