Skip to content

Choose _ ChooseFormatter

axunonb edited this page Oct 2, 2022 · 11 revisions

The ChooseFormatter searches a value in a list of options, and outputs the corresponding value from a list of outputs.

Syntax Details

{ Any Value : choose(1|2|n) : output 1 | output 2 | output n | default }

Value formatter name (choices) outputs default
Any value "choose" list of choices list of outputs (optional) output if nothing matched
  • There is no limitation for the number of choices and corresponding outputs
  • The value can be anything. It will be converted to string (using ToString), and then matched against the choices. This works great for numbers, booleans, strings, enums, and most simple types.
  • outputs may include nested placeholders
  • Comparing bool and null objects is not case-sensitive

Configuration:

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

char SplitChar: default is '|'

CaseSensitivityType CaseSensitivity for choices: default is SmartSettings.CaseSensitivity, unless overwritten

Examples

Numeric Input

Smart.Format("{0:choose(1|2|3):one|two|three|other}", 1);
// outputs: "one"

Smart.Format("{0:choose(1|2|3):one|two|three|other}", 9);
// outputs: "other"

Smart.Format("{0:choose(4.0|4.1|4.2):dot zero|dot one|dot two|other}", 4.1M);
// outputs: "dot one"

Boolean Input

Smart.Format("{0:choose(True|False):yes|no}", true);
Smart.Format("{0:choose(true|false):yes|no}", true);
// both output: "yes"

Enum Input

enum Gender { Female, Male, Unknown };

Smart.Format("{0:choose(Male|Female):his|her|their}", Gender.Female);
// outputs: "her"

Smart.Format("{0:choose(Male|Female):his|her|their}", Gender.None);
// outputs: "their"

null Input

Smart.Format("{0:choose(null): N/A|{}}", default(object));
Smart.Format("{0:choose(NULL): N/A|{}}", default(object));
// both outputs: "N/A"

string: null or empty or value

Smart.Format("{0:choose(null|):null|empty|{}}", default(string?))) 
// outputs: "null"
Smart.Format("{0:choose(null|):null|empty|{}}", ""))
// outputs: "empty"
Smart.Format("{0:choose(null|):null|empty|{}}", "something else")) 
// "something else"

Nested placeholder in output

Output value if not null
Smart.Format("{0:choose(null): N/A|{}}", 1234);
// outputs: "1234"
Output can use a ListFormatter
var input = null;

var arg = new {
    Input = input,
    Examples = new[] { "good", "bad" }
};

Smart.Format("{Input:choose(null|):Must be a word like {Examples:list:'{}'|, }|'' is not a word|{}}", arg);

// Outputs, depending on input variable:
null   => "Must be a word like 'good', 'bad'"
""     => "'' is not a word"
"good" => "good"
"bad"  => "bad"

Change Configuration

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

String comparisons are case-insensitive.

var chooseFmt = Smart.Default.GetFormatterExtension<ChooseFormatter>()!;
chooseFmt.SplitChar = ',';
chooseFmt.CaseSensitivity = CaseSensitivityType.CaseInsensitive;
Smart.Format("{0:choose(one,two,three):|1|,|2|,|3|,|??|}", "TWO");
// outputs: "|2|"
Clone this wiki locally