Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Remove IterableExtensions that have exact equivalents in the SDK
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oprypin committed Jan 25, 2024
1 parent 2d57a82 commit d85ae60
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 112 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
39 changes: 0 additions & 39 deletions lib/src/iterable_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,6 @@ extension IterableExtension<T> on Iterable<T> {
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;
Expand All @@ -297,12 +290,6 @@ extension IterableExtension<T> on Iterable<T> {
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
Expand Down Expand Up @@ -348,32 +335,6 @@ extension IterableExtension<T> on Iterable<T> {
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
Expand Down
11 changes: 0 additions & 11 deletions lib/src/list_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,6 @@ extension ListExtensions<E> on List<E> {
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
Expand Down
62 changes: 0 additions & 62 deletions test/extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 _) {}), {});
Expand Down Expand Up @@ -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<RangeError>()));
});
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(<int>[]).slices(1), []);
Expand Down Expand Up @@ -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<RangeError>()));
});
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(<int>[].slices(1), []);
Expand Down

0 comments on commit d85ae60

Please sign in to comment.