@@ -17,7 +17,8 @@ type GetPath = {
1717} ;
1818
1919export const hasOwnProperty = ( object : Object , value : string ) =>
20- Object . prototype . hasOwnProperty . call ( object , value ) ;
20+ Object . prototype . hasOwnProperty . call ( object , value ) ||
21+ Object . prototype . hasOwnProperty . call ( object . constructor . prototype , value ) ;
2122
2223export const getPath = (
2324 object : Object ,
@@ -27,40 +28,45 @@ export const getPath = (
2728 propertyPath = propertyPath . split ( '.' ) ;
2829 }
2930
30- const lastProp = propertyPath . length === 1 ;
31-
3231 if ( propertyPath . length ) {
32+ const lastProp = propertyPath . length === 1 ;
3333 const prop = propertyPath [ 0 ] ;
3434 const newObject = object [ prop ] ;
35+
3536 if ( ! lastProp && ( newObject === null || newObject === undefined ) ) {
3637 // This is not the last prop in the chain. If we keep recursing it will
3738 // hit a `can't access property X of undefined | null`. At this point we
38- // know that the chain broken and we return right away.
39+ // know that the chain has broken and we can return right away.
3940 return {
4041 hasEndProp : false ,
4142 lastTraversedObject : object ,
4243 traversedPath : [ ] ,
4344 } ;
44- } else {
45- const result = getPath ( newObject , propertyPath . slice ( 1 ) ) ;
46- result . lastTraversedObject || ( result . lastTraversedObject = object ) ;
47- result . traversedPath . unshift ( prop ) ;
48- if ( propertyPath . length === 1 ) {
49- result . hasEndProp = hasOwnProperty ( object , prop ) ;
50- if ( ! result . hasEndProp ) {
51- delete result . value ;
52- result . traversedPath . shift ( ) ;
53- }
45+ }
46+
47+ const result = getPath ( newObject , propertyPath . slice ( 1 ) ) ;
48+
49+ if ( result . lastTraversedObject === null ) {
50+ result . lastTraversedObject = object ;
51+ }
52+
53+ result . traversedPath . unshift ( prop ) ;
54+
55+ if ( lastProp ) {
56+ result . hasEndProp = hasOwnProperty ( object , prop ) ;
57+ if ( ! result . hasEndProp ) {
58+ result . traversedPath . shift ( ) ;
5459 }
55- return result ;
5660 }
57- } else {
58- return {
59- lastTraversedObject : null ,
60- traversedPath : [ ] ,
61- value : object ,
62- } ;
61+
62+ return result ;
6363 }
64+
65+ return {
66+ lastTraversedObject : null ,
67+ traversedPath : [ ] ,
68+ value : object ,
69+ } ;
6470} ;
6571
6672// Strip properties from object that are not present in the subset. Useful for
0 commit comments