Skip to content

Commit

Permalink
Merge 8357f4e into 891d0b5
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Sep 22, 2021
2 parents 891d0b5 + 8357f4e commit 9838f76
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/utils/iterable_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extension IterableExtension<E> on Iterable<E> {
}) {
if (isEmpty) return const Iterable.empty();

if (E == num) {
if (E == num || E == int || E == double) {
nextValue ??= (a) => (a as num) + 1 as E;
} else if (E == String) {
nextValue ??= (a) {
Expand All @@ -81,11 +81,9 @@ extension IterableExtension<E> on Iterable<E> {
final a = elementAt(i);
b = elementAt(i + 1);

if (!(compare ??= (a, b) => a == b)(nextValue(a), b)) {
ranges.add([
start,
if (inclusive) nextValue(a) else a,
]);
final nextA = nextValue(a);
if (!(compare ??= (a, b) => a == b)(nextA, b)) {
ranges.add([start, if (inclusive) nextA else a]);
start = b;
}
}
Expand Down
22 changes: 22 additions & 0 deletions test/iterable_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ void main() {
});

group('.compactizeRange', () {
test('should return an empty Iterable', () {
expect(const [].compactizeRange(), const []);
});

test('should return one range pair', () {
expect(
const [1].compactizeRange(),
const [
[1, 1],
],
);
});

test('should return one range pair, inclusive', () {
expect(
const [1].compactizeRange(inclusive: true),
const [
[1, 2],
],
);
});

test('should return a compactized Iterable<num>', () {
expect(
const [1, 2, 3, 4, 5.0, 7, 8, 9, 11].compactizeRange(),
Expand Down

0 comments on commit 9838f76

Please sign in to comment.