From d85ae604d4649e5e324196828baf26397ce7d549 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Thu, 25 Jan 2024 21:21:11 +0100 Subject: [PATCH] Remove IterableExtensions that have exact equivalents in the SDK Since Dart 3.0, these extensions are in the SDK, exported from core: firstOrNull, lastOrNull, singleOrNull, elementAtOrNull This package already bumped the version to be above that. So, the deletion will not cause any breakage to users, only potential "unused import" warnings. --- CHANGELOG.md | 3 ++ lib/src/iterable_extensions.dart | 39 -------------------- lib/src/list_extensions.dart | 11 ------ test/extensions_test.dart | 62 -------------------------------- 4 files changed, 3 insertions(+), 112 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e38e1a8..e4c5ddd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ - Shuffle `IterableExtension.sample` results. - Fix `mergeSort` when the runtime iterable generic is a subtype of the static generic. +- Remove `firstOrNull`, `lastOrNull`, `singleOrNull` and `elementAtOrNull()` + from `IterableExtensions`. Since Dart 3.0, exact equivalents to these are + available in Dart core, so 'package:collection' would only be shadowing these. - Require Dart `^3.1.0` - Mark "mixin" classes as `mixin`. diff --git a/lib/src/iterable_extensions.dart b/lib/src/iterable_extensions.dart index 1bf4b3e..5def483 100644 --- a/lib/src/iterable_extensions.dart +++ b/lib/src/iterable_extensions.dart @@ -269,13 +269,6 @@ extension IterableExtension on Iterable { return null; } - /// The first element, or `null` if the iterable is empty. - T? get firstOrNull { - var iterator = this.iterator; - if (iterator.moveNext()) return iterator.current; - return null; - } - /// The last element satisfying [test], or `null` if there are none. T? lastWhereOrNull(bool Function(T element) test) { T? result; @@ -297,12 +290,6 @@ extension IterableExtension on Iterable { return result; } - /// The last element, or `null` if the iterable is empty. - T? get lastOrNull { - if (isEmpty) return null; - return last; - } - /// The single element satisfying [test]. /// /// Returns `null` if there are either no elements @@ -348,32 +335,6 @@ extension IterableExtension on Iterable { return result; } - /// The single element of the iterable, or `null`. - /// - /// The value is `null` if the iterable is empty - /// or it contains more than one element. - T? get singleOrNull { - var iterator = this.iterator; - if (iterator.moveNext()) { - var result = iterator.current; - if (!iterator.moveNext()) { - return result; - } - } - return null; - } - - /// The [index]th element, or `null` if there is no such element. - /// - /// Returns the element at position [index] of this iterable, - /// just like [elementAt], if this iterable has such an element. - /// If this iterable does not have enough elements to have one with the given - /// [index], the `null` value is returned, unlike [elementAt] which throws - /// instead. - /// - /// The [index] must not be negative. - T? elementAtOrNull(int index) => skip(index).firstOrNull; - /// Associates the elements in `this` by the value returned by [key]. /// /// Returns a map from keys computed by [key] to the last value for which diff --git a/lib/src/list_extensions.dart b/lib/src/list_extensions.dart index 40fa8af..570be5d 100644 --- a/lib/src/list_extensions.dart +++ b/lib/src/list_extensions.dart @@ -259,17 +259,6 @@ extension ListExtensions on List { return true; } - /// The [index]th element, or `null` if there is no such element. - /// - /// Returns the element at position [index] of this list, - /// just like [elementAt], if this list has such an element. - /// If this list does not have enough elements to have one with the given - /// [index], the `null` value is returned, unlike [elementAt] which throws - /// instead. - /// - /// The [index] must not be negative. - E? elementAtOrNull(int index) => (index < length) ? this[index] : null; - /// Contiguous [slice]s of `this` with the given [length]. /// /// Each slice is a view of this list [length] elements long, except for the diff --git a/test/extensions_test.dart b/test/extensions_test.dart index 3b1401a..e235f5e 100644 --- a/test/extensions_test.dart +++ b/test/extensions_test.dart @@ -426,17 +426,6 @@ void main() { 0); }); }); - group('.firstOrNull', () { - test('empty', () { - expect(iterable([]).firstOrNull, null); - }); - test('single', () { - expect(iterable([1]).firstOrNull, 1); - }); - test('first of multiple', () { - expect(iterable([1, 3, 5]).firstOrNull, 1); - }); - }); group('.lastWhereOrNull', () { test('empty', () { expect(iterable([]).lastWhereOrNull(unreachable), null); @@ -474,17 +463,6 @@ void main() { 7); }); }); - group('.lastOrNull', () { - test('empty', () { - expect(iterable([]).lastOrNull, null); - }); - test('single', () { - expect(iterable([1]).lastOrNull, 1); - }); - test('last of multiple', () { - expect(iterable([1, 3, 5]).lastOrNull, 5); - }); - }); group('.singleWhereOrNull', () { test('empty', () { expect(iterable([]).singleWhereOrNull(unreachable), null); @@ -526,17 +504,6 @@ void main() { null); }); }); - group('.singleOrNull', () { - test('empty', () { - expect(iterable([]).singleOrNull, null); - }); - test('single', () { - expect(iterable([1]).singleOrNull, 1); - }); - test('multiple', () { - expect(iterable([1, 3, 5]).singleOrNull, null); - }); - }); group('.lastBy', () { test('empty', () { expect(iterable([]).lastBy((dynamic _) {}), {}); @@ -1204,21 +1171,6 @@ void main() { }); }); }); - group('.elementAtOrNull', () { - test('empty', () async { - expect(iterable([]).elementAtOrNull(0), isNull); - }); - test('negative index', () async { - expect(() => iterable([1]).elementAtOrNull(-1), - throwsA(isA())); - }); - test('index within range', () async { - expect(iterable([1]).elementAtOrNull(0), 1); - }); - test('index too high', () async { - expect(iterable([1]).elementAtOrNull(1), isNull); - }); - }); group('.slices', () { test('empty', () { expect(iterable([]).slices(1), []); @@ -1820,20 +1772,6 @@ void main() { ['1', 'b']); }); }); - group('.elementAtOrNull', () { - test('empty', () async { - expect([].elementAtOrNull(0), isNull); - }); - test('negative index', () async { - expect(() => [1].elementAtOrNull(-1), throwsA(isA())); - }); - test('index within range', () async { - expect([1].elementAtOrNull(0), 1); - }); - test('index too high', () async { - expect([1].elementAtOrNull(1), isNull); - }); - }); group('.slices', () { test('empty', () { expect([].slices(1), []);