From 3bb2f7fe4cfb42695cbfac4cfaa15b72708963a1 Mon Sep 17 00:00:00 2001 From: Anirudh Varma Date: Tue, 30 Jun 2020 02:07:49 +0530 Subject: [PATCH] fix(pluck): operator breaks with null/undefined inputs. (#5524) * 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 --- spec/operators/pluck-spec.ts | 22 ++++++++++++++++++++++ src/internal/operators/pluck.ts | 4 ++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/spec/operators/pluck-spec.ts b/spec/operators/pluck-spec.ts index 2d710a9264..71ad0bcdb6 100644 --- a/spec/operators/pluck-spec.ts +++ b/spec/operators/pluck-spec.ts @@ -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); + }); }); diff --git a/src/internal/operators/pluck.ts b/src/internal/operators/pluck.ts index dbe050f25d..543f0d715a 100644 --- a/src/internal/operators/pluck.ts +++ b/src/internal/operators/pluck.ts @@ -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;