tags: - options - parsing - select-lists
Options & Select Lists¶
EverSharp provides specialized functions for parsing structured option strings commonly used in select lists, dropdowns, and configuration data. These functions parse strings in the format "Name##Value##InfoText" where ## acts as the delimiter.
Understanding the Format¶
The option string format has three parts separated by double hash (##):
- Name (first part): What the user sees (display text)
- Value (second part): The actual value stored/used in code
- InfoText (third part, optional): Additional help text or description
Quick Reference¶
| Function | Returns | Example Input | Example Output |
|---|---|---|---|
GetOptionName() |
Display text (first part) | "Hello##World" |
"Hello" |
GetOptionValue() |
Actual value (second part) | "Hello##World" |
"World" |
GetOptionInfoText() |
Help text (third part) | "Hello##World##Info" |
"Info" |
GetOptionName()¶
Extracts the display name (first part) from an option string.
Syntax¶
Parameters¶
- string: The option string in
"Name##Value"or"Name##Value##InfoText"format
Returns¶
The display name portion (first part before ##).
Example: Basic Usage¶
Option = "United Kingdom##GB##Country in Europe";
DisplayText = Option.GetOptionName();
// DisplayText = "United Kingdom"
Example: From Test Suite¶
When to Use¶
Use GetOptionName() when you need to:
- Display human-readable text to users
- Show the label for a selected option
- Generate UI dropdown labels
GetOptionValue()¶
Extracts the actual value (second part) from an option string.
Syntax¶
Parameters¶
- string: The option string in
"Name##Value"or"Name##Value##InfoText"format
Returns¶
The value portion (second part between ## delimiters).
Example: Basic Usage¶
Option = "United Kingdom##GB##Country in Europe";
CountryCode = Option.GetOptionValue();
// CountryCode = "GB"
Example: From Test Suite¶
When to Use¶
Use GetOptionValue() when you need to:
- Get the actual value to store in a database
- Use the programmatic identifier for an option
- Compare against expected values in business logic
GetOptionInfoText()¶
Extracts the help text (third part) from an option string.
Syntax¶
Parameters¶
- string: The option string in
"Name##Value##InfoText"format
Returns¶
The info text portion (third part after second ##).
Example: Basic Usage¶
Option = "United Kingdom##GB##Country in Europe";
HelpText = Option.GetOptionInfoText();
// HelpText = "Country in Europe"
Example: From Test Suite¶
Option = "Hello##World##This is a standard way of testing an algorithm";
Result = Option.GetOptionInfoText();
// Result = "This is a standard way of testing an algorithm"
When to Use¶
Use GetOptionInfoText() when you need to:
- Display tooltip text
- Show additional context to users
- Provide help documentation for options
Distinguishing Between Name and Value¶
The key difference is purpose:
| Aspect | GetOptionName() | GetOptionValue() |
|---|---|---|
| Purpose | User-facing display | Machine-readable identifier |
| Example | "United Kingdom" | "GB" |
| Usage | Show in dropdown, labels | Store in database, logic |
| Audience | End users | Developers/systems |
Real-World Example¶
// Option from a country selection dropdown
SelectedOption = "United Kingdom##GB##Country in Europe";
// For display in UI
CountryLabel = SelectedOption.GetOptionName();
// CountryLabel = "United Kingdom"
// For storing in database
CountryCode = SelectedOption.GetOptionValue();
// CountryCode = "GB"
// For tooltip/help text
CountryInfo = SelectedOption.GetOptionInfoText();
// CountryInfo = "Country in Europe"
Error Handling¶
All three functions handle edge cases gracefully:
Invalid Format (Single Hash)¶
Option = "Hello#World#Invalid";
ResultName = Option.GetOptionName();
ResultValue = Option.GetOptionValue();
ResultInfo = Option.GetOptionInfoText();
// All return the original string unchanged
// ResultName = "Hello#World#Invalid"
// ResultValue = "Hello#World#Invalid"
// ResultInfo = "Hello#World#Invalid"
Null or Undefined Values¶
// Option is undefined
ResultName = Option.GetOptionName();
ResultValue = Option.GetOptionValue();
ResultInfo = Option.GetOptionInfoText();
// All return empty string
// ResultName = ""
// ResultValue = ""
// ResultInfo = ""
Common Patterns¶
Building Options Dictionaries¶
Use ReduceToObj() to convert option arrays into dictionaries:
Options = [
"United Kingdom##GB",
"United States##US",
"Germany##DE"
];
// Create a dictionary mapping display names to values
OptionsDict = Options.ReduceToObj((acc, x) =>
acc.AddProperty(x.GetOptionName(), x.GetOptionValue())
);
// OptionsDict = {
// "United Kingdom": "GB",
// "United States": "US",
// "Germany": "DE"
// }
Reverse Lookup by Value¶
Options = [
"United Kingdom##GB##Country in Europe",
"United States##US##Country in North America"
];
SelectedValue = "GB";
// Find the option matching the value
SelectedOption = Options.Find(x => x.GetOptionValue() == SelectedValue);
DisplayName = SelectedOption.GetOptionName();
// DisplayName = "United Kingdom"
Filtering Options by Criteria¶
Options = [
"Premium Plan##premium##$99/month",
"Basic Plan##basic##$9/month",
"Enterprise Plan##enterprise##$299/month"
];
// Filter to plans with "Plan" in the name
FilteredPlans = Options.Filter(x =>
x.GetOptionName().Contains("Plan")
);
Generating UI Select Options¶
Options = [
"Manager##mgr##Can approve requests",
"Developer##dev##Can deploy code",
"Viewer##view##Read-only access"
];
// Build HTML-like option elements
SelectOptions = Options.Map(x => {
return {
"label": x.GetOptionName(),
"value": x.GetOptionValue(),
"title": x.GetOptionInfoText()
};
});
// SelectOptions = [
// { "label": "Manager", "value": "mgr", "title": "Can approve requests" },
// { "label": "Developer", "value": "dev", "title": "Can deploy code" },
// { "label": "Viewer", "value": "view", "title": "Read-only access" }
// ]
Best Practices¶
1. Consistent Delimiter Usage¶
Always use double hash (##) as the delimiter:
2. Sanitize User Input¶
If options come from user input, validate the format:
function IsValidOption(optionString) {
parts = optionString.Split("##");
return parts.Length >= 2; // At minimum, Name##Value
}
3. Default Values for Missing Parts¶
Provide fallbacks when InfoText is optional:
Option = "Name##Value"; // No InfoText
InfoText = Option.GetOptionInfoText();
DisplayInfo = InfoText == "" ? "No additional information" : InfoText;
4. Cache Parsed Values¶
If parsing the same option repeatedly, cache the results:
// Instead of calling GetOptionValue() multiple times
SelectedOption = "UK##GB##Europe";
selectedValue = SelectedOption.GetOptionValue(); // Parse once
if (selectedValue == "GB") { /* ... */ }
if (selectedValue == "US") { /* ... */ }
Comparison with Other Approaches¶
vs. Split()¶
// Manual approach
Option = "Hello##World##Info";
parts = Option.Split("##");
name = parts[0];
value = parts[1];
info = parts[2];
// Option functions (cleaner)
name = Option.GetOptionName();
value = Option.GetOptionValue();
info = Option.GetOptionInfoText();
Advantages of option functions:
- More readable and self-documenting
- Built-in null/error handling
- No array index management
- Returns original string if format invalid
Related Functions¶
- Split(): Low-level string splitting for custom delimiters
- ReduceToObj(): Build dictionaries from option arrays
- Filter(): Find options matching criteria
- Map(): Transform option arrays into other formats