Skip to content

Commit

Permalink
Add function to retrieve all property descriptors
Browse files Browse the repository at this point in the history
This is essentially a recursive form of Object.getOwnPropertyDescriptors
which ignores Symbol-keyed properties. Intended for debugging only; this
function is far from performant.
  • Loading branch information
Alhadis committed Apr 29, 2017
1 parent d441480 commit 9168bb3
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@ function alphabetiseProperties(input, strictCase = false){
}


/**
* Retrieve a descriptor for each property available on an object.
*
* Both inherited and non-enumerable properties are included.
* The usual rules of prototypal inheritance apply; redefined
* properties replace their inherited counterparts.
*
* @param {Object} subject
* @return {Map} A list of property descriptors keyed by name.
* @example getProperties(foo) == Map{"keys" => descriptors}
*/
function getProperties(subject){
let object = subject;
const refs = new WeakSet();
const ancestry = [subject];
while((object = Object.getPrototypeOf(object)) && !refs.has(object))
ancestry.push(object);

const result = new Map();
for(const obj of ancestry.reverse()){
const names = Object.getOwnPropertyNames(obj);
for(const name of names){
const desc = Object.getOwnPropertyDescriptor(obj, name);
result.set(name, desc);
}
}

return result;
}


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

0 comments on commit 9168bb3

Please sign in to comment.