Skip to content

Commit 3bb2f7f

Browse files
anirudhvarma12benlesh
authored andcommitted
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
1 parent a01c9cf commit 3bb2f7f

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

spec/operators/pluck-spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,26 @@ describe('pluck operator', () => {
176176
expectObservable(r, unsub).toBe(expected);
177177
expectSubscriptions(a.subscriptions).toBe(asubs);
178178
});
179+
180+
it('should support symbols', () => {
181+
const sym = Symbol('sym');
182+
183+
const a = cold('--x--|', {x: {[sym]: 'abc'}});
184+
const asubs = '^ !';
185+
const expected = '--y--|';
186+
187+
const r = a.pipe(pluck(sym));
188+
expectObservable(r).toBe(expected, {y: 'abc'});
189+
expectSubscriptions(a.subscriptions).toBe(asubs);
190+
});
191+
192+
it('should not break on null values', () => {
193+
const a = cold('--x--|', {x: null});
194+
const asubs = '^ !';
195+
const expected = '--y--|';
196+
197+
const r = a.pipe(pluck('prop'));
198+
expectObservable(r).toBe(expected, {y: undefined});
199+
expectSubscriptions(a.subscriptions).toBe(asubs);
200+
});
179201
});

src/internal/operators/pluck.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ function plucker(props: string[], length: number): (x: string) => any {
5656
const mapper = (x: string) => {
5757
let currentProp = x;
5858
for (let i = 0; i < length; i++) {
59-
const p = currentProp[props[i]];
60-
if (typeof p !== 'undefined') {
59+
const p = currentProp != null ? currentProp[props[i]] : undefined;
60+
if (p !== void 0) {
6161
currentProp = p;
6262
} else {
6363
return undefined;

0 commit comments

Comments
 (0)