Skip to content

Commit

Permalink
Merge pull request #80 from acherkashyn/map_none_all_any
Browse files Browse the repository at this point in the history
Add KtMap methods none(), all() and any()
  • Loading branch information
passsy authored Feb 20, 2019
2 parents 05264f6 + 90e284d commit 328b919
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
51 changes: 51 additions & 0 deletions lib/src/collection/extension/map_extensions_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,57 @@ abstract class KtMapExtensionsMixin<K, V>
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<K, V> 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<K, V> 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<K, V> entry in entries.iter) {
if (predicate(entry.key, entry.value)) {
return true;
}
}
return false;
}

@override
String toString() {
return entries.joinToString(
Expand Down
18 changes: 18 additions & 0 deletions lib/src/collection/kt_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,22 @@ abstract class KtMapExtension<K, V> {
* [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);
}
74 changes: 74 additions & 0 deletions test/collection/map_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,80 @@ void testMap(KtMap<K, V> Function<K, V>() 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<ArgumentError>(() => 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<ArgumentError>(() => 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<ArgumentError>(() => emptyMap().any(null));
expect(e.message, allOf(contains("null"), contains("predicate")));
});
});
}

class ThirdPartyMap<K, V>
Expand Down

0 comments on commit 328b919

Please sign in to comment.