The set of common table member converters for the YetAnotherConsoleTables library.
Converters
- Root namespace
- FormattableConverter
- Chain namespace
- StringReplace namespace
- ValueChange namespace
- Enumerable namespace
Append a suffix to member values. Any object of attribute parameter types can be used as the suffix.
class Box
{
[TableMemberConverter<AppendValueConverter>(ConstructorArgs = new[] { " kg" })]
public double Weight { get; set; } = 100;
}
----------
| Weight |
----------
| 100 kg |
----------
Prepend a prefix to member values. Any object of attribute parameter types can be used as the prefix.
class Box
{
[TableMemberConverter<PrependValueConverter>(ConstructorArgs = new[] { "UAH: " })]
public double Price { get; set; } = 1000;
}
-------------
| Price |
-------------
| UAH: 1000 |
-------------
Prepend a prefix and append a suffix to member values at the same time. You can achieve the same behavior with a ChainedConverter, but this one will run faster. Any objects of attribute parameter types can be used as the prefix and the suffix.
class Box
{
[TableMemberConverter<WrapValueConverter>(ConstructorArgs = new[] { "UAH ", " per item" })]
public int DiscountPerItem { get; set; } = 5;
}
-------------------
| DiscountPerItem |
-------------------
| UAH 5 per item |
-------------------
Convert an object that implements IFormattable
using the ToString()
method of this interface.
class Box
{
[TableMemberConverter<FormattableConverter>(ConstructorArgs = new[] { "P2" })]
public double Discount { get; set; } = 0.05;
[TableMemberConverter<FormattableConverter>(ConstructorArgs = new[] { "C2", "en-US" })]
public double Price { get; set; } = 900;
}
----------------------
| Discount | Price |
----------------------
| 5.00 % | $900.00 |
----------------------
Find all occurrences of a specified string and replace them with another specified string.
class Box
{
[TableMemberConverter<StringReplaceConverter>(ConstructorArgs = new[] { "<br/>", "\r\n" })]
public string HtmlDescription { get; set; } = "Description<br/>Description";
}
-------------------
| HtmlDescription |
-------------------
| Description |
| Description |
-------------------
Replace all strings that match a specified pattern with a specified replacement string.
class User
{
[TableMemberConverter<RegexReplaceConverter>(ConstructorArgs = new[] { ".", "*" })]
public string Password { get; set; } = "qwerty";
}
------------
| Password |
------------
| ****** |
------------
Combine two or more converters in a chain. Out of the box, converters are available that combine 2 to 5 other converters. If you need to combine more than 5 converters, the ChainedConverter
can be nested in another.
class Box
{
[TableMemberConverter<ChainedConverter<FormattableConverter, AppendValueConverter>>(ConstructorArgs = new object[] { new[] { "C" }, new[] { " per item" }})]
public int Discount { get; set; } = 5;
}
------------------
| Discount |
------------------
| ¤5.00 per item |
------------------
Use the first element of the IEnumerable
. The first element can be retrieved using FirstOrDefault
or First
LINQ method.
class Student
{
[TableMember(DisplayName = "First Mark")]
[TableMemberConverter<EnumerableFirstConverter<string>>]
public string[] Marks { get; set; } = new[] { "A", "A", "F" };
}
--------------
| First Mark |
--------------
| A |
--------------
Use the last element of the IEnumerable
. The last element can be retrieved using LastOrDefault
or Last
LINQ method.
class Student
{
[TableMember(DisplayName = "Last Mark")]
[TableMemberConverter<EnumerableLastConverter<string>>]
public string[] Marks { get; set; } = new[] { "A", "A", "F" };
}
------------
| Last Mark |
-------------
| F |
-------------
Join all elements of the IEnumerable
using the passed separator.
class Student
{
[TableMemberConverter<EnumerableJoinConverter<string>>(ConstructorArgs = new object[] { ", " })]
public string[] Marks { get; set; } = new[] { "A", "A", "F" };
}
-----------
| Marks |
-----------
| A, A, F |
-----------