octopus_extensions 2.2.16 copy "octopus_extensions: ^2.2.16" to clipboard
octopus_extensions: ^2.2.16 copied to clipboard

Pack of extensions and helters for day to day code boost

Octopus Extensions #

A lightweight Dart package that extends core types like Map, Iterable, String, Number, and DateTime with additional convenient methods.


Features #

  • Utility methods to simplify and clean your Dart code.
  • No external dependencies.

Getting started #

Add this to your pubspec.yaml:

dependencies:
  extensions: ^latest

Then run:

flutter pub get

or

dart pub get

Import it in your Dart code:

import 'package:octopusllc/extensions.dart';

Map Extensions #


Usage #

mergeWith #

final Map<String, dynamic> settings = {
  'tasks': {'type': 'agent', 'icon': 'add'},
};

final Map<String, dynamic> i18n = {
  'tasks': {'title': 'Tasks'},
  'goals': {'title': 'Goals'},
};

final Map<String, dynamic> merged = settings.mergeWith(i18n);

/* RESULT
{
  'tasks': {'type': 'agent', 'icon': 'add', 'title': 'Tasks'},
  'goals': {'title': 'Goals'},
}
*/

getByField #

final List<Map<String, dynamic>> users = [
  {'name': 'Alice'},
  {'name': 'Bob'},
  {'name': 'Charlie'},
];

final List<String> names = users.getByField<String>('name');

/* RESULT
[Alice, Bob, Charlie]
*/

filterByKeys #

final Map<String, dynamic> data = {
  'a': {'value': 1},
  'b': {'value': 2},
};

final Map<String, dynamic> filtered = data.filterByKeys(['b']);

/* RESULT
{'b': {'value': 2'}}
*/

filterBy #

final Map<String, Map<String, dynamic>> data = {
  'monday': {
    '1': {'type': 'workout'},
    '2': {'type': 'rest'},
  },
};

final Map<String, dynamic> result = data.filterBy('type', 'workout');

/* RESULT
{
  'monday': {
    '1': {'type': 'workout'}
  }
}
*/

countByField #

final Map<String, Map<String, Map<String, dynamic>>> data = {
  'day1': {
    'e1': {'type': 'meeting'},
    'e2': {'type': 'call'},
  },
  'day2': {
    'e3': {'type': 'meeting'},
  },
};

final Map<String, int> counts = data.countByField('type');

/* RESULT
{'meeting': 2, 'call': 1}
*/

groupByChildField #

final Map<String, Map<String, String>> colors = {
  'c1': {'group': 'spring'},
  'c2': {'group': 'summer'},
  'c3': {'group': 'spring'},
};

final Map<String, Map<String, Map<String, String>>> grouped = colors.groupByChildField('group');

/* RESULT
{
  'spring': {
    'c1': {'group': 'spring'},
    'c3': {'group': 'spring'}
  },
  'summer': {
    'c2': {'group': 'summer'}
  }
}
*/

filterByValue #

final Map<String, Map<String, dynamic>> data = {
  '1': {'type': 'goal'},
  '2': {'type': 'task'},
};

final Map<String, Map<String, dynamic>> filtered = data.filterByValue('type', 'goal');

/* RESULT
{'1': {'type': 'goal'}}
*/

groupByField #

final Map<String, Map<String, Map<String, dynamic>>> data = {
  '1': {
    'a': {'priority': 'high'},
    'b': {'priority': 'low'},
  },
};

final Map<String, List<Map<String, dynamic>>> grouped = data.groupByField('priority');

/* RESULT
{
  'high': [{'priority': 'high'}],
  'low': [{'priority': 'low'}]
}
*/

stringValuesOnly #

final Map<String, dynamic> map = {
  'created': DateTime(2022, 1, 1),
  'name': 'Alice',
};

final Map<String, dynamic> result = map.stringValuesOnly;

/* RESULT
{created: 2022-01-01 00:00:00.000, name: Alice}
*/

convertAllValuesToString #

final Map<String, dynamic> data = {
  'name': 'Alice',
  'details': {
    'birthday': DateTime(1990, 1, 1),
  },
};

final Map<String, dynamic> result = data.convertAllValuesToString();

/* RESULT
{
  name: Alice,
  details: {birthday: 1990-01-01T00:00:00.000}
}
*/

getOptionsAsList #

final Map<String, Map<String, dynamic>> strategies = {
  's1': {'type': 'growth'},
  's2': {'type': 'risk'},
};

final Map<String, List<Map<String, dynamic>>> grouped = strategies.getOptionsAsList(byField: 'type');

/* RESULT
{
  'growth': [{'strategy': 's1'}],
  'risk': [{'strategy': 's2'}]
}
*/

getBoolList #

final List<int> list = [0, 1, 2];

final List<bool> result = list.getBoolList;

/* RESULT
[true, false, false]
*/

toBoolList #

final List<int> list = [0, 1, 2];

final List<bool> result = list.toBoolList(trueAtIndex: 1);

/* RESULT
[false, true, false]
*/

calculateAvarage #

final Map<String, int> data = {'10': 2, '20': 4};

final double average = data.calculateAvarage;

/* RESULT
16.666666666666668
*/

sumValues #

final Map<String, num> data = {'a': 5, 'b': 10};

final double sum = data.sumValues;

/* RESULT
15.0
*/

randomEntryByField #

final Map<String, Map<String, dynamic>> data = {
  'x': {'title': 'Alpha'},
  'y': {'title': 'Beta'},
};

final String randomTitle = data.randomEntryByField('title');

/* RESULT
Alpha // or Beta (random)
*/

reversedKeys #

final Map<String, dynamic> original = {'a': 1, 'b': 2};

final Map<String, dynamic> reversed = original.reversedKeys;

/* RESULT
{'b': 2, 'a': 1}
*/
0
likes
0
points
62
downloads

Publisher

verified publisheroctopus-apps.com

Weekly Downloads

Pack of extensions and helters for day to day code boost

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

intl

More

Packages that depend on octopus_extensions