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

Commit

Permalink
* CaseInsensitiveMap: added constructor fromEntries.
Browse files Browse the repository at this point in the history
* collection: ^1.19.0
  • Loading branch information
gmpassos committed Jun 14, 2024
1 parent 53d4041 commit 6c3abb7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
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'}));
});
}

0 comments on commit 6c3abb7

Please sign in to comment.