From fbfa3fd164cc39b7ffdc8df966a862e3e16bdca5 Mon Sep 17 00:00:00 2001 From: gmpassos Date: Wed, 10 Mar 2021 20:27:16 -0300 Subject: [PATCH] v3.0.4 - `collections`: improve Null Safety usage. --- CHANGELOG.md | 4 ++++ lib/src/collections.dart | 33 +++++++++++++++++---------------- pubspec.yaml | 4 ++-- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b72629..ebb4078 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 3.0.4 + +- `collections`: improve Null Safety usage. + ## 3.0.3 - Null safety migration adjustments. diff --git a/lib/src/collections.dart b/lib/src/collections.dart index ff1040c..b8a9580 100644 --- a/lib/src/collections.dart +++ b/lib/src/collections.dart @@ -582,7 +582,7 @@ Map? copyMapString(Map? m) { } /// Gets a [map] entry ignoring key case. -MapEntry? getEntryIgnoreCase(Map map, String? key) { +MapEntry? getEntryIgnoreCase(Map map, String? key) { if (key == null) return null; var val = map[key]; if (val != null) return MapEntry(key, val); @@ -591,8 +591,8 @@ MapEntry? getEntryIgnoreCase(Map map, String? key) { for (var k in map.keys) { if (k.toLowerCase() == keyLC) { - V value = map[k]!; - return MapEntry(k, value); + var value = map[k]; + return MapEntry(k, value); } } @@ -659,7 +659,7 @@ typedef CompareMapEntryFunction = int Function( MapEntry entry1, MapEntry entry2); /// Returns a Map sorted by keys. -Map sortMapEntriesByKey(Map map, [bool reversed = false]) => +Map sortMapEntriesByKey(Map map, [bool reversed = false]) => sortMapEntries( map, (a, b) => parseComparable(a.key)!.compareTo(b.key), reversed); @@ -1059,31 +1059,31 @@ Set toNonNullSet(Set? set, {bool forceTypeCast = true}) { /// Finds in [map] a entry that has one of [keys]. /// /// [ignoreCase] If [true] ignores the case of the keys. -MapEntry? findKeyEntry(Map? map, List? keys, +MapEntry? findKeyEntry(Map? map, List? keys, [bool ignoreCase = false]) { - if (map == null || keys == null) return null; + if (map == null || keys == null || map.isEmpty || keys.isEmpty) return null; if (ignoreCase) { for (var key in keys) { if (map.containsKey(key)) { - V value = map[key]!; - return MapEntry(key, value); + var value = map[key]; + return MapEntry(key, value); } var keyLC = key.toString().toLowerCase(); - for (var k in map.keys) { + for (var k in map.keys.whereType()) { if (k.toString().toLowerCase() == keyLC) { - V value = map[k]!; - return MapEntry(k, value); + var value = map[k]; + return MapEntry(k, value); } } } } else { for (var key in keys) { if (map.containsKey(key)) { - V value = map[key]!; - return MapEntry(key, value); + var value = map[key]; + return MapEntry(key, value); } } } @@ -1094,7 +1094,7 @@ MapEntry? findKeyEntry(Map? map, List? keys, /// Finds in [map] a value that has one of [keys]. /// /// [ignoreCase] If [true] ignores the case of the keys. -V? findKeyValue(Map? map, List? keys, +V? findKeyValue(Map? map, List? keys, [bool ignoreCase = false]) { var entry = findKeyEntry(map, keys, ignoreCase); return entry != null ? entry.value : null; @@ -1147,7 +1147,8 @@ V? findKeyPathValue(Map? map, String? keyPath, /// Finds in [map] a key that has one of [keys]. /// /// [ignoreCase] If [true] ignores the case of the keys. -K? findKeyName(Map? map, List? keys, [bool ignoreCase = false]) { +K? findKeyName(Map? map, List? keys, + [bool ignoreCase = false]) { var entry = findKeyEntry(map, keys, ignoreCase); return entry != null ? entry.key : null; } @@ -2171,7 +2172,7 @@ class NNField { final bool deepHashcode; /// Optional value filter to apply before set. - final T Function(Object? value)? filter; + final T Function(dynamic value)? filter; /// Optional value to apply before get. final T Function(T value)? resolver; diff --git a/pubspec.yaml b/pubspec.yaml index bacd2ef..0ad8ab6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: swiss_knife description: Dart Useful Tools - collections, math, date, uri, json, events, resources, regexp, etc... -version: 3.0.3 +version: 3.0.4 homepage: https://github.com/gmpassos/swiss_knife environment: @@ -13,7 +13,7 @@ dependencies: dev_dependencies: pedantic: ^1.11.0 - test: ^1.16.5 + test: ^1.16.7 #test_coverage: ^0.4.3 #dependency_overrides: