Last updated:
0 purchases
enchanted collection
Enchanted collection toolkit
A collection of useful data structures and algorithms for collections.
🗂️ Summary #
List util's
⦿ For each mapper
⦿ Mapper
⦿ Single where or null
⦿ Insert in index
⦿ Change element in index
⦿ Add value as last element of list
⦿ Split into groups
⦿ Is any element diferent from null AND is any element null
⦿ Remove null elements of a list
Map util's
⦿ Try cast map ( Return null if not possible )
Cast functions
⦿ Transform a list into a map
⦿ Transform a list into a map with mapper
⦿ Transform a map into a list
⦿ Cast object/dynamic to desired type
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');
});
copied to clipboard
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"}]
copied to clipboard
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));
copied to clipboard
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]
copied to clipboard
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]
copied to clipboard
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]
copied to clipboard
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 ] ]
copied to clipboard
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
copied to clipboard
Remove null elements of a list #
final withNullList = [1, 2, 3, null];
print(withNullList.removeNull); // [1, 2, 3]
copied to clipboard
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
copied to clipboard
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;
}
copied to clipboard
Example:
final String invalidJson = 'invalid json';
final String validJson = '{"name": "igor"}';
print(invalidJson.tryDecode); // null
print(validJson.tryDecode); // {"name": "igor"}
copied to clipboard
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;
},
);
copied to clipboard
persons map will be:
{
"igor": 23,
"miranda": 30
}
copied to clipboard
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
copied to clipboard
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);
copied to clipboard
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
copied to clipboard
Made with ❤ by Igor Miranda
If you like the package, give a 👍
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.