Commit 88606749 authored by Luan Nico's avatar Luan Nico 💬 Committed by Jochum van der Ploeg
Browse files

Add some Kotlin null accessors on lists, useful for null-safety

parent 2436eb72
Pipeline #262865596 passed with stage
in 2 minutes and 10 seconds
## 0.4.3
- Added `firstOrNull`, `singleOrNull`, `getOrNull` to Iterable.
## 0.4.2
- Added `range` based methods to `num` types: `downTo`, `to` and `until`.
......
......@@ -4,8 +4,11 @@ library collections;
import 'dart:collection';
part 'src/collections/associate.dart';
part 'src/collections/get_or_null.dart';
part 'src/collections/grouping_by.dart';
part 'src/collections/chunked.dart';
part 'src/collections/first_or_null.dart';
part 'src/collections/range_num.dart';
part 'src/collections/range.dart';
part 'src/collections/repeat.dart';
part 'src/collections/single_or_null.dart';
part of collections;
extension FirstOrNull<T> on Iterable<T> {
/// Returns the first element of the list or null if empty.
///
/// Optionally provide a test function; in that case it functions just
/// like firstWhere but returns null instead of error if no element
/// matches (or the list is empty).
///
/// The T response parameter here is optional (T?).
T firstOrNull([bool Function(T) test]) {
return firstWhere(test ?? (_) => true, orElse: () => null);
}
}
part of collections;
extension GetOrNull<T> on Iterable<T> {
/// Returns the element of position index of the list or null if index
/// is out of bounds.
///
/// Note: beware that using this on a non-List Iterable this will have to
/// iterate the list, similar to elementAt.
///
/// The T response parameter here is optional (T?).
T getOrNull(int index) {
if (index >= 0 && index < length) {
return elementAt(index);
}
return null;
}
}
part of collections;
extension SingleOrNull<T> on Iterable<T> {
/// Returns the single element of a `length = 1` list or null otherwise.
///
/// Optionally provide a test function; in that case it functions just
/// like singleWhere but returns null instead of error if no or more than
/// one element matches (or the list is empty).
///
/// The T response parameter here is optional (T?).
T singleOrNull([bool Function(T) test]) {
test ??= (_) => true;
T result;
var foundMatching = false;
for (final element in this) {
if (test(element)) {
if (foundMatching) {
// more than one match
return null;
}
result = element;
foundMatching = true;
}
}
return result;
}
}
name: dartlin
description: Dartlin is a helper library that provides readable methods with which you can write cleaner looking code.
version: 0.4.2
version: 0.4.3
homepage: https://gitlab.com/wolfenrain/dartlin
environment:
......
import 'package:flutter_test/flutter_test.dart';
import 'package:dartlin/collections.dart';
void main() {
test('Should return first or null when no test lambda is provided', () {
final list1 = [1, 2, 3];
expect(list1.firstOrNull(), 1);
final list2 = [10, 0];
expect(list2.firstOrNull(), 10);
final list3 = [];
expect(list3.firstOrNull(), null);
});
test('Should return first or null when a test lambda is provided', () {
final list1 = [1, 2, 3];
expect(list1.firstOrNull((e) => e.isEven), 2);
final list2 = [10, 0];
expect(list2.firstOrNull((e) => e.isOdd), null);
final list3 = <int>[];
expect(list3.firstOrNull((e) => e.isEven), null);
final list4 = [1, 3, 5, 2, 7, 10, 20];
expect(list4.firstOrNull((e) => e.isOdd), 1);
expect(list4.firstOrNull((e) => e.isEven), 2);
expect(list4.firstOrNull((e) => e.isEven && e.isOdd), null);
});
}
import 'package:flutter_test/flutter_test.dart';
import 'package:dartlin/collections.dart';
void main() {
test('Should return element or null for non-empty lists', () {
final list = [1, 2, 3];
expect(list.getOrNull(0), 1);
expect(list.getOrNull(1), 2);
expect(list.getOrNull(2), 3);
expect(list.getOrNull(-10), null);
expect(list.getOrNull(-1), null);
expect(list.getOrNull(3), null);
expect(list.getOrNull(500), null);
});
test('Should always return null for an empty list', () {
final list = [];
for (var i in range(-10, to: 10)) {
expect(list.getOrNull(i), null);
}
});
}
import 'package:flutter_test/flutter_test.dart';
import 'package:dartlin/collections.dart';
void main() {
test('Should return single or null when no test lambda is provided', () {
final list1 = [1, 2, 3];
expect(list1.singleOrNull(), null);
final list2 = [10];
expect(list2.singleOrNull(), 10);
final list3 = [];
expect(list3.singleOrNull(), null);
});
test('Should return single or null when test lambda is provided', () {
final list1 = [1, 2, 3];
expect(list1.singleOrNull((e) => e.isEven), 2);
final list2 = [10, 0];
expect(list2.singleOrNull((e) => e.isOdd), null);
expect(list2.singleOrNull((e) => e.isEven), null);
final list3 = <int>[];
expect(list3.singleOrNull((e) => e.isEven), null);
final list4 = [1, 3, 5, 2, 7, 10, 20];
expect(list4.singleOrNull((e) => e.isOdd), null);
expect(list4.singleOrNull((e) => e.isEven), null);
expect(list4.singleOrNull((e) => e.isEven && e.isOdd), null);
expect(list4.singleOrNull((e) => e == 10), 10);
});
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment