diff --git a/lib/src/collection/extension/map_extensions_mixin.dart b/lib/src/collection/extension/map_extensions_mixin.dart index 86299d95..b34a6a5f 100644 --- a/lib/src/collection/extension/map_extensions_mixin.dart +++ b/lib/src/collection/extension/map_extensions_mixin.dart @@ -178,6 +178,57 @@ abstract class KtMapExtensionsMixin entries.forEach((entry) => action(entry.key, entry.value)); } + @override + bool none(Function(K key, V value) predicate) { + assert(() { + if (predicate == null) throw ArgumentError("predicate can't be null"); + return true; + }()); + if (isEmpty()) { + return true; + } + for (KtMapEntry entry in entries.iter) { + if (predicate(entry.key, entry.value)) { + return false; + } + } + return true; + } + + @override + bool all(Function(K key, V value) predicate) { + assert(() { + if (predicate == null) throw ArgumentError("predicate can't be null"); + return true; + }()); + if (isEmpty()) { + return true; + } + for (KtMapEntry entry in entries.iter) { + if (!predicate(entry.key, entry.value)) { + return false; + } + } + return true; + } + + @override + bool any(Function(K key, V value) predicate) { + assert(() { + if (predicate == null) throw ArgumentError("predicate can't be null"); + return true; + }()); + if (isEmpty()) { + return false; + } + for (KtMapEntry entry in entries.iter) { + if (predicate(entry.key, entry.value)) { + return true; + } + } + return false; + } + @override String toString() { return entries.joinToString( diff --git a/lib/src/collection/kt_map.dart b/lib/src/collection/kt_map.dart index d0d3031e..82555c59 100644 --- a/lib/src/collection/kt_map.dart +++ b/lib/src/collection/kt_map.dart @@ -261,4 +261,22 @@ abstract class KtMapExtension { * [action] must not be null. */ void forEach(Function(K key, V value) action); + + /** + * Returns `true` if there is no entries in the map that match the given [predicate]. + * [predicate] must not be null. + */ + bool none(Function(K key, V value) predicate); + + /** + * Returns true if all entries match the given [predicate]. + * [predicate] must not be null. + */ + bool all(Function(K key, V value) predicate); + + /** + * Returns true if there is at least one entry that matches the given [predicate]. + * [predicate] must not be null. + */ + bool any(Function(K key, V value) predicate); } diff --git a/test/collection/map_extensions_test.dart b/test/collection/map_extensions_test.dart index d9d3d3e7..ee7f852b 100644 --- a/test/collection/map_extensions_test.dart +++ b/test/collection/map_extensions_test.dart @@ -494,6 +494,80 @@ void testMap(KtMap Function() emptyMap, expect(e.message, allOf(contains("null"), contains("action"))); }); }); + + group("none", () { + test("none() non-empty map", () { + final map = mapFrom({ + 1: "Bulbasaur", + 2: "Ivysaur", + 3: "Stegosaur", + }); + expect(map.none((key, value) => key == 1 && value == "Bulbasaur"), false); + expect(map.none((_, value) => value == "Random text"), true); + }); + + test("none() empty map", () { + final map = emptyMap(); + expect(map.none((key, value) => key == 1 && value == "Bulbasaur"), true); + expect(map.none((key, value) => false), true); + }); + + test("predicate must be non null", () { + final e = + catchException(() => emptyMap().none(null)); + expect(e.message, allOf(contains("null"), contains("predicate"))); + }); + }); + + group("all", () { + test("all() non-empty map", () { + final map = mapFrom({ + 1: "Bulbasaur", + 2: "Ivysaur", + 3: "Stegosaur", + }); + expect(map.all((key, value) => key > 0 && value.contains("aur")), true); + expect(map.all((key, value) => value == "Bulbasaur"), false); + expect(map.all((key, value) => value.isNotEmpty), true); + }); + + test("all() empty map", () { + final map = emptyMap(); + expect(map.none((key, value) => key == 1 && value == "Bulbasaur"), true); + expect(map.none((key, value) => false), true); + }); + + test("predicate must be non null", () { + final e = + catchException(() => emptyMap().all(null)); + expect(e.message, allOf(contains("null"), contains("predicate"))); + }); + }); + + group("any", () { + test("any() non-empty map", () { + final map = mapFrom({ + 1: "Bulbasaur", + 2: "Ivysaur", + 3: "Stegosaur", + }); + expect(map.any((key, value) => value == "Bulbasaur"), true); + expect(map.any((key, _) => key == -35), false); + }); + + test("any() empty map", () { + final map = emptyMap(); + expect(map.any((key, value) => key == 1 && value == "Bulbasaur"), false); + expect(map.any((key, value) => true), false); + expect(map.any((key, value) => false), false); + }); + + test("predicate must be non null", () { + final e = + catchException(() => emptyMap().any(null)); + expect(e.message, allOf(contains("null"), contains("predicate"))); + }); + }); } class ThirdPartyMap