From c5f65508505cf1f90560e6be76425e09c455bec3 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 | 10 ++++++++++ src/internal/operators/pluck.ts | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/spec/operators/pluck-spec.ts b/spec/operators/pluck-spec.ts index 616b2aae73..d7bdaaddb9 100644 --- a/spec/operators/pluck-spec.ts +++ b/spec/operators/pluck-spec.ts @@ -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); + }); }); diff --git a/src/internal/operators/pluck.ts b/src/internal/operators/pluck.ts index d566529e9b..96733e5a53 100644 --- a/src/internal/operators/pluck.ts +++ b/src/internal/operators/pluck.ts @@ -51,7 +51,7 @@ export function pluck(...properties: Array): 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 {