ms_map_utils 0.6.0+1 copy "ms_map_utils: ^0.6.0+1" to clipboard
ms_map_utils: ^0.6.0+1 copied to clipboard

outdated

A dart package that implement a lot of util functions for a Map

Overview Pub #

GitHub stars

A simple lib to incremease Map with usefull functions

Whats MS_Map_Utils do #

Add usefull functions to map:

  • trim trim all Strings in a map it's recursive.
  • compact remove all MapEntries thats values is null it's recursive too.
  • reduce iterate all items in Map for reduce to a unique value returned from callback ReduceFunction .
  • removeKeys remove all entries that contains a key in list.
  • removeKeysExcept remove all entries that NOT contains a key in list.
  • putIfAbsentAsync put a item if absent or return existent value async.
  • containsKeys check if map contains all keys of list.
  • doIfContains do some work if map contains a key.

Usage #

Just import lib and use extensions, call the functions to starts work:

// 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'}

trim #

The function trim all Strings in a map it's recursive.

    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.

compact #

The function compact remove all MapEntries thats values is null it's recursive.

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.

reduce #

The function reduce iterate all items in Map for reduce to a unique value returned from callback ReduceFunction .

  test('Multiplies all int values to 120', () {
    Map mapNumbers = <String, int>{
      'key1': 1,
      'key2': 2,
      'key3': 3,
      'key4': 4,
      'key5': 5,
    };
    var value = mapNumbers
        .reduce<int>((int acc, _, value) => (acc ?? 1) * (value as int));
    expect(value, 120, reason: 'Value reduced must be 120');
  });

see more in test file.

removeKeys #

The function removeKeys remove all entries that contains a key in list.

  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.

removeKeysExcept #

The function removeKeysExcept remove all entries that NOT contains a key in list.

  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.

putIfAbsentAsync #

The function putIfAbsentAsync put a item if absent or return existent value async.

  test('Call async function for empty map', () async {
    var emptyMap = <String, String>{};
    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.

containsKeys #

The function containsKeys check if map contains all keys of list.

  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.

doIfContains #

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 returned.

  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']);
  });

see more in test file.

21
likes
0
pub points
71%
popularity

Publisher

verified publishermarcussoftware.info

A dart package that implement a lot of util functions for a Map

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

meta

More

Packages that depend on ms_map_utils