2022-05-25 14:32:15 -07:00
|
|
|
import 'dart:collection';
|
|
|
|
|
|
|
|
/// A map from strings to [T] where keys are compared without case sensitivity.
|
|
|
|
class CaseInsensitiveMap<K extends String?, T> extends MapBase<K, T> {
|
|
|
|
final Map<K, T> _normalized = {};
|
|
|
|
|
2023-09-14 06:37:21 -07:00
|
|
|
CaseInsensitiveMap();
|
|
|
|
|
|
|
|
factory CaseInsensitiveMap.of(Map<K, T> other) {
|
|
|
|
final map = CaseInsensitiveMap<K, T>();
|
|
|
|
other.forEach((key, value) {
|
|
|
|
map[key] = value;
|
|
|
|
});
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2022-05-25 14:32:15 -07:00
|
|
|
@override
|
|
|
|
T? operator [](Object? key) {
|
|
|
|
if (key is String?) {
|
|
|
|
return _normalized[key?.toLowerCase()];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void operator []=(K key, T value) {
|
|
|
|
_normalized[key?.toLowerCase() as K] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void clear() {
|
|
|
|
_normalized.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Iterable<K> get keys => _normalized.keys;
|
|
|
|
|
|
|
|
@override
|
|
|
|
T? remove(Object? key) {
|
|
|
|
if (key is String?) {
|
|
|
|
return _normalized.remove(key?.toLowerCase());
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|