diff --git a/src/R3/Operators/WhereNotNull.cs b/src/R3/Operators/WhereNotNull.cs new file mode 100644 index 0000000..70b632c --- /dev/null +++ b/src/R3/Operators/WhereNotNull.cs @@ -0,0 +1,13 @@ +namespace R3; + +public static partial class ObservableExtensions +{ + public static Observable WhereNotNull(this Observable source) where TResult : class + { + return new WhereSelect( + source: source, + selector: static item => item!, + predicate: item => item is not null + ); + } +} diff --git a/tests/R3.Tests/OperatorTests/WhereNotNullTest.cs b/tests/R3.Tests/OperatorTests/WhereNotNullTest.cs new file mode 100644 index 0000000..5f8057d --- /dev/null +++ b/tests/R3.Tests/OperatorTests/WhereNotNullTest.cs @@ -0,0 +1,24 @@ +namespace R3.Tests.OperatorTests; + +public class WhereNotNullTest(ITestOutputHelper output) +{ + [Fact] + public void WhereNotNull() + { + var p = new Subject(); + + using var list = p.WhereNotNull().ToLiveList(); + + p.OnNext(null); + list.AssertEqual([]); + + p.OnNext("foo"); + list.AssertEqual(["foo"]); + + p.OnNext(null); + list.AssertEqual(["foo"]); + + p.OnNext("bar"); + list.AssertEqual(["foo", "bar"]); + } +}