enchanted_collection 1.3.0 copy "enchanted_collection: ^1.3.0" to clipboard
enchanted_collection: ^1.3.0 copied to clipboard

A collection toolkit of useful data structures and algorithms for collections.

Enchanted collection toolkit

A collection of useful data structures and algorithms for collections.

🗂️ Summary #

List util's #

For each mapper #

A for each function with aditional info in each interation. Such as:

  • Is the first interation? isFirst
  • Is the last interation? isLast
  • The index of the current interation index
  • The value of the current interation value
final forEachMapperExample = [1, 2, 3];
forEachMapperExample.forEachMapper((value, isFirst, isLast, index) {
  print('value: $value');
  print('isFirst: $isFirst');
  print('isLast: $isLast');
  print('index: $index');
});

Mapper #

A map function with aditional info in each interation.

Such as:

  • Is the first interation? isFirst
  • Is the last interation? isLast
  • The index of the current interation index
  • The value of the current interation value

Returns a list of the mapped values.

final mapperExample = ['igor', 'miranda', 'souza'];
final List<Map<int, String>> mappedList = mapperExample.mapper((
  value,
  isFirst,
  isLast,
  index,
) {
  if (isFirst || isLast) {
    return {index: value.toUpperCase()};
  }
  return {index: value};
});
print(mappedList); // [{0: "IGOR"}, {1: "miranda"}, {2: "SOUZA"}]

Single where or null #

Equal to singleWhere but will not throw a error if element dosen't exist's. But will return null instead.

final List<int> singleWhereOrNullList = [1, 2, 3];
// Will return null:
print(singleWhereOrNullList.singleWhereOrNull((e) => e == 4));
// Will return 3:
print(singleWhereOrNullList.singleWhereOrNull((e) => e == 3));

Insert in index #

Will add [newValue] in the list in the position of [index].

Equal to [List.insert] function. But will return the list itself with the added value. So you can cascade it.

final insertInIndexExample = [1, 2, 4];
insertInIndexExample.insertInIndex(1, 3);
print(insertInIndexExample); // [1, 2, 3, 4]

Change element in index #

Will change the value in the index position to newValue.

final changeListExample = [1, 2, 3];
changeListExample // cascade notation
    .changeAtIndexTo(0, 4)
    .changeAtIndexTo(1, 5)
    .changeAtIndexTo(2, 6);
print(changeListExample); // [4, 5, 6]

Add value as last element of list #

Will add [newValue] in the last position of the list.

Equal to [List.add] function. But will return the list itself with the added value. So you can cascade it.

final addInLastExample = [1, 2, 3];
addInLastExample // cascade notation
    .addInLast(4)
    .addInLast(5)
    .addInLast(6);
print(addInLastExample); // [1, 2, 3, 4, 5, 6]

Split into groups #

Will split the list into groups of quantityPerGroup elements.

The last group may have less elements than quantityPerGroup.

final splitIntoGroupsExample = [1, 2, 3, 4, 5, 6, 7];
final List<List<int>> groups = splitIntoGroupsExample.splitIntoGroups(3);
print(groups); // [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7 ] ]

Is any element diferent from null AND is any element null #

final withNullList = [1, 2, 3, null];
final withoutNullList = [1, 2, 3];

print(withNullList.isAnyElementDiffFromNull); // true
print(withoutNullList.isAnyElementDiffFromNull); // true

Remove null elements of a list #

final withNullList = [1, 2, 3, null];
print(withNullList.removeNull); // [1, 2, 3]

Integer util's #

bool isBiggerThen(int valueToBeCompared) #

Will return true if the value is bigger then [valueToBeCompared].

bool isBiggerOrEqualThen(int valueToBeCompared) #

Will return true if the value is bigger or equal then [valueToBeCompared].

bool isSmallerThen(int valueToBeCompared) #

Will return true if the value is smaller then [valueToBeCompared].

bool isSmallerOrEqualThen(int valueToBeCompared) #

Will return true if the value is smaller or equal then [valueToBeCompared].

Example of thoose 4 compare functions:

final isBiggerThenTest = 4.isBiggerThen(6); // false
final isBiggerOrEqualThenTest = 4.isBiggerOrEqualThen(4); // true 
final isSmallerThenTest = 4.isSmallerThen(6); // true
final isSmallerOrEqualThenTest = 4.isSmallerOrEqualThen(4); // true

Map util's #

Try cast map #

Try to decode a json string into a map. If the string is not a valid json, will return null.

Is just a boilerplate to avoid try/catch blocks. Basically, this:

try {
  return jsonDecode(this);
} catch (_) {
  return null;
}

Example:

final String invalidJson = 'invalid json';
final String validJson = '{"name": "igor"}';
print(invalidJson.tryDecode); // null
print(validJson.tryDecode); // {"name": "igor"}

Cast functions #

Transform a list into a map #

Will cast a list into a map. For each element in the list, will call the onElementToKey and onElementToValue functions.

Example:

final list = [
  {
    'name': 'igor',
    'age': 23
  },
  {
    'name': 'miranda',
    'age': 30
  },
];
final Map<String, int> persons = list.castToMap(
  (element) {
    return element['name'] as String;
  },
  (element) {
    return element['age'] as int;
  },
);

persons map will be:

{
  "igor": 23,
  "miranda": 30
}

Transform a list into a map with mapper #

Check castToMap documentation for base info about this function. That because this function is basically a castToMap but with more info in each interation, such as:

  • Is the first interation? isFirst
  • Is the last interation? isLast
  • The index of the current interation index
  • The value of the current interation value

Transform a map into a list #

For each Map entry, will map it to the return type with toElementFunc function and each return will be a element in the List.

final Map<String, int> personsMap = {
  "Igor": 23,
  "Miranda": 30,
};
final List<String> personsList = personsMap.castToList(
  (key, value) {
    return 'My name is $key, and I have $value years old.';
  },
);
print(personsList);

Cast object/dynamic to desired type #

Cast value to type [R]. Basically, a shortcut to this as R:

final dynamic dynamicList = 3;
final int intList = dynamicList.as<int>();
print(intList); // 3

Made with ❤ by Igor Miranda
If you like the package, give a 👍

2
likes
160
pub points
74%
popularity
screenshot

Publisher

unverified uploader

A collection toolkit of useful data structures and algorithms for collections.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on enchanted_collection