Skip to content

#117 add: Iterable.modifyWhere() and Iterable.modifyFirstWhere() #152

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ final increasingSubSequences = list.chunkWhile((a, b) => a + 1 == b);
`splitWhen` is the opposite of `chunkWhile` that starts a new chunk every time
the predicate _didn't_ match.

### .modifyWhere()

Applies a given modifier as long as predicate is true:
```dart
final values = [1, 2, 3];
values.modifyWhere((v) => v > 1, (v) => -v); // [1, -2, -3]
```

### .modifyFirstWhere()

Applies the modifier to the first element that matches predicate:
```dart
final values = [1, 2, 3];
values.modifyWhere((v) => v > 1, (v) => -v); // [1, -2, 3]
```

## String

### .capitalize
Expand Down
37 changes: 37 additions & 0 deletions lib/src/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1381,3 +1381,40 @@ extension IterableStartsWithExtension<E> on Iterable<E> {
return true;
}
}

extension IterableModifyWhereExtension<T> on Iterable<T> {
/// Applies [modifier] to all elements matching the given [predicate]
///
/// ```dart
/// final values = [1, 2, 3];
/// values.modifyWhere((v) => v > 1, (v) => -v); // [1, -2, -3]
/// ```
Iterable<T> modifyWhere(
bool Function(T) predicate,
T Function(T) modifier,
) {
return map((item) => predicate(item) ? modifier(item) : item);
}
}

extension IterableModifyFirstWhereExtension<T> on Iterable<T> {
/// Applies [modifier] to the first element matching the given [predicate]
///
/// ```dart
/// final values = [1, 2, 3];
/// values.modifyWhere((v) => v > 1, (v) => -v); // [1, -2, 3]
/// ```
Iterable<T> modifyFirstWhere(
bool Function(T) predicate,
T Function(T) modifier,
) {
bool modified = false;
return modifyWhere(
(v) => !modified && predicate(v),
(v) {
modified = true;
return modifier(v);
},
);
}
}
42 changes: 42 additions & 0 deletions test/iterable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1003,5 +1003,47 @@ void main() {
expect([null, 1, 2, 3].startsWith([null, 1]), true);
expect([1, null, 3].startsWith([1, null]), true);
});

test('.modifyWhere()', () {
final items = [1, 2, 3, 4, 5, 6];

expect(
items.modifyWhere((v) => v < 3, (i) => -i),
[-1, -2, 3, 4, 5, 6],
);
expect(
items.modifyWhere((v) => v > 3, (i) => -i),
[1, 2, 3, -4, -5, -6],
);
expect(
items.modifyWhere((v) => v < 0, (i) => -i),
[1, 2, 3, 4, 5, 6],
);
expect(
items.modifyWhere((v) => v > 0, (i) => i * 2),
[2, 4, 6, 8, 10, 12],
);
});

test('.modifyFirstWhere()', () {
final items = [1, 2, 3, 4, 5, 6];

expect(
items.modifyFirstWhere((v) => v < 3, (i) => -i),
[-1, 2, 3, 4, 5, 6],
);
expect(
items.modifyFirstWhere((v) => v > 3, (i) => -i),
[1, 2, 3, -4, 5, 6],
);
expect(
items.modifyFirstWhere((v) => v < 0, (i) => -i),
[1, 2, 3, 4, 5, 6],
);
expect(
items.modifyFirstWhere((v) => v > 0, (i) => i * 2),
[2, 2, 3, 4, 5, 6],
);
});
});
}