Skip to content

Commit

Permalink
fix(last): Allow defaultValue of undefined.
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Mar 3, 2021
1 parent 4983760 commit ef3e721
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions spec/operators/last-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ describe('last', () => {
});
});

it('should allow undefined as a default value', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' -----a--a---a-| ');
const e1subs = ' ^-------------! ';
const expected = '--------------(U|)';

expectObservable(e1.pipe(last((value) => value === 'b', undefined))).toBe(expected, { U: undefined });
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});

it('should return last element matches with predicate', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' --a--b--a--b--| ');
Expand Down
2 changes: 1 addition & 1 deletion src/internal/operators/last.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ export function last<T, D>(
source.pipe(
predicate ? filter((v, i) => predicate(v, i, source)) : identity,
takeLast(1),
hasDefaultValue ? defaultIfEmpty<T, D>(defaultValue) : throwIfEmpty(() => new EmptyError())
hasDefaultValue ? defaultIfEmpty(defaultValue!) : throwIfEmpty(() => new EmptyError())
);
}

0 comments on commit ef3e721

Please sign in to comment.