From a34fbf45a1b591190fe352835e0bf5dda6f8ecb2 Mon Sep 17 00:00:00 2001 From: Anas35 <65850618+Anas35@users.noreply.github.com> Date: Fri, 15 Oct 2021 17:36:14 +0530 Subject: [PATCH] add minOrNull(), maxOrNull() and deprecate min(), max() (#170) --- lib/src/collection/kt_iterable.dart | 24 +++++++-- test/collection/iterable_extensions_test.dart | 52 +++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/lib/src/collection/kt_iterable.dart b/lib/src/collection/kt_iterable.dart index bd4b9a2..bf516a0 100644 --- a/lib/src/collection/kt_iterable.dart +++ b/lib/src/collection/kt_iterable.dart @@ -17,7 +17,11 @@ abstract class KtIterable { extension KtComparableIterableExtension> on KtIterable { /// 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> } /// 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> extension KtNumIterableExtension on KtIterable { /// 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 on KtIterable { } /// 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(); diff --git a/test/collection/iterable_extensions_test.dart b/test/collection/iterable_extensions_test.dart index 120a192..502ae78 100644 --- a/test/collection/iterable_extensions_test.dart +++ b/test/collection/iterable_extensions_test.dart @@ -1354,6 +1354,32 @@ void testIterable(KtIterable Function() 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(); + final int? max = iterable.maxOrNull(); + expect(max, null); + }); + }); + group("maxBy", () { test("gets max value", () { final iterable = iterableOf(["1", "3", "2"]); @@ -1432,6 +1458,32 @@ void testIterable(KtIterable Function() emptyIterable, }); }); + group("minOrNull", () { + test("gets min int value", () { + final KtIterable iterable = iterableOf([3, 1, 2]); + final int? min = iterable.minOrNull(); + expect(min, 1); + }); + + test("gets min double value", () { + final KtIterable 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(); + final int? min = iterable.minOrNull(); + expect(min, null); + }); + }); + group("minBy", () { test("gets min value", () { final iterable = iterableOf(["1", "3", "2"]);