Skip to content

Commit

Permalink
edited .separatedBy() to return a list by default. (#4)
Browse files Browse the repository at this point in the history
* removed .separatedBy() to return a list by default.

* addressed comments

* addressed comments. btw misellaneous is spelled wrong. should be miscellaneous

* some comments were cut out. put them back in.

* comment for helper function was vague and was updated to provide more clarity as to its role.

* changelog update
  • Loading branch information
decheverri123 authored May 25, 2023
1 parent 38130dd commit 695e113
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.1

- `.separatedBy()` now returns a list by default.

## 1.1.0

- Rename `horizontal` to `paddingHorizontal` and so on
Expand Down
11 changes: 7 additions & 4 deletions lib/src/miscellaneous_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ extension DevTools on Object {
/// Allows to insert a separator between the items of the iterable.
extension SeparatedIterable on Iterable<Widget> {
/// Allows to insert a [separator] between the items of the iterable.
Iterable<Widget> separatedBy(Widget separator) sync* {
List<Widget> separatedBy(Widget separator) {
final result = <Widget>[];
final iterator = this.iterator;
if (iterator.moveNext()) {
yield iterator.current;
result.add(iterator.current);
while (iterator.moveNext()) {
yield separator;
yield iterator.current;
result
..add(separator)
..add(iterator.current);
}
}
return result;
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: awesome_flutter_extensions
description: >
A Flutter Extension to remove boilerplate when accessing
ancestor context properties
version: 1.1.0
version: 1.1.1
homepage: https://bestofcode.dev
repository: https://github.com/nank1ro/awesome_flutter_extensions

Expand Down
58 changes: 58 additions & 0 deletions test/misellaneous_ext_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import 'package:awesome_flutter_extensions/src/miscellaneous_ext.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
// Dart's equality operator (==) tests whether two references are to the same
// object, not if they have the same value. The Text widgets in the expected
// list and in the actual list are different instances, so they are not equal
// even if their properties are the same. The way to solve this issue is to
// compare the data of the widgets, not the widgets themselves. You can do
// this by creating a helper function that checks if two widgets have the same
// type and data. If they do, then they are equal.

bool areWidgetsEqual(Widget widget1, Widget widget2) {
if (widget1.runtimeType != widget2.runtimeType) {
return false;
}

if (widget1 is Text && widget2 is Text) {
return (widget1.data == widget2.data);
} else if (widget1 is SizedBox && widget2 is SizedBox) {
return (widget1.height == widget2.height);
}

return false;
}

group('misellaneous_ext_test -', () {
test('num to Duration', () {
final dur1 = 1000.milliseconds;
Expand Down Expand Up @@ -29,5 +52,40 @@ void main() {
final dur = 1.seconds;
expect(dur.future(), isA<Future<dynamic>>());
});

test(
'SeparatedIterable.separatedBy inserts separator',
() {
// Create a list of widgets
final widgets = <Widget>[
const Text('1'),
const Text('2'),
const Text('3')
];

//Create a separator widget
const Widget separator = SizedBox(height: 8);

// Separate the widgets by the separator
final separatedList = widgets.separatedBy(separator);

final expectedResult = <Widget>[
const Text('1'),
const SizedBox(height: 8),
const Text('2'),
const SizedBox(height: 8),
const Text('3'),
];

expect(separatedList.length, expectedResult.length);

for (var i = 0; i < separatedList.length; i++) {
expect(
areWidgetsEqual(separatedList[i], expectedResult[i]),
isTrue,
);
}
},
);
});
}

0 comments on commit 695e113

Please sign in to comment.