-
Notifications
You must be signed in to change notification settings - Fork 3
Feature/extensions #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
500a3a7
Moved code to src folder
NicolaVerbeeck ce00f9e
Moved code to src folder
NicolaVerbeeck b5eee90
Added stream extensions
NicolaVerbeeck 2565ccd
Add tests
NicolaVerbeeck d1d4507
Merge branch 'main' into feature/Extensions
vanlooverenkoen 9a90a9c
Fixed coverage
NicolaVerbeeck 3f95a70
Merge remote-tracking branch 'origin/feature/Extensions' into feature…
NicolaVerbeeck b60840c
Formatting
NicolaVerbeeck dd45a28
Merge branch 'main' into feature/Extensions
NicolaVerbeeck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| export 'package:icapps_architecture/provider/change_notifier_ex.dart'; | ||
| export 'src/provider/change_notifier_ex.dart'; | ||
| export 'src/extension/list_extensions.dart'; | ||
| export 'src/extension/map_extensions.dart'; | ||
| export 'src/extension/iterable_extensions.dart'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import 'package:tuple/tuple.dart'; | ||
|
|
||
| extension IterableExtension<T> on Iterable<T> { | ||
| /// Counts all elements for which [where] returns true | ||
| int count(bool Function(T) where) { | ||
| var c = 0; | ||
| for (final element in this) { | ||
| if (where(element)) { | ||
| ++c; | ||
| } | ||
| } | ||
| return c; | ||
| } | ||
|
|
||
| /// Sums the result of [valueProvider] for each item | ||
| E sum<E extends num>(E Function(T) valueProducer) { | ||
| num value = 0; | ||
| forEach((e) => value = (value + valueProducer(e)) as E); | ||
| return value as E; | ||
| } | ||
|
|
||
| /// Finds the first item that matches [where], if no such item could be found | ||
| /// returns `null` | ||
| T? find(bool Function(T) where) { | ||
| for (final element in this) { | ||
| if (where(element)) return element; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /// Returns `true` if every item matches [where] | ||
| bool all(bool Function(T) where) { | ||
| for (final element in this) { | ||
| if (!where(element)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /// Create a map by mapping every element using [key]. Duplicate values | ||
| /// are discarded | ||
| Map<S, T> associateBy<S>(S Function(T) key) { | ||
| final map = <S, T>{}; | ||
| for (final element in this) { | ||
| map[key(element)] = element; | ||
| } | ||
| return map; | ||
| } | ||
|
|
||
| /// Splits the elements according to [on]. Items for which [on] is true will | ||
| /// be stored in [Tuple2.item1], other items in [Tuple2.item2] | ||
| Tuple2<List<T>, List<T>> split(bool Function(T) on) { | ||
| final left = <T>[]; | ||
| final right = <T>[]; | ||
| forEach((element) { | ||
| if (on(element)) { | ||
| left.add(element); | ||
| } else { | ||
| right.add(element); | ||
| } | ||
| }); | ||
|
|
||
| return Tuple2(left, right); | ||
| } | ||
|
|
||
| /// Same as [Iterable.map] except that the [mapper] function also receives | ||
| /// the index of the item being mapped | ||
| Iterable<R> mapIndexed<R>(R Function(int, T) mapper) { | ||
| var c = 0; | ||
| return map((element) => mapper(c++, element)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| extension ListExtensions<T> on List<T> { | ||
| ///Replaces all data in the list with [newData] | ||
| void replaceAll(List<T> newData) { | ||
| clear(); | ||
| addAll(newData); | ||
| } | ||
|
|
||
| /// Sorts the list based on the comparable returned by [by]. By default | ||
| /// the sorting is [ascending] | ||
| void sortBy<R>(Comparable<R>? by(T item), {bool ascending = true}) { | ||
| sort((a, b) { | ||
vanlooverenkoen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| final byA = by(a); | ||
| final byB = by(b); | ||
| if (byA == null) return ascending ? -1 : 1; | ||
| if (byB == null) return ascending ? 1 : -1; | ||
| return _compareValues(byA, byB, ascending); | ||
| }); | ||
| } | ||
|
|
||
| /// Sorts the list by comparing first comparing using [by] and if the items | ||
| /// are equal, by comparing them using [by2]. By default | ||
| /// the sorting is [ascending] | ||
| void sortBy2<R>( | ||
| Comparable<R>? by(T item), | ||
| Comparable<R>? by2(T item), { | ||
| bool ascending = true, | ||
| }) { | ||
| sort((a, b) { | ||
| final byA = by(a); | ||
| final byB = by(b); | ||
| if (byA == null && byB != null) return ascending ? -1 : 1; | ||
| if (byB == null && byA != null) return ascending ? 1 : -1; | ||
| if (byA != null && byB != null) { | ||
| final result = _compareValues(byA, byB, ascending); | ||
| if (result != 0) return result; | ||
| } | ||
|
|
||
| final byA2 = by2(a); | ||
| final byB2 = by2(b); | ||
| if (byA2 == null && byB2 == null) return 0; | ||
| if (byA2 == null) return ascending ? -1 : 1; | ||
| if (byB2 == null) return ascending ? 1 : -1; | ||
| return _compareValues(byA2, byB2, ascending); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| int _compareValues<T extends Comparable<dynamic>>(T a, T b, bool ascending) { | ||
| if (identical(a, b)) return 0; | ||
| if (ascending) return a.compareTo(b); | ||
| return -a.compareTo(b); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| extension MapExtension<K, V> on Map<K?, V> { | ||
| /// Removes all null keys from the map | ||
| Map<K, V> removeNullKeys() { | ||
| final map = <K, V>{}; | ||
| forEach((key, value) { | ||
| if (key != null) map[key] = value; | ||
| }); | ||
| return map; | ||
| } | ||
| } | ||
|
|
||
| extension MapExtension2<K, V> on Map<K, V?> { | ||
| /// Removes all null values from the map | ||
| Map<K, V> removeNullValues() { | ||
| final map = <K, V>{}; | ||
| forEach((key, value) { | ||
| if (value != null) map[key] = value; | ||
| }); | ||
| return map; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:icapps_architecture/icapps_architecture.dart'; | ||
|
|
||
| void main() { | ||
| late List<int> sut; | ||
| late List<double> sut2; | ||
|
|
||
| setUp(() { | ||
| sut = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
| sut2 = [1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1, 9.1, 10.1]; | ||
| }); | ||
| group('Iterable extension tests', () { | ||
| test('Test count', () { | ||
| expect(sut.count((e) => e < 100), 10); | ||
| expect(sut.count((e) => e < 5), 4); | ||
| }); | ||
| test('Test sum', () { | ||
| expect(sut.sum((e) => e), 55); | ||
| expect(sut2.sum((e) => e).round(), 56); | ||
| }); | ||
| test('Test find', () { | ||
| expect(sut.find((e) => e == 2), 2); | ||
| expect(sut.find((e) => e == 102), null); | ||
| }); | ||
| test('Test all', () { | ||
| expect(sut.all((e) => e < 100), true); | ||
| expect(sut.all((e) => e < 5), false); | ||
| }); | ||
| test('Test associate by', () { | ||
| expect(sut.associateBy((e) => e.toString()), { | ||
| '1': 1, | ||
| '2': 2, | ||
| '3': 3, | ||
| '4': 4, | ||
| '5': 5, | ||
| '6': 6, | ||
| '7': 7, | ||
| '8': 8, | ||
| '9': 9, | ||
| '10': 10, | ||
| }); | ||
| }); | ||
| test('Test split', () { | ||
| final result = sut.split((e) => e <= 7); | ||
| expect(result.item1, [1, 2, 3, 4, 5, 6, 7]); | ||
| expect(result.item2, [8, 9, 10]); | ||
| }); | ||
| test('Test map indexed', () { | ||
| final seenIndexes = <int>[]; | ||
| final seenValues = <int>[]; | ||
| sut.mapIndexed((index, e) { | ||
| seenIndexes.add(index); | ||
| seenValues.add(e); | ||
| }).toList(growable: false); | ||
| expect(seenIndexes, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
| expect(seenValues, sut); | ||
| }); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:icapps_architecture/icapps_architecture.dart'; | ||
| import 'package:tuple/tuple.dart'; | ||
|
|
||
| void main() { | ||
| late List<int> sut; | ||
|
|
||
| setUp(() { | ||
| sut = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
| }); | ||
| group('List extension tests', () { | ||
| test('Test replace all', () { | ||
| expect(sut, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); | ||
| expect(sut..replaceAll([11, 12, 13]), [11, 12, 13]); | ||
| }); | ||
| test('Test sort by', () { | ||
| sut.sortBy((e) => 10 - e); | ||
| expect(sut, [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); | ||
| }); | ||
| test('Test sort by null', () { | ||
| sut.sortBy((e) => e % 2 == 0 ? null : e); | ||
| expect(sut, [2, 4, 6, 8, 10, 1, 3, 5, 7, 9]); | ||
| }); | ||
| test('Test sort by descending', () { | ||
| sut.sortBy((e) => e, ascending: false); | ||
| expect(sut, [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); | ||
| }); | ||
| test('Test sort by null descending', () { | ||
| sut.sortBy((e) => e % 2 == 0 ? null : e, ascending: false); | ||
| expect(sut, [9, 7, 5, 3, 1, 10, 8, 6, 4, 2]); | ||
| }); | ||
| test('Test sort by2', () { | ||
| final complexSut = <Tuple2<String, int>>[ | ||
| Tuple2("ABC", 2), | ||
| Tuple2("ABC", 1), | ||
| Tuple2("BCD", 25), | ||
| Tuple2("ABCDE", 25), | ||
| ]; | ||
|
|
||
| complexSut.sortBy2((e) => e.item1, (e) => e.item2); | ||
| expect(complexSut, <Tuple2<String, int>>[ | ||
| Tuple2("ABC", 1), | ||
| Tuple2("ABC", 2), | ||
| Tuple2("ABCDE", 25), | ||
| Tuple2("BCD", 25), | ||
| ]); | ||
| }); | ||
| test('Test sort by2, descending', () { | ||
| final complexSut = <Tuple2<String, int>>[ | ||
| Tuple2("ABC", 2), | ||
| Tuple2("ABC", 1), | ||
| Tuple2("BCD", 25), | ||
| Tuple2("ABCDE", 25), | ||
| ]; | ||
|
|
||
| complexSut.sortBy2((e) => e.item1, (e) => e.item2, ascending: false); | ||
| expect(complexSut, <Tuple2<String, int>>[ | ||
| Tuple2("BCD", 25), | ||
| Tuple2("ABCDE", 25), | ||
| Tuple2("ABC", 2), | ||
| Tuple2("ABC", 1), | ||
| ]); | ||
| }); | ||
| test('Test sort by2 nullable', () { | ||
| final complexSut = <Tuple2<String?, int?>>[ | ||
| Tuple2("ABC", 2), | ||
| Tuple2("ABC", 1), | ||
| Tuple2("BCD", 25), | ||
| Tuple2(null, null), | ||
| Tuple2("ABCDE", 25), | ||
| Tuple2(null, 25), | ||
| Tuple2(null, 24), | ||
| Tuple2("ABC", null), | ||
| ]; | ||
|
|
||
| complexSut.sortBy2((e) => e.item1, (e) => e.item2); | ||
| expect(complexSut, <Tuple2<String?, int?>>[ | ||
| Tuple2(null, null), | ||
| Tuple2(null, 24), | ||
| Tuple2(null, 25), | ||
| Tuple2("ABC", null), | ||
| Tuple2("ABC", 1), | ||
| Tuple2("ABC", 2), | ||
| Tuple2("ABCDE", 25), | ||
| Tuple2("BCD", 25), | ||
| ]); | ||
| }); | ||
| test('Test sort by2 nullable descending', () { | ||
| final complexSut = <Tuple2<String?, int?>>[ | ||
| Tuple2("ABC", 2), | ||
| Tuple2("ABC", 1), | ||
| Tuple2(null, null), | ||
| Tuple2("BCD", 25), | ||
| Tuple2("ABCDE", 25), | ||
| Tuple2(null, 25), | ||
| Tuple2(null, 24), | ||
| Tuple2("ABC", null), | ||
| ]; | ||
|
|
||
| complexSut.sortBy2((e) => e.item1, (e) => e.item2, ascending: false); | ||
| expect(complexSut, <Tuple2<String?, int?>>[ | ||
| Tuple2("BCD", 25), | ||
| Tuple2("ABCDE", 25), | ||
| Tuple2("ABC", 2), | ||
| Tuple2("ABC", 1), | ||
| Tuple2("ABC", null), | ||
| Tuple2(null, 25), | ||
| Tuple2(null, 24), | ||
| Tuple2(null, null), | ||
| ]); | ||
| }); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.