@@ -21,33 +21,23 @@ type GetPath = {
2121 value ?: unknown ;
2222} ;
2323
24- // Return whether object instance inherits getter from its class.
25- const hasGetterFromConstructor = ( object : object , key : string ) => {
26- const constructor = object . constructor ;
27- if ( constructor === Object ) {
28- // A literal object has Object as constructor.
29- // Therefore, it cannot inherit application-specific getters.
30- // Furthermore, Object has __proto__ getter which is not relevant.
31- // Array, Boolean, Number, String constructors don’t have any getters.
32- return false ;
33- }
34- if ( typeof constructor !== 'function' ) {
35- // Object.create(null) constructs object with no constructor nor prototype.
36- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create#Custom_and_Null_objects
24+ /**
25+ * Checks if `hasOwnProperty(object, key)` up the prototype chain, stopping at `Object.prototype`.
26+ */
27+ const hasPropertyInObject = ( object : object , key : string ) : boolean => {
28+ const shouldTerminate =
29+ ! object || typeof object !== 'object' || object === Object . prototype ;
30+
31+ if ( shouldTerminate ) {
3732 return false ;
3833 }
3934
40- const descriptor = Object . getOwnPropertyDescriptor (
41- constructor . prototype ,
42- key ,
35+ return (
36+ Object . prototype . hasOwnProperty . call ( object , key ) ||
37+ hasPropertyInObject ( Object . getPrototypeOf ( object ) , key )
4338 ) ;
44- return descriptor !== undefined && typeof descriptor . get === 'function' ;
4539} ;
4640
47- export const hasOwnProperty = ( object : object , key : string ) : boolean =>
48- Object . prototype . hasOwnProperty . call ( object , key ) ||
49- hasGetterFromConstructor ( object , key ) ;
50-
5141export const getPath = (
5242 object : Record < string , any > ,
5343 propertyPath : string | Array < string > ,
@@ -129,7 +119,7 @@ export const getObjectSubset = (
129119 seenReferences . set ( object , trimmed ) ;
130120
131121 Object . keys ( object )
132- . filter ( key => hasOwnProperty ( subset , key ) )
122+ . filter ( key => hasPropertyInObject ( subset , key ) )
133123 . forEach ( key => {
134124 trimmed [ key ] = seenReferences . has ( object [ key ] )
135125 ? seenReferences . get ( object [ key ] )
@@ -299,7 +289,7 @@ export const subsetEquality = (
299289 }
300290 const result =
301291 object != null &&
302- hasOwnProperty ( object , key ) &&
292+ hasPropertyInObject ( object , key ) &&
303293 equals ( object [ key ] , subset [ key ] , [
304294 iterableEquality ,
305295 subsetEqualityWithContext ( seenReferences ) ,
0 commit comments