Skip to content

Lists _ ListFormatter

axunonb edited this page May 17, 2022 · 8 revisions

The ListFormatter enumerates the content of an IEnumerable, e.g. an Array. The data source must be an IList.

The ListFormatter implements ISource and IFormatter.

Syntax Details

{ IEnumerable : formatter-name : template | spacer | finalSpacer }

IEnumerable formatter name template spacer finalSpacer
Any IEnumerable "list" or implicit required required optional
  • template will be used for each item
  • spacer will be added after each item except the last
  • finalSpacer if supplied, will replace the very final spacer

spacer and finalSpacer may contain Placeholders

Note:

  • The spacers may also in include character literals. So e.g. instead of a comma you could also use \n for listing each item on a new line.
  • spacer and finalSpacer may contain Placeholders

Configuration:

string Name: default is list
The name to use a named formatter

char SplitChar: default is '|'

The character used to split the option text literals

Examples

Simple Array

var items = new[] { "one", "two", "three" };
// Important: You cannot use "items" as an indexed parameter directly,
// as it would be used as params with 3 args.
// So we have to cast
var result = Smart.Format("{0:list:{}|, |, and }", (IList) items);
// Outputs: "one, two, and three"

Nested IEnumerable

var array1 = new[] { "Pepsi", "Coke", "beer", "water" };
var array2 = new[] { "pizza", "hamburger" };
var array3 = new[] { "fries", "chips", "pretzels" };
var array4 = new[] { "carrots", "corn", "green beans" };
var arrayOfArrays = new[] { array1, array2, array3, array4 };

var namedFormatString = "{Food:list:{:list:|, |, and }|;\n|;\n}";
//                       |          |                |        |
//                       |          |__ inner list __|        |
//                       |______________ outer list __________|

Smart.Format(namedFormatString, new {Food = arrayOfArrays});

/* outputs:
Pepsi, Coke, beer, and water;
pizza, and hamburger;
fries, chips, and pretzels;
carrots, corn, and green beans
*/

Nullable IEnumerable

Smart.Format("{TheList?:list:{}|, |, and}", new { TheList = default(object)});
// outputs: ""

Synchronize 2 Lists

var letters = "ABC".ToCharArray();
var words = "One|Two|Three".Split('|');

// works with indexed and named placeholders
Smart.Format("{0:list:{} = {1[Index]}|, }", letters, words);
// outputs: "A = One, B = Two, C = Three"

Output Specific List Element

var data = new { Words = "One|Two|Three".Split('|') };

Smart.Format("Second word: {Words[1]}", data);
// outputs: "Second word: Two"

Change the Option Split Character

Change the split char from '|' to ',' (TAB), so we can use '|' for the output

Smart.Default.GetFormatterExtension<ListFormatter>()!.SplitChar = ',';
var items = new[] { "one", "two", "three" };
Smart.Format("{0:list:{},|,|}", (IList) items);
// outputs: "one|two|three"

Spacers with Placeholders

var args = new {
    Names = new[] { "John", "Mary", "Amy" },
    IsAnd = true, // true or false
    Split = ", "  // comma and space as list separator
};
_ = Smart.Format("{Names:list:{}|{Split}| {IsAnd:and|nor} }", args);
// Output for "IsAnd=true":  "John, Mary and Amy"
// Output for "IsAnd=false": "John, Mary nor Amy"
Clone this wiki locally