-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Iterable separatedBy/separatedByIndexed extension (#705)
* feat: IterableExt separatedBy and separatedByIndexed * feat: use separatedBy extension * chore: use extension for widget spacings --------- Co-authored-by: Dominik Toton <[email protected]>
- Loading branch information
1 parent
9e13fe9
commit 9ed3380
Showing
8 changed files
with
146 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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 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 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
38 changes: 38 additions & 0 deletions
38
catalyst_voices/packages/catalyst_voices_shared/lib/src/utils/iterable_ext.dart
This file contains 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,38 @@ | ||
extension IterableExt<T> on Iterable<T> { | ||
/// Syntax sugar for [separatedByIndexed] when index or value does not | ||
/// matter. | ||
/// | ||
/// One common example is Column children separated by SizedBox. | ||
/// | ||
/// ```dart | ||
/// Column( | ||
/// children: <Widget>[ | ||
/// const Text('Title'), | ||
/// const Text('Subtitle'), | ||
/// const Text('Body'), | ||
/// ].separatedBy(const SizedBox(height: 8)).toList(), | ||
/// ); | ||
/// ``` | ||
Iterable<T> separatedBy(T value) => separatedByIndexed((_, __) => value); | ||
|
||
/// Inserts a value generated by the provided [builder] function between each | ||
/// element of the iterable. | ||
/// | ||
/// The builder function receives the index of the current element and | ||
/// the element itself. | ||
/// Returns a new iterable containing the original elements separated by the | ||
/// values generated by the builder function. | ||
Iterable<T> separatedByIndexed(T Function(int index, T value) builder) sync* { | ||
for (var index = 0; index < length; index++) { | ||
final value = elementAt(index); | ||
final isLast = index == length - 1; | ||
|
||
// Yield the current value and the separator if it's not the last element. | ||
if (isLast) { | ||
yield* [value]; | ||
} else { | ||
yield* [value, builder(index, value)]; | ||
} | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
catalyst_voices/packages/catalyst_voices_shared/test/src/utils/iterable_ext_test.dart
This file contains 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,85 @@ | ||
import 'package:catalyst_voices_shared/src/catalyst_voices_shared.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
void main() { | ||
group('separatedBy', () { | ||
test('adds given separator between each item in the list', () { | ||
// Given | ||
const source = [1, 2, 3, 4]; | ||
const separator = 99; | ||
const expectedList = [1, separator, 2, separator, 3, separator, 4]; | ||
|
||
// When | ||
final separatedSource = source.separatedBy(separator); | ||
|
||
// Then | ||
expect(separatedSource, expectedList); | ||
}); | ||
|
||
test('adds nothing when source is empty', () { | ||
// Given | ||
const source = <int>[]; | ||
const separator = 99; | ||
const expectedList = <int>[]; | ||
|
||
// When | ||
final separatedSource = source.separatedBy(separator); | ||
|
||
// Then | ||
expect(separatedSource, expectedList); | ||
}); | ||
|
||
test('adds nothing when source has one item', () { | ||
// Given | ||
const source = <int>[1]; | ||
const separator = 99; | ||
const expectedList = <int>[1]; | ||
|
||
// When | ||
final separatedSource = source.separatedBy(separator); | ||
|
||
// Then | ||
expect(separatedSource, expectedList); | ||
}); | ||
}); | ||
|
||
group('separatedByIndexed', () { | ||
test('inserts correctly separator base on index', () { | ||
// Given | ||
const source = [1, 2, 3, 4]; | ||
const expectedList = [1, 0, 2, 1, 3, 2, 4]; | ||
|
||
// When | ||
final separatedSource = source.separatedByIndexed((index, _) => index); | ||
|
||
// Then | ||
expect(separatedSource, expectedList); | ||
}); | ||
|
||
test('adds nothing when source is empty', () { | ||
// Given | ||
const source = <int>[]; | ||
const separator = 99; | ||
const expectedList = <int>[]; | ||
|
||
// When | ||
final separatedSource = source.separatedByIndexed((_, __) => separator); | ||
|
||
// Then | ||
expect(separatedSource, expectedList); | ||
}); | ||
|
||
test('adds nothing when source has one item', () { | ||
// Given | ||
const source = <int>[1]; | ||
const separator = 99; | ||
const expectedList = <int>[1]; | ||
|
||
// When | ||
final separatedSource = source.separatedByIndexed((_, __) => separator); | ||
|
||
// Then | ||
expect(separatedSource, expectedList); | ||
}); | ||
}); | ||
} |
This file contains 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 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 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