Skip to content

Commit 9168bb3

Browse files
committed
Add function to retrieve all property descriptors
This is essentially a recursive form of Object.getOwnPropertyDescriptors which ignores Symbol-keyed properties. Intended for debugging only; this function is far from performant.
1 parent d441480 commit 9168bb3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

lib/objects.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,36 @@ function alphabetiseProperties(input, strictCase = false){
7575
}
7676

7777

78+
/**
79+
* Retrieve a descriptor for each property available on an object.
80+
*
81+
* Both inherited and non-enumerable properties are included.
82+
* The usual rules of prototypal inheritance apply; redefined
83+
* properties replace their inherited counterparts.
84+
*
85+
* @param {Object} subject
86+
* @return {Map} A list of property descriptors keyed by name.
87+
* @example getProperties(foo) == Map{"keys" => descriptors}
88+
*/
89+
function getProperties(subject){
90+
let object = subject;
91+
const refs = new WeakSet();
92+
const ancestry = [subject];
93+
while((object = Object.getPrototypeOf(object)) && !refs.has(object))
94+
ancestry.push(object);
95+
96+
const result = new Map();
97+
for(const obj of ancestry.reverse()){
98+
const names = Object.getOwnPropertyNames(obj);
99+
for(const name of names){
100+
const desc = Object.getOwnPropertyDescriptor(obj, name);
101+
result.set(name, desc);
102+
}
103+
}
104+
105+
return result;
106+
}
107+
78108

79109
/**
80110
* "Flatten" a (possibly nested) list of strings into a single-level array.

0 commit comments

Comments
 (0)