Skip to content

Commit

Permalink
fix(pluck): operator breaks with null/undefined inputs. (#5524)
Browse files Browse the repository at this point in the history
* test(pluck): add failing test case for using null value.

* fix(pluck): check for null/undefined object before attempting to access prop

* Remove null coalescing when checking values to prevent null values being converted to undefined
  • Loading branch information
anirudhvarma12 authored Jun 29, 2020
1 parent 289d5e8 commit c5f6550
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions spec/operators/pluck-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,14 @@ describe('pluck operator', () => {
expectObservable(r).toBe(expected, {y: 'abc'});
expectSubscriptions(a.subscriptions).toBe(asubs);
});

it('should not break on null values', () => {
const a = cold('--x--|', {x: null});
const asubs = '^ !';
const expected = '--y--|';

const r = a.pipe(pluck('prop'));
expectObservable(r).toBe(expected, {y: undefined});
expectSubscriptions(a.subscriptions).toBe(asubs);
});
});
2 changes: 1 addition & 1 deletion src/internal/operators/pluck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function pluck<T, R>(...properties: Array<string | number | symbol>): Ope
return map((x) => {
let currentProp: any = x;
for (let i = 0; i < length; i++) {
const p = currentProp[properties[i]];
const p = currentProp?.[properties[i]];
if (typeof p !== 'undefined') {
currentProp = p;
} else {
Expand Down

0 comments on commit c5f6550

Please sign in to comment.