Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KtMap methods none(), all() and any() #80

Merged
merged 2 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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