-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathSelectorHelpers.cs
44 lines (39 loc) · 1.24 KB
/
SelectorHelpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
namespace Havit.Blazor.Components.Web;
/// <summary>
/// Helper methods for selectors.
/// </summary>
public static class SelectorHelpers
{
/// <summary>
/// Returns text from an item based on the textSelector.
/// When the textSelector is <c>null</c>, it returns item.ToString().
/// It never returns <c>null</c>. Instead, <c>null</c> values are converted to an empty string.
/// </summary>
public static string GetText<TItem>(Func<TItem, string> textSelector, TItem item)
{
return (textSelector != null)
? textSelector.Invoke(item) ?? String.Empty
: item?.ToString() ?? String.Empty;
}
/// <summary>
/// When the item is <c>null</c>, it returns <c>default(TValue)</c>.
/// Otherwise, it returns the value from the item based on the valueSelector.
/// When the valueSelector is <c>null</c> and <c>TValue</c> is the same as <c>TItem</c>, it returns the item.
/// </summary>
public static TValue GetValue<TItem, TValue>(Func<TItem, TValue> valueSelector, TItem item)
{
if (item == null)
{
return default;
}
if (valueSelector != null)
{
return valueSelector(item);
}
if (typeof(TValue) == typeof(TItem))
{
return (TValue)(object)item;
}
throw new InvalidOperationException("ValueSelector is required.");
}
}