Skip to content

Commit

Permalink
Added foreachindexed & fixed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vanlooverenkoen committed Apr 20, 2021
1 parent 8a62869 commit 5895752
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
7 changes: 7 additions & 0 deletions lib/src/extension/iterable_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ extension IterableExtension<T> on Iterable<T> {
var c = 0;
return map((element) => mapper(c++, element));
}

/// Same as [Iterable.foreach] except that the [f] function also receives
/// the index of the item
void forEachIndexed(Function(int, T) f) {
var index = 0;
return forEach((element) => f(index++, element));
}
}

/// Extension on lists
Expand Down
21 changes: 6 additions & 15 deletions lib/src/extension/list_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,15 @@ extension ListExtensions<T> on List<T> {
addAll(newData);
}

///Replaces first item that matches where with [newData]
void replaceFirstWhere(bool Function(T) where, T newData) {
final result = find(where);
if (result == null) return;
final index = indexOf(result);
removeAt(index);
insert(index, newData);
}

///Replaces all items that matches where with [newData]
void replaceWhere(bool Function(T) where, T newData) {
void replaceWhere(bool Function(T) where, T newData, {int? count}) {
final whereResult = this.where(where);
print(whereResult);
whereResult.forEach((result) {
whereResult.forEachIndexed((index, result) {
if (count != null && index >= count) return;
if (result == null) return;
final index = indexOf(result);
removeAt(index);
insert(index, newData);
final originalIndex = indexOf(result);
removeAt(originalIndex);
insert(originalIndex, newData);
});
}

Expand Down
10 changes: 10 additions & 0 deletions test/extension/iterable_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ void main() {
expect(seenIndexes, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
expect(seenValues, sut);
});
test('Test foreach indexed', () {
final seenIndexes = <int>[];
final seenValues = <int>[];
sut.forEachIndexed((index, e) {
seenIndexes.add(index);
seenValues.add(e);
});
expect(seenIndexes, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
expect(seenValues, sut);
});
test('Test flatten', () {
final Iterable<Iterable<String>> list = [
['1', '2'],
Expand Down
9 changes: 7 additions & 2 deletions test/extension/list_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ void main() {
expect(sut, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
expect(sut..replaceAll([11, 12, 13]), [11, 12, 13]);
});
test('Test replaceFirstWhere', () {
test('Test replaceWhere count 1', () {
expect(sut, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
expect(sut..replaceFirstWhere((item) => item == 2, 100),
expect(sut..replaceWhere((item) => item == 2, 100, count: 1),
[1, 100, 3, 4, 5, 6, 7, 8, 9, 10]);
});
test('Test replaceWhere count 0', () {
expect(sut, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
expect(sut..replaceWhere((item) => item == 2, 100, count: 0),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
});
test('Test replaceWhere', () {
expect(sut, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
expect(
Expand Down

0 comments on commit 5895752

Please sign in to comment.