aboutsummaryrefslogtreecommitdiff
path: root/src/Jellyfin.Extensions/DictionaryExtensions.cs
blob: 5bb828d016aad9f4ee8251de453355c408c662e8 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Collections.Generic;

namespace Jellyfin.Extensions
{
    /// <summary>
    /// Static extensions for the <see cref="IReadOnlyDictionary{TKey,TValue}"/> interface.
    /// </summary>
    public static class DictionaryExtensions
    {
        /// <summary>
        /// Gets a string from a string dictionary, checking all keys sequentially,
        /// stopping at the first key that returns a result that's neither null nor blank.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="key1">The first checked key.</param>
        /// <returns>System.String.</returns>
        public static string? GetFirstNotNullNorWhiteSpaceValue(this IReadOnlyDictionary<string, string> dictionary, string key1)
        {
            return dictionary.GetFirstNotNullNorWhiteSpaceValue(key1, string.Empty, string.Empty);
        }

        /// <summary>
        /// Gets a string from a string dictionary, checking all keys sequentially,
        /// stopping at the first key that returns a result that's neither null nor blank.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="key1">The first checked key.</param>
        /// <param name="key2">The second checked key.</param>
        /// <returns>System.String.</returns>
        public static string? GetFirstNotNullNorWhiteSpaceValue(this IReadOnlyDictionary<string, string> dictionary, string key1, string key2)
        {
            return dictionary.GetFirstNotNullNorWhiteSpaceValue(key1, key2, string.Empty);
        }

        /// <summary>
        /// Gets a string from a string dictionary, checking all keys sequentially,
        /// stopping at the first key that returns a result that's neither null nor blank.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="key1">The first checked key.</param>
        /// <param name="key2">The second checked key.</param>
        /// <param name="key3">The third checked key.</param>
        /// <returns>System.String.</returns>
        public static string? GetFirstNotNullNorWhiteSpaceValue(this IReadOnlyDictionary<string, string> dictionary, string key1, string key2, string key3)
        {
            if (dictionary.TryGetValue(key1, out var val) && !string.IsNullOrWhiteSpace(val))
            {
                return val;
            }

            if (!string.IsNullOrEmpty(key2) && dictionary.TryGetValue(key2, out val) && !string.IsNullOrWhiteSpace(val))
            {
                return val;
            }

            if (!string.IsNullOrEmpty(key3) && dictionary.TryGetValue(key3, out val) && !string.IsNullOrWhiteSpace(val))
            {
                return val;
            }

            return null;
        }
    }
}