Skip to content

Commit

Permalink
add firstNotNullOfOrNull
Browse files Browse the repository at this point in the history
  • Loading branch information
JarvanMo committed Dec 11, 2023
1 parent 22bc8ee commit f13a1fb
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 23 deletions.
1 change: 0 additions & 1 deletion .idea/kart.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 21 additions & 19 deletions .idea/libraries/Dart_SDK.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 33 additions & 2 deletions lib/src/foundation/kt/collections/iterables.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'package:kart/kart.dart';

class IndexedValue<T> {
int index;
T value;
Expand Down Expand Up @@ -132,6 +130,39 @@ extension GetElementsForIterable<E> on Iterable<E> {
return null;
}
}

/// Returns the first non-null value produced by [transform] function being applied to elements of this
/// collection in iteration order, or null if no non-null value was produced.
/// Example:
/// ```dart
/// Iterable<String> iterables = ["Ada", "John", "James", "Linda"];
/// var result = iterables.firstNotNullOfOrNull(transform: (e) {
/// if (e == "James") {
/// return 1;
/// } else {
/// return null;
/// }
/// }); // result is 1
///
/// int? result = iterables.firstNotNullOfOrNull(transform: (e) {
/// if (e == "Arthur") {
/// return 2;
/// } else {
/// return null;
/// }
/// }); //result is null
///
R? firstNotNullOfOrNull<R extends Object?>(
{required R? Function(E e) transform}) {
for (var element in this) {
var result = transform(element);
if (result != null) {
return result;
}
}

return null;
}
}

extension MapForIterable<E> on Iterable<E> {
Expand Down
32 changes: 32 additions & 0 deletions test/foundation/kt/collections/iterables_get_elements_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:ffi';

import 'package:kart/kart.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -27,4 +29,34 @@ void main() {
expect(result, 2);
});
});

group("firstNotNullOfOrNull", () {
test("non-null value was produced | return 1", () {
Iterable<String> iterables = ["Ada", "John", "James", "Linda"];

var result = iterables.firstNotNullOfOrNull(transform: (e) {
if (e == "James") {
return 1;
} else {
return null;
}
});

expect(result, 1);
});

test("no non-null value was produced | return null", () {
Iterable<String> iterables = ["Ada", "John", "James", "Linda"];

int? result = iterables.firstNotNullOfOrNull(transform: (e) {
if (e == "Arthur") {
return null;
} else {
return null;
}
});

expect(result, null);
});
});
}

0 comments on commit f13a1fb

Please sign in to comment.