From 6c3abb75b451042e30adc0c970add74bc9b12585 Mon Sep 17 00:00:00 2001 From: gmpassos Date: Thu, 13 Jun 2024 22:00:35 -0300 Subject: [PATCH] * `CaseInsensitiveMap`: added constructor `fromEntries`. * collection: ^1.19.0 --- CHANGELOG.md | 4 ++++ lib/src/case_insensitive_map.dart | 14 ++++++++++++-- pubspec.yaml | 2 +- test/case_insensitive_map_test.dart | 6 ++++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07f4bf5..5be8452 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/src/case_insensitive_map.dart b/lib/src/case_insensitive_map.dart index 88a190e..ed344e6 100644 --- a/lib/src/case_insensitive_map.dart +++ b/lib/src/case_insensitive_map.dart @@ -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 extends CanonicalizedMap { - 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 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> entries) + : super.fromEntries(entries, _canonicalizer); + + static String _canonicalizer(String key) => key.toLowerCase(); } diff --git a/pubspec.yaml b/pubspec.yaml index a6d6452..ef5baea 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/case_insensitive_map_test.dart b/test/case_insensitive_map_test.dart index f62d4fe..7c65850 100644 --- a/test/case_insensitive_map_test.dart +++ b/test/case_insensitive_map_test.dart @@ -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'})); + }); }