2023-03-03 19:25:33 +00:00
|
|
|
|
/* DictionaryExtension.cs
|
|
|
|
|
* License: NCSA Open Source License
|
|
|
|
|
*
|
|
|
|
|
* Copyright: Merijn Hendriks
|
|
|
|
|
* AUTHORS:
|
|
|
|
|
* waffle.lord
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Aki.Launcher.Extensions
|
|
|
|
|
{
|
|
|
|
|
public static class DictionaryExtensions
|
|
|
|
|
{
|
2023-12-09 22:28:04 +00:00
|
|
|
|
public static TKey GetKeyByValue<TKey, TValue>(this Dictionary<TKey, TValue> dic, TValue value)
|
2023-03-03 19:25:33 +00:00
|
|
|
|
{
|
2023-12-09 22:28:04 +00:00
|
|
|
|
List<TKey> keys = dic.Keys.ToList();
|
2023-03-03 19:25:33 +00:00
|
|
|
|
|
2023-12-09 22:28:04 +00:00
|
|
|
|
for (var x = 0; x < keys.Count; x++)
|
2023-03-03 19:25:33 +00:00
|
|
|
|
{
|
2023-12-09 22:28:04 +00:00
|
|
|
|
if (dic.TryGetValue(keys[x], out var tempValue))
|
2023-03-03 19:25:33 +00:00
|
|
|
|
{
|
|
|
|
|
if (tempValue != null && tempValue.Equals(value))
|
|
|
|
|
{
|
2023-12-09 22:28:04 +00:00
|
|
|
|
return keys[x];
|
2023-03-03 19:25:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return default;
|
|
|
|
|
}
|
2023-12-09 22:28:04 +00:00
|
|
|
|
|
|
|
|
|
public static TKey GetKeyByInput<TKey, TValue>(this Dictionary<TKey, TValue> dic,
|
|
|
|
|
string input)
|
|
|
|
|
{
|
|
|
|
|
var keys = dic.Keys.ToList();
|
|
|
|
|
var values = dic.Values.ToList();
|
|
|
|
|
|
|
|
|
|
for (var x = 0; x < dic.Count; x++)
|
|
|
|
|
{
|
|
|
|
|
if (values[x] is string s && (input.ToLower() == s.ToLower() || input.ToLower().StartsWith(s.ToLower())))
|
|
|
|
|
{
|
|
|
|
|
return keys[x];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return default;
|
|
|
|
|
}
|
2023-03-03 19:25:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|