| | 1 | | using System.Collections.Generic; |
| | 2 | |
|
| | 3 | | namespace Jellyfin.Extensions |
| | 4 | | { |
| | 5 | | /// <summary> |
| | 6 | | /// Static extensions for the <see cref="IReadOnlyDictionary{TKey,TValue}"/> interface. |
| | 7 | | /// </summary> |
| | 8 | | public static class DictionaryExtensions |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Gets a string from a string dictionary, checking all keys sequentially, |
| | 12 | | /// stopping at the first key that returns a result that's neither null nor blank. |
| | 13 | | /// </summary> |
| | 14 | | /// <param name="dictionary">The dictionary.</param> |
| | 15 | | /// <param name="key1">The first checked key.</param> |
| | 16 | | /// <returns>System.String.</returns> |
| | 17 | | public static string? GetFirstNotNullNorWhiteSpaceValue(this IReadOnlyDictionary<string, string> dictionary, str |
| | 18 | | { |
| 27 | 19 | | return dictionary.GetFirstNotNullNorWhiteSpaceValue(key1, string.Empty, string.Empty); |
| | 20 | | } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Gets a string from a string dictionary, checking all keys sequentially, |
| | 24 | | /// stopping at the first key that returns a result that's neither null nor blank. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="dictionary">The dictionary.</param> |
| | 27 | | /// <param name="key1">The first checked key.</param> |
| | 28 | | /// <param name="key2">The second checked key.</param> |
| | 29 | | /// <returns>System.String.</returns> |
| | 30 | | public static string? GetFirstNotNullNorWhiteSpaceValue(this IReadOnlyDictionary<string, string> dictionary, str |
| | 31 | | { |
| 11 | 32 | | return dictionary.GetFirstNotNullNorWhiteSpaceValue(key1, key2, string.Empty); |
| | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Gets a string from a string dictionary, checking all keys sequentially, |
| | 37 | | /// stopping at the first key that returns a result that's neither null nor blank. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="dictionary">The dictionary.</param> |
| | 40 | | /// <param name="key1">The first checked key.</param> |
| | 41 | | /// <param name="key2">The second checked key.</param> |
| | 42 | | /// <param name="key3">The third checked key.</param> |
| | 43 | | /// <returns>System.String.</returns> |
| | 44 | | public static string? GetFirstNotNullNorWhiteSpaceValue(this IReadOnlyDictionary<string, string> dictionary, str |
| | 45 | | { |
| 62 | 46 | | if (dictionary.TryGetValue(key1, out var val) && !string.IsNullOrWhiteSpace(val)) |
| | 47 | | { |
| 6 | 48 | | return val; |
| | 49 | | } |
| | 50 | |
|
| 56 | 51 | | if (!string.IsNullOrEmpty(key2) && dictionary.TryGetValue(key2, out val) && !string.IsNullOrWhiteSpace(val)) |
| | 52 | | { |
| 2 | 53 | | return val; |
| | 54 | | } |
| | 55 | |
|
| 54 | 56 | | if (!string.IsNullOrEmpty(key3) && dictionary.TryGetValue(key3, out val) && !string.IsNullOrWhiteSpace(val)) |
| | 57 | | { |
| 3 | 58 | | return val; |
| | 59 | | } |
| | 60 | |
|
| 51 | 61 | | return null; |
| | 62 | | } |
| | 63 | | } |
| | 64 | | } |