Skip to content

Commit

Permalink
Merge pull request #243 from erri120/where-not-null
Browse files Browse the repository at this point in the history
Add `WhereNotNull` operator
  • Loading branch information
neuecc authored Aug 13, 2024
2 parents 5767749 + 3f16e31 commit cd2d5e8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/R3/Operators/WhereNotNull.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace R3;

public static partial class ObservableExtensions
{
public static Observable<TResult> WhereNotNull<TResult>(this Observable<TResult?> source) where TResult : class
{
return new WhereSelect<TResult?, TResult>(
source: source,
selector: static item => item!,
predicate: item => item is not null
);
}
}
24 changes: 24 additions & 0 deletions tests/R3.Tests/OperatorTests/WhereNotNullTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace R3.Tests.OperatorTests;

public class WhereNotNullTest(ITestOutputHelper output)
{
[Fact]
public void WhereNotNull()
{
var p = new Subject<string?>();

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"]);
}
}

0 comments on commit cd2d5e8

Please sign in to comment.