using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
namespace EmbedIO.Utilities
{
///
/// Provides extension methods for .
///
public static class NameValueCollectionExtensions
{
///
/// Converts a to a dictionary of objects.
/// Values in the returned dictionary will wither be strings, or arrays of strings,
/// depending on the presence of multiple values for the same key in the collection.
///
/// The on which this method is called.
/// A associating the collection's keys
/// with their values.
/// is .
public static Dictionary ToDictionary(this NameValueCollection @this)
=> @this.Keys.Cast().ToDictionary(key => key, key => {
var values = @this.GetValues(key);
if (values == null)
return null;
return values.Length switch {
0 => null,
1 => (object) values[0],
_ => (object) values
};
});
///
/// Converts a to a dictionary of strings.
///
/// The on which this method is called.
/// A associating the collection's keys
/// with their values (or comma-separated lists in case of multiple values).
/// is .
public static Dictionary ToStringDictionary(this NameValueCollection @this)
=> @this.Keys.Cast().ToDictionary(key => key, @this.Get);
///
/// Converts a to a dictionary of arrays of strings.
///
/// The on which this method is called.
/// A associating the collection's keys
/// with arrays of their values.
/// is .
public static Dictionary ToArrayDictionary(this NameValueCollection @this)
=> @this.Keys.Cast().ToDictionary(key => key, @this.GetValues);
///
/// Determines whether a contains one or more values
/// for the specified .
///
/// The on which this method is called.
/// The key to look for.
/// if at least one value for
/// is present in the collection; otherwise, .
///
/// is .
public static bool ContainsKey(this NameValueCollection @this, string key)
=> @this.Keys.Cast().Contains(key);
///
/// Determines whether a contains one or more values
/// for the specified , at least one of which is equal to the specified
/// . Value comparisons are carried out using the
/// comparison type.
///
/// The on which this method is called.
/// The name to look for.
/// The value to look for.
/// if at least one of the values for
/// in the collection is equal to ; otherwise, .
///
/// is .
/// White space is trimmed from the start and end of each value before comparison.
///
public static bool Contains(this NameValueCollection @this, string name, string value)
=> Contains(@this, name, value, StringComparison.OrdinalIgnoreCase);
///
/// Determines whether a contains one or more values
/// for the specified , at least one of which is equal to the specified
/// . Value comparisons are carried out using the specified
/// .
///
/// The on which this method is called.
/// The name to look for.
/// The value to look for.
/// One of the enumeration values
/// that specifies how the strings will be compared.
/// if at least one of the values for
/// in the collection is equal to ; otherwise, .
///
/// is .
/// White space is trimmed from the start and end of each value before comparison.
///
public static bool Contains(this NameValueCollection @this, string name, string? value, StringComparison comparisonType)
{
value = value?.Trim();
return @this[name]?.SplitByComma()
.Any(val => string.Equals(val?.Trim(), value, comparisonType)) ?? false;
}
}
}