Skip to content

Commit

Permalink
feat(djangoflow_remote_config): Add mapExtensions (#71)
Browse files Browse the repository at this point in the history
- Add `removeNullAndCast` to `Map<String, dynamic>`
- Add tests
- Add export
  • Loading branch information
ajil-apx committed Oct 26, 2023
1 parent b99d82c commit aad72fe
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Expand Up @@ -6,3 +6,4 @@ export 'src/data/models/app_launch_details/app_launch_details.dart';
export 'src/presentation/remote_config_part_updater.dart';
export 'src/configuration/constants.dart';
export 'src/utils/remote_config_extensions.dart';
export 'src/utils/map_extensions.dart';
@@ -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;
}
}
}
@@ -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});
},
);
}

0 comments on commit aad72fe

Please sign in to comment.