Skip to content

Commit

Permalink
To use dart property, T doesn't have to be Comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
passsy committed May 24, 2021
1 parent 37d5cdd commit 1df6e1a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/src/collection/kt_iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ abstract class KtIterable<T> {

extension KtComparableIterableExtension<T extends Comparable<T>>
on KtIterable<T> {
/// Returns a dart:core [Iterable]
///
/// This method can be used to interop between the dart:collection and the
/// kt.dart world.
Iterable<T> get dart => iter;

/// Returns the largest element or `null` if there are no elements.
T? max() {
final i = iterator();
Expand Down Expand Up @@ -125,6 +119,12 @@ extension KtDoubleIterableExtension on KtIterable<double> {
}

extension KtIterableExtensions<T> on KtIterable<T> {
/// Returns a dart:core [Iterable]
///
/// This method can be used to interop between the dart:collection and the
/// kt.dart world.
Iterable<T> get dart => iter;

/// Returns `true` if all elements match the given [predicate].
bool all(bool Function(T element) predicate) {
if (this is KtCollection && (this as KtCollection).isEmpty()) return true;
Expand Down
30 changes: 30 additions & 0 deletions test/collection/iterable_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,15 @@ void testIterable(KtIterable<T> Function<T>() emptyIterable,
final Iterable<String> iterable = emptyIterable<String>().dart;
expect(iterable.length, 0);
});

test('dart work on all objects', () {
// there was once a bug where it only worked for Comparable<T>
iterableOf(<dynamic>[]).dart;
iterableOf(<Object>[]).dart;
iterableOf(<num>[]).dart;
iterableOf(<RegExp>[]).dart;
iterableOf(<Future>[]).dart;
});
});

group("drop", () {
Expand Down Expand Up @@ -1025,6 +1034,27 @@ void testIterable(KtIterable<T> Function<T>() emptyIterable,
});
});

group("iter", () {
test("iterate using a for loop", () {
final items = KtMutableList<String>.empty();
for (final String s in iterableOf(["a", "b", "c"]).iter) {
items.add(s);
}
expect(items.size, 3);
if (ordered) {
expect(items, listOf("a", "b", "c"));
}
});

test('iter work on all objects', () {
iterableOf(<dynamic>[]).iter;
iterableOf(<Object>[]).iter;
iterableOf(<num>[]).iter;
iterableOf(<RegExp>[]).iter;
iterableOf(<Future>[]).iter;
});
});

group("joinToString", () {
if (ordered) {
test("joinToString", () {
Expand Down

0 comments on commit 1df6e1a

Please sign in to comment.