Repository: passsy/kt.dart (dart)

Issue:
Add minOrNull()
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/min-or-null.html

Pull request:
*  Add minOrNulll() and maxOrNull()
*  Deprecate min() and max()
* Closes #155 #156

Patch:
diff --git a/lib/src/collection/kt_iterable.dart b/lib/src/collection/kt_iterable.dart
index 59c330d..0fd853b 100644
--- a/lib/src/collection/kt_iterable.dart
+++ b/lib/src/collection/kt_iterable.dart
@@ -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();
@@ -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();
@@ -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();
@@ -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();


Test patch:
diff --git a/test/collection/iterable_extensions_test.dart b/test/collection/iterable_extensions_test.dart
index b1553de..b7de599 100644
--- a/test/collection/iterable_extensions_test.dart
+++ b/test/collection/iterable_extensions_test.dart
@@ -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"]);
@@ -1412,6 +1438,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"]);