-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(djangoflow_remote_config): Add mapExtensions (#71)
- Add `removeNullAndCast` to `Map<String, dynamic>` - Add tests - Add export
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 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
17 changes: 17 additions & 0 deletions
17
packages/djangoflow_remote_config/lib/src/utils/map_extensions.dart
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,17 @@ | ||
extension MapExtensions on Map<String, dynamic>? { | ||
/// Removes all null values from the map and casts it to a Map<String, Object> | ||
Map<String, Object>? removeNullAndCast() { | ||
final Map<String, Object> newMap = {}; | ||
if (this == null || this!.isEmpty) { | ||
return null; | ||
} else { | ||
this!.forEach((key, value) { | ||
if (value != null) { | ||
newMap[key] = value; | ||
} | ||
}); | ||
|
||
return newMap; | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
packages/djangoflow_remote_config/test/utils/extensions/map_extensions_test.dart
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,41 @@ | ||
import 'package:djangoflow_remote_config/src/utils/map_extensions.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
const Map<String, dynamic> testMap = { | ||
'key1': 'value1', | ||
'key2': 2, | ||
'key3': null, | ||
}; | ||
|
||
test('MapExtension returns null if map is null', () { | ||
const Map<String, dynamic>? nullMap = null; | ||
final Map<String, Object>? newMap = nullMap?.removeNullAndCast(); | ||
|
||
expect(newMap, null); | ||
}); | ||
|
||
test('MapExtension returns null if map is empty', () { | ||
const Map<String, dynamic> nullMap = {}; | ||
final Map<String, Object>? newMap = nullMap.removeNullAndCast(); | ||
|
||
expect(newMap, null); | ||
}); | ||
|
||
test( | ||
'MapExtension casts Map<String,dynamic> to Map<String, Object>', | ||
() { | ||
final Map<String, Object>? newMap = testMap.removeNullAndCast(); | ||
expect(newMap, isA<Map<String, Object>>()); | ||
}, | ||
); | ||
|
||
test( | ||
'MapExtension removes null values', | ||
() { | ||
final Map<String, Object>? newMap = testMap.removeNullAndCast(); | ||
expect(newMap?.containsValue(null), false); | ||
expect(newMap, {'key1': 'value1', 'key2': 2}); | ||
}, | ||
); | ||
} |