Skip to content

Commit

Permalink
add minOrNull(), maxOrNull() and deprecate min(), max() (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anas35 authored Oct 15, 2021
1 parent 9cc2506 commit a34fbf4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/src/collection/kt_iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ abstract class KtIterable<T> {
extension KtComparableIterableExtension<T extends Comparable<T>>
on KtIterable<T> {
/// Returns the largest element or `null` if there are no elements.
T? max() {
@Deprecated("use maxOrNull")
T? max() => maxOrNull();

/// Returns the largest element or `null` if there are no elements.
T? maxOrNull() {
final i = iterator();
if (!iterator().hasNext()) return null;
T max = i.next();
Expand All @@ -31,7 +35,11 @@ extension KtComparableIterableExtension<T extends Comparable<T>>
}

/// Returns the smallest element or `null` if there are no elements.
T? min() {
@Deprecated("use minOrNull")
T? min() => minOrNull();

/// Returns the smallest element or `null` if there are no elements.
T? minOrNull() {
final i = iterator();
if (!iterator().hasNext()) return null;
T min = i.next();
Expand All @@ -47,7 +55,11 @@ extension KtComparableIterableExtension<T extends Comparable<T>>

extension KtNumIterableExtension<T extends num> on KtIterable<T> {
/// Returns the largest element or `null` if there are no elements.
T? max() {
@Deprecated("use maxOrNull")
T? max() => maxOrNull();

/// Returns the largest element or `null` if there are no elements.
T? maxOrNull() {
final i = iterator();
if (!iterator().hasNext()) return null;
T max = i.next();
Expand All @@ -63,7 +75,11 @@ extension KtNumIterableExtension<T extends num> on KtIterable<T> {
}

/// Returns the smallest element or `null` if there are no elements.
T? min() {
@Deprecated("use minOrNull")
T? min() => minOrNull();

/// Returns the smallest element or `null` if there are no elements.
T? minOrNull() {
final i = iterator();
if (!iterator().hasNext()) return null;
T min = i.next();
Expand Down
52 changes: 52 additions & 0 deletions test/collection/iterable_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,32 @@ void testIterable(KtIterable<T> Function<T>() emptyIterable,
});
});

group("maxOrNull", () {
test("gets max value int", () {
final iterable = iterableOf([1, 3, 2]);
final int? max = iterable.maxOrNull();
expect(max, 3);
});

test("gets max value double", () {
final iterable = iterableOf([1.0, 3.2, 2.0]);
final double? max = iterable.maxOrNull();
expect(max, 3.2);
});

test("gets max value comparable", () {
final iterable = iterableOf(["a", "x", "b"]);
final String? max = iterable.maxOrNull();
expect(max, "x");
});

test("empty iterable return null", () {
final iterable = emptyIterable<int>();
final int? max = iterable.maxOrNull();
expect(max, null);
});
});

group("maxBy", () {
test("gets max value", () {
final iterable = iterableOf(["1", "3", "2"]);
Expand Down Expand Up @@ -1432,6 +1458,32 @@ void testIterable(KtIterable<T> Function<T>() emptyIterable,
});
});

group("minOrNull", () {
test("gets min int value", () {
final KtIterable<int> iterable = iterableOf([3, 1, 2]);
final int? min = iterable.minOrNull();
expect(min, 1);
});

test("gets min double value", () {
final KtIterable<double> iterable = iterableOf([3.2, 1.4, 2.2]);
final double? min = iterable.minOrNull();
expect(min, 1.4);
});

test("gets max value comparable", () {
final iterable = iterableOf(["x", "b", "a", "h"]);
final String? min = iterable.minOrNull();
expect(min, "a");
});

test("empty iterable return null", () {
final iterable = emptyIterable<int>();
final int? min = iterable.minOrNull();
expect(min, null);
});
});

group("minBy", () {
test("gets min value", () {
final iterable = iterableOf(["1", "3", "2"]);
Expand Down

0 comments on commit a34fbf4

Please sign in to comment.