https://github.com/marcus-software/ms_map_utils
A dart package that implement a lot of util functions for a Map
https://github.com/marcus-software/ms_map_utils
dart flutter map
Last synced: 10 months ago
JSON representation
A dart package that implement a lot of util functions for a Map
- Host: GitHub
- URL: https://github.com/marcus-software/ms_map_utils
- Owner: Marcus-Software
- License: mit
- Created: 2020-01-13T13:08:07.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-03-03T13:04:13.000Z (over 4 years ago)
- Last Synced: 2024-11-15T06:43:58.873Z (over 1 year ago)
- Topics: dart, flutter, map
- Language: Dart
- Homepage: https://pub.dev/packages/ms_map_utils
- Size: 55.7 KB
- Stars: 7
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Overview
[](https://pub.dartlang.org/packages/ms_map_utils)
[](https://github.com/Marcus-Software/ms_map_utils/tree/master/test)
[](https://github.com/Marcus-Software/ms_map_utils)
A simple lib to increase Map with useful functions
## Whats MS_Map_Utils do
Add useful functions to map:
* [`compact`](#compact) remove all MapEntries that's values is `null` _it's recursive too_.
* [`containsKeys`](#containsKeys) check if map contains all keys of list.
* [`diff`](#diff) returns a new map contend only difference between maps.
* [`doIfContains`](#doIfContains) do some work if map contains a key.
* [`listCombine`](#listCombine) creates an `Map` by using one array for keys and another for its values
* [`putIfAbsentAsync`](#putIfAbsentAsync) put an item if absent or return existent value async.
* [`reduce`](#reduce) iterate all items in `Map` for reduce to a unique value returned from callback `ReduceFunction` .
* [`removeKeysExcept`](#removeKeysExcept) remove all entries that NOT contains a key in list.
* [`removeKeys`](#removeKeys) remove all entries that contains a key in list.
* [`trim`](#trim) trim all Strings in a map _it's recursive_.
## Usage
Just import lib and use [extensions](https://dart.dev/guides/language/extension-methods), call the functions to starts work:
``` dart
// Don't forget to import
import 'package:ms_map_utils/ms_map_utils.dart';
Map itsAMap = {'key1' : null, 'key2' : 'just a String'};
compact(itsAMap); // Output: {'key2':'just a String'}
// or using extensions.
itsAMap.compact(); // Output: {'key2':'just a String'}
```
The function `compact` remove all MapEntries that's values is `null` _it's recursive_.
``` dart
test('Must return a new empty HashMap without null values', () {
var mapWithNullValues = {
'k1': null,
'k2': null,
'k3': null,
'map': {
'k1': null,
'k2': null,
'k3': null,
},
'list': [
{
'k1': null,
'k2': null,
'k3': null,
},
{
'k1': null,
'k2': null,
'k3': null,
},
]
};
var mapWithoutNull = compact(mapWithNullValues);
expect(mapWithoutNull, {
'map': {},
'list': [{}, {}]
});
});
```
see more in [test file](./test/compact_test.dart).
The function `containsKeys` check if map contains all keys of list.
``` dart
test('Must return true if contains a list of keys', () {
var listOfKeyToCheck = ['key1', 'key2'];
var mapToCheck = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'};
expect(mapToCheck.containsKeys(listOfKeyToCheck), isTrue);
});
```
see more in [test file](./test/contains_keys_test.dart).
The function `diff` returns a new map contend only difference between maps
``` dart
test(
'must return a new map with differences in values with differences is in nested map',
() {
var fromMap = {
'key1': 'value1',
'nestedMap': {'key2': 'value2', 'key3': 'value3'},
'otherNestedMap': {'randomKey': []}
};
var toMap = {
'key1': 'value1',
'nestedMap': {'key2': 123, 'key3': 'value3'},
'otherNestedMap': 'random value'
};
expect(diff(fromMap, toMap), {
'nestedMap': {'key2': 123},
'otherNestedMap': 'random value'
});
});
```
see more in [test file](./test/diff_test.dart).
The function `doIfContains` will be call a callback function if the map contains a key ou else it will be a callback function `elseIf` if `elseIf` is null, null will return.
``` dart
test('must return a object if contains a key', () {
final testMap = {'key1': 'value1', 'key2': 'value2'};
final newThing = doIfContains>(testMap, 'key2',
doWork: (key, value) =>
[value.toString(), 'new awesome thing', key.toString()],
elseIf: () => ['nothing']);
expect(newThing, ['value2', 'new awesome thing', 'key2']);
});
```
see more in [test file](./test/list_combine_test.dart).
The function `listCombine` creates an `Map` by using one array for keys and another for its values.
``` dart
test('combine two list as map', () {
final keys = ['red', 'green', 'blue', 'white', 'black'];
final values = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFFFF, 0x000000];
final newMap = listCombine(keys, values);
expect(newMap is Map, isTrue);
expect({
'red': 0xFF0000,
'green': 0x00FF00,
'blue': 0x0000FF,
'white': 0xFFFFFF,
'black': 0x000000,
}, newMap);
});
```
see more in [test file](./test/do_if_contains_test.dart).
The function `putIfAbsentAsync` put an item if absent or return existent value async.
``` dart
test('Call async function for empty map', () async {
var emptyMap = {};
expect(emptyMap.containsKey('test'), isFalse);
var item = await emptyMap.putIfAbsentAsync('test', () async {
await Future.delayed(Duration(milliseconds: 1500));
return 'Random String';
});
expect(emptyMap, isNotEmpty);
expect(emptyMap.containsKey('test'), isTrue);
expect(item, 'Random String');
});
```
see more in [test file](./test/put_if_absent_async_test.dart).
The function `reduce` iterate all items in `Map` for reduce to a unique value returned from callback `ReduceFunction` .
``` dart
test('Multiplies all int values to 120', () {
Map mapNumbers = {
'key1': 1,
'key2': 2,
'key3': 3,
'key4': 4,
'key5': 5,
};
var value = mapNumbers
.reduce((int acc, _, value) => (acc ?? 1) * (value as int));
expect(value, 120, reason: 'Value reduced must be 120');
});
```
see more in [test file](./test/reduce_test.dart).
The function `removeKeys` remove all entries that contains a key in list.
``` dart
test('Remove all entries that has a key in list with recursive', () {
Map mapNumbers = {
'key1': 1,
'key2': 2,
'key3': 3,
'key4': 4,
'key5': 5,
'map': {
'key1': 1,
'key2': 2,
'key3': 3,
'key4': 4,
'key5': 5,
}
};
mapNumbers.removeKeys(['key1', 'key2'], true);
expect(mapNumbers.length, 4);
expect(mapNumbers['map'].length, 3);
});
```
see more in [test file](./test/remove_keys_test.dart).
The function `removeKeysExcept` remove all entries that NOT contains a key in list.
``` dart
test('Remove all entries that has a key NOT in list with recursive', () {
Map mapNumbers = {
'key1': 1,
'key2': 2,
'key3': 3,
'key4': 4,
'key5': 5,
'map': {
'key1': 1,
'key2': 2,
'key3': 3,
'key4': 4,
'key5': 5,
}
};
mapNumbers.removeKeysExcept(['key1', 'key2', 'map'], true);
expect(mapNumbers.length, 3);
expect(mapNumbers['map'].length, 2);
});
```
see more in [test file](./test/remove_keys_except_test.dart).
The function `trim` all Strings in a map _it's recursive_.
``` dart
test('Most trim any Strings values', () {
const mapToTrim = {
'key1': ' random string ',
'key2': ' another random string ',
'key3': 321
};
expect(mapToTrim.trim(true), {
'key1': 'random string',
'key2': 'another random string',
'key3': 321
});
});
```
see more in [test file](./test/trim_test.dart).
[See another libs here](https://pub.dev/publishers/marcussoftware.info/packages)