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 and benlesh committed Jun 29, 2020
1 parent a01c9cf commit 3bb2f7f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 22 additions & 0 deletions spec/operators/pluck-spec.ts
Expand Up @@ -176,4 +176,26 @@ describe('pluck operator', () => {
expectObservable(r, unsub).toBe(expected);
expectSubscriptions(a.subscriptions).toBe(asubs);
});

it('should support symbols', () => {
const sym = Symbol('sym');

const a = cold('--x--|', {x: {[sym]: 'abc'}});
const asubs = '^ !';
const expected = '--y--|';

const r = a.pipe(pluck(sym));
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);
});
});
4 changes: 2 additions & 2 deletions src/internal/operators/pluck.ts
Expand Up @@ -56,8 +56,8 @@ function plucker(props: string[], length: number): (x: string) => any {
const mapper = (x: string) => {
let currentProp = x;
for (let i = 0; i < length; i++) {
const p = currentProp[props[i]];
if (typeof p !== 'undefined') {
const p = currentProp != null ? currentProp[props[i]] : undefined;
if (p !== void 0) {
currentProp = p;
} else {
return undefined;
Expand Down

0 comments on commit 3bb2f7f

Please sign in to comment.