Skip to content

Condition _ ConditionalFormatter

axunonb edited this page Mar 30, 2022 · 2 revisions

Description

The ConditionalFormatter has a lot of functionality for shaping the output string based on conditions.

The behavior of ConditionalFormatter varies depending on the data type of the placeholder.

Syntax Details

{ Any Value : cond : output 1 | output 2 | output n | default }

Value formatter name outputs default
Any value "cond" list of choices (optional) output if nothing matched

The number of outputs including default must always have at least 2 choices.

Configuration:

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

char SplitChar: default is '|'

Data Type: Number, Simple Conditions

Number: can be short, ushort, int, uint, long, ulong, float, double, decimal

The value of the number determins the index in the list choices. If the value is not an integer, the largest integer less than or equal to the specified value is used.

Negative values, or values exceeding the number of choices will output the default.

Examples
Smart.Format("{0:cond:Apple|Pie|Orange|Banana|No fruit}", arg);
/* Outputs:
arg == 0:   "Apple"
arg == 3:   "Banana"
arg < 0 || arg > 3: "No fruit"
*/

Data Type: Number, Complex Conditions

Output options:

cond1 ? output cond1 | cond-n ? output cond-n

Each option is separated by the SplitChar. The comparison is followed by a "?" and then the text for a match. The last (default) entry neither contains a comparison, nor a "?". Valid comparisons: >= > = == < <= !=.

Comparisons can be combined using "&" for AND or "/" for OR.

Example:

Cluster output by age groups
var age = 32;
var arg = new KeyValuePair<string, object>("Age", age);
Smart.Format("{Age:cond:<=0?Not yet born?|>=55?Senior Citizen|>=30?Adult|>=18?Young Adult|>12?Teenager|>2?Child|Baby}", arg);
/* Outputs, depending on age:
32: "Adult"
70: "Senior Citizen"
 2: "Baby"
 0: "Not yet born?"
*/

Data Type: bool

true uses the first, false uses the second item of the output choices.

Examples
Smart.Format("{0:cond:Sun|Moon}", arg);
/* Outputs:
arg == true:   "Sun"
arg == false:  "Moon"
*/

Data Type: string

Outputs the value, if it's not null or string.Empty, else the default choice.

Examples
Smart.Format("{0:cond:{}|Null or Empty}", arg);
/* Outputs:
arg == "smart": "smart"
arg == null:    "Null or Empty"
arg == "":      "Null or Empty"
*/

Data Types: DateTime, DateTimeOffset

Compares the value to the current calender date (year, month, day).

With 3 output choices, the index is 0 for past, 1 for present, 2 for future. With 2 output choices, the index is 0 for present or past, 1 for future.

Examples
Smart.Format("{0:cond:yesterday|today|tomorrow}", arg);
/* Outputs:
arg == DateTime.Now.AddDays(-1): "yesterday"
arg == DateTime.Now:             "today"
arg == DateTime.Now.AddDays(-1): "tomorrow"
*/

Data Types: TimeSpan

Compares the value to TimeSpan.Zero.

With 3 output choices, the index is 0 for negative, 1 for zero, 2 for positive. With 2 output choices, the index is 0 for negative or zero, 1 for positive.

Examples
Smart.Format("{0:cond:{Hours} hours ago|now|{Hours} hours later}", arg);
/* Outputs:
arg == TimeSpan.Zero.Add(new TimeSpan(-2,0,0)): "2 hours ago"
arg == TimeSpan.Zero:                           "now"
arg == TimeSpan.Zero.Add(new TimeSpan(3,0,0)):  "3 hours later"
*/
Clone this wiki locally