-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Marcus-Software/mde/feature/containsKeys
doIfContains implemented
- Loading branch information
Showing
9 changed files
with
167 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
part of '../ms_map_utils.dart'; | ||
|
||
/// Return a result of [doWork] if a map contains a key or return | ||
/// a result of [elseIf] if non null else will return null | ||
T doIfContains<T>(Map map, Object key, | ||
{T Function(Object key, Object value) doWork, T Function() elseIf}) { | ||
assert(doWork != null, 'doWork should not be non null'); | ||
assert(map != null, 'map should not be non null'); | ||
if (map?.containsKey(key) == true) { | ||
return doWork(key, map[key]); | ||
} else if (elseIf != null) { | ||
return elseIf(); | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'package:ms_map_utils/ms_map_utils.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test('must return a object if contains a key', () { | ||
final testMap = {'key1': 'value1', 'key2': 'value2'}; | ||
final newThing = doIfContains<List<String>>(testMap, 'key2', | ||
doWork: (key, value) => | ||
[value.toString(), 'new awesome thing', key.toString()], | ||
elseIf: () => ['nothing']); | ||
expect(newThing, ['value2', 'new awesome thing', 'key2']); | ||
}); | ||
|
||
test('must return a default object if not contains a key', () { | ||
final testMap = {'key1': 'value1', 'key2': 'value2'}; | ||
final newThing = doIfContains<List<String>>(testMap, 'key3', | ||
doWork: (key, value) => | ||
[value.toString(), 'new awesome thing', key.toString()], | ||
elseIf: () => ['nothing']); | ||
expect(newThing, ['nothing']); | ||
}); | ||
|
||
test( | ||
'must return a null if not contains a key and not pass callback to elseIf', | ||
() { | ||
final testMap = {'key1': 'value1', 'key2': 'value2'}; | ||
final newThing = doIfContains<List<String>>(testMap, 'key3', | ||
doWork: (key, value) => | ||
[value.toString(), 'new awesome thing', key.toString()]); | ||
expect(newThing, null); | ||
}); | ||
|
||
test('must return throw assertion error if doWork is null', () { | ||
final testMap = {'key1': 'value1', 'key2': 'value2'}; | ||
expect(() => doIfContains<List<String>>(testMap, 'key3'), | ||
throwsA(TypeMatcher<AssertionError>())); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters