{"id":30052305,"url":"https://github.com/marcus-software/ms_map_utils","last_synced_at":"2025-08-07T16:57:15.367Z","repository":{"id":56834959,"uuid":"233597871","full_name":"Marcus-Software/ms_map_utils","owner":"Marcus-Software","description":"A dart package that implement a lot of util functions for a Map","archived":false,"fork":false,"pushed_at":"2022-03-03T13:04:13.000Z","size":57,"stargazers_count":7,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-15T06:43:58.873Z","etag":null,"topics":["dart","flutter","map"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/ms_map_utils","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Marcus-Software.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-13T13:08:07.000Z","updated_at":"2023-05-20T17:11:33.000Z","dependencies_parsed_at":"2022-09-02T03:40:50.631Z","dependency_job_id":null,"html_url":"https://github.com/Marcus-Software/ms_map_utils","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/Marcus-Software/ms_map_utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcus-Software%2Fms_map_utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcus-Software%2Fms_map_utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcus-Software%2Fms_map_utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcus-Software%2Fms_map_utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Marcus-Software","download_url":"https://codeload.github.com/Marcus-Software/ms_map_utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Marcus-Software%2Fms_map_utils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269292886,"owners_count":24392505,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-07T02:00:09.698Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dart","flutter","map"],"created_at":"2025-08-07T16:57:10.691Z","updated_at":"2025-08-07T16:57:15.353Z","avatar_url":"https://github.com/Marcus-Software.png","language":"Dart","funding_links":["https://www.buymeacoffee.com/marcusedu"],"categories":[],"sub_categories":[],"readme":"# Overview\n\n[![Pub](https://img.shields.io/pub/v/ms_map_utils.svg)](https://pub.dartlang.org/packages/ms_map_utils)\n[![Pub](./coverage_badge.svg)](https://github.com/Marcus-Software/ms_map_utils/tree/master/test)\n[![GitHub stars](https://img.shields.io/github/stars/Marcus-Software/ms_map_utils?style=social)](https://github.com/Marcus-Software/ms_map_utils)\n\u003cspan class=\"badge-buymeacoffee\"\u003e\n\u003ca href=\"https://www.buymeacoffee.com/marcusedu\" title=\"Donate to this project using Buy Me A Coffee\"\u003e\u003cimg src=\"https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg\" alt=\"Buy Me A Coffee donate button\" /\u003e\u003c/a\u003e\n\u003c/span\u003e\n\n\nA simple lib to increase Map with useful functions\n\n## Whats MS_Map_Utils do\n\nAdd useful functions to map:\n\n* [`compact`](#compact) remove all MapEntries that's values is `null` _it's recursive too_.\n* [`containsKeys`](#containsKeys) check if map contains all keys of list.\n* [`diff`](#diff) returns a new map contend only difference between maps.\n* [`doIfContains`](#doIfContains) do some work if map contains a key.\n* [`listCombine`](#listCombine) creates an `Map` by using one array for keys and another for its values\n* [`putIfAbsentAsync`](#putIfAbsentAsync) put an item if absent or return existent value async.\n* [`reduce`](#reduce) iterate all items in `Map` for reduce to a unique value returned from callback `ReduceFunction` .\n* [`removeKeysExcept`](#removeKeysExcept) remove all entries that NOT contains a key in list.\n* [`removeKeys`](#removeKeys) remove all entries that contains a key in list.\n* [`trim`](#trim) trim all Strings in a map _it's recursive_.\n\n## Usage\n\nJust import lib and use [extensions](https://dart.dev/guides/language/extension-methods), call the functions to starts work:\n\n``` dart\n// Don't forget to import\nimport 'package:ms_map_utils/ms_map_utils.dart';\n\nMap itsAMap = {'key1' : null, 'key2' : 'just a String'};\ncompact(itsAMap); // Output: {'key2':'just a String'}\n// or using extensions.\nitsAMap.compact(); // Output: {'key2':'just a String'}\n```\n\n## \u003ca name=\"compact\"\u003e\u003c/a\u003ecompact\n\nThe function `compact` remove all MapEntries that's values is `null` _it's recursive_.\n\n``` dart\ntest('Must return a new empty HashMap without null values', () {\n    var mapWithNullValues = {\n      'k1': null,\n      'k2': null,\n      'k3': null,\n      'map': {\n        'k1': null,\n        'k2': null,\n        'k3': null,\n      },\n      'list': [\n        {\n          'k1': null,\n          'k2': null,\n          'k3': null,\n        },\n        {\n          'k1': null,\n          'k2': null,\n          'k3': null,\n        },\n      ]\n    };\n    var mapWithoutNull = compact(mapWithNullValues);\n    expect(mapWithoutNull, {\n      'map': {},\n      'list': [{}, {}]\n    });\n  });\n```\n\nsee more in [test file](./test/compact_test.dart).\n\n## \u003ca name=\"containsKeys\"\u003e\u003c/a\u003econtainsKeys\n\nThe function `containsKeys` check if map contains all keys of list.\n\n``` dart\n  test('Must return true if contains a list of keys', () {\n    var listOfKeyToCheck = ['key1', 'key2'];\n    var mapToCheck = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'};\n    expect(mapToCheck.containsKeys(listOfKeyToCheck), isTrue);\n  });\n```\n\nsee more in [test file](./test/contains_keys_test.dart).\n\n## \u003ca name=\"diff\"\u003e\u003c/a\u003ediff\n\nThe function `diff` returns a new map contend only difference between maps\n\n``` dart\n  test(\n      'must return a new map with differences in values with differences is in nested map',\n      () {\n    var fromMap = {\n      'key1': 'value1',\n      'nestedMap': {'key2': 'value2', 'key3': 'value3'},\n      'otherNestedMap': {'randomKey': []}\n    };\n    var toMap = {\n      'key1': 'value1',\n      'nestedMap': {'key2': 123, 'key3': 'value3'},\n      'otherNestedMap': 'random value'\n    };\n    expect(diff(fromMap, toMap), {\n      'nestedMap': {'key2': 123},\n      'otherNestedMap': 'random value'\n    });\n  });\n```\n\nsee more in [test file](./test/diff_test.dart).\n\n## \u003ca name=\"doIfContains\"\u003e\u003c/a\u003edoIfContains\n\nThe 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.\n\n``` dart\n  test('must return a object if contains a key', () {\n    final testMap = {'key1': 'value1', 'key2': 'value2'};\n    final newThing = doIfContains\u003cList\u003cString\u003e\u003e(testMap, 'key2',\n        doWork: (key, value) =\u003e\n            [value.toString(), 'new awesome thing', key.toString()],\n        elseIf: () =\u003e ['nothing']);\n    expect(newThing, ['value2', 'new awesome thing', 'key2']);\n  });\n```\n\nsee more in [test file](./test/list_combine_test.dart).\n\n## \u003ca name=\"listCombine\"\u003e\u003c/a\u003elistCombine\n\nThe function `listCombine` creates an `Map` by using one array for keys and another for its values.\n\n``` dart\n  test('combine two list as map', () {\n    final keys = ['red', 'green', 'blue', 'white', 'black'];\n    final values = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFFFF, 0x000000];\n    final newMap = listCombine\u003cString, int\u003e(keys, values);\n    expect(newMap is Map\u003cString, int\u003e, isTrue);\n    expect({\n      'red': 0xFF0000,\n      'green': 0x00FF00,\n      'blue': 0x0000FF,\n      'white': 0xFFFFFF,\n      'black': 0x000000,\n    }, newMap);\n  });\n```\n\nsee more in [test file](./test/do_if_contains_test.dart).\n\n## \u003ca name=\"putIfAbsentAsync\"\u003e\u003c/a\u003eputIfAbsentAsync\n\nThe function `putIfAbsentAsync` put an item if absent or return existent value async.\n\n``` dart\n  test('Call async function for empty map', () async {\n    var emptyMap = \u003cString, String\u003e{};\n    expect(emptyMap.containsKey('test'), isFalse);\n    var item = await emptyMap.putIfAbsentAsync('test', () async {\n      await Future.delayed(Duration(milliseconds: 1500));\n      return 'Random String';\n    });\n    expect(emptyMap, isNotEmpty);\n    expect(emptyMap.containsKey('test'), isTrue);\n    expect(item, 'Random String');\n  });\n```\n\nsee more in [test file](./test/put_if_absent_async_test.dart).\n\n## \u003ca name=\"reduce\"\u003e\u003c/a\u003ereduce\n\nThe function `reduce` iterate all items in `Map` for reduce to a unique value returned from callback `ReduceFunction` .\n\n``` dart\n  test('Multiplies all int values to 120', () {\n    Map mapNumbers = \u003cString, int\u003e{\n      'key1': 1,\n      'key2': 2,\n      'key3': 3,\n      'key4': 4,\n      'key5': 5,\n    };\n    var value = mapNumbers\n        .reduce\u003cint\u003e((int acc, _, value) =\u003e (acc ?? 1) * (value as int));\n    expect(value, 120, reason: 'Value reduced must be 120');\n  });\n```\n\nsee more in [test file](./test/reduce_test.dart).\n\n## \u003ca name=\"removeKeys\"\u003e\u003c/a\u003eremoveKeys\n\nThe function `removeKeys` remove all entries that contains a key in list.\n\n``` dart\n  test('Remove all entries that has a key in list with recursive', () {\n    Map mapNumbers = {\n      'key1': 1,\n      'key2': 2,\n      'key3': 3,\n      'key4': 4,\n      'key5': 5,\n      'map': {\n        'key1': 1,\n        'key2': 2,\n        'key3': 3,\n        'key4': 4,\n        'key5': 5,\n      }\n    };\n    mapNumbers.removeKeys(['key1', 'key2'], true);\n    expect(mapNumbers.length, 4);\n    expect(mapNumbers['map'].length, 3);\n  });\n```\n\nsee more in [test file](./test/remove_keys_test.dart).\n\n## \u003ca name=\"removeKeysExcept\"\u003e\u003c/a\u003eremoveKeysExcept\n\nThe function `removeKeysExcept` remove all entries that NOT contains a key in list.\n\n``` dart\n  test('Remove all entries that has a key NOT in list with recursive', () {\n    Map mapNumbers = {\n      'key1': 1,\n      'key2': 2,\n      'key3': 3,\n      'key4': 4,\n      'key5': 5,\n      'map': {\n        'key1': 1,\n        'key2': 2,\n        'key3': 3,\n        'key4': 4,\n        'key5': 5,\n      }\n    };\n    mapNumbers.removeKeysExcept(['key1', 'key2', 'map'], true);\n    expect(mapNumbers.length, 3);\n    expect(mapNumbers['map'].length, 2);\n  });\n```\n\nsee more in [test file](./test/remove_keys_except_test.dart).\n\n## \u003ca name=\"trim\"\u003e\u003c/a\u003etrim\n\nThe function `trim` all Strings in a map _it's recursive_.\n\n``` dart\n    test('Most trim any Strings values', () {\n      const mapToTrim = {\n        'key1': '  random string    ',\n        'key2': '       another random string       ',\n        'key3': 321\n      };\n      expect(mapToTrim.trim(true), {\n        'key1': 'random string',\n        'key2': 'another random string',\n        'key3': 321\n      });\n    });\n```\n\nsee more in [test file](./test/trim_test.dart).\n\n[See another libs here](https://pub.dev/publishers/marcussoftware.info/packages)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcus-software%2Fms_map_utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarcus-software%2Fms_map_utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarcus-software%2Fms_map_utils/lists"}