Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

* CaseInsensitiveMap: added constructor fromEntries. #99

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
## 4.0.3-wip

* `CaseInsensitiveMap`: added constructor `fromEntries`.

* Require Dart 3.4

* collection: ^1.19.0

## 4.0.2

* Remove `package:charcode` from dev_dependencies.
Expand Down
14 changes: 12 additions & 2 deletions lib/src/case_insensitive_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ import 'package:collection/collection.dart';
///
/// Much of HTTP is case-insensitive, so this is useful to have pre-defined.
class CaseInsensitiveMap<V> extends CanonicalizedMap<String, String, V> {
CaseInsensitiveMap() : super((key) => key.toLowerCase());
/// Creates an empty case-insensitive map.
CaseInsensitiveMap() : super(_canonicalizer);

/// Creates a case-insensitive map that is initialized with the key/value
/// pairs of [other].
CaseInsensitiveMap.from(Map<String, V> other)
: super.from(other, (key) => key.toLowerCase());
: super.from(other, _canonicalizer);

/// Creates a case-insensitive map that is initialized with the key/value
/// pairs of [entries].
CaseInsensitiveMap.fromEntries(Iterable<MapEntry<String, V>> entries)
: super.fromEntries(entries, _canonicalizer);

static String _canonicalizer(String key) => key.toLowerCase();
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ environment:
sdk: ^3.4.0

dependencies:
collection: ^1.15.0
collection: ^1.19.0
source_span: ^1.8.0
string_scanner: ^1.1.0
typed_data: ^1.3.0
Expand Down
6 changes: 6 additions & 0 deletions test/case_insensitive_map_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ void main() {
expect(map, containsPair('FoO', 'bAr'));
expect(map, equals({'fOo': 'bAr'}));
});

test('.fromEntries() converts an existing map', () {
final map = CaseInsensitiveMap.fromEntries({'fOo': 'bAr'}.entries);
expect(map, containsPair('FoO', 'bAr'));
expect(map, equals({'fOo': 'bAr'}));
});
}