-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (26 loc) · 872 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'use strict'
const isObject = require('is-object')
/**
* Returns an object’s class and its ancestors.
* @param {object|function} obj The object or the first class in the chain.
* @return {array.<function>} The class and the chain of classes it extends.
*/
function getClassChain (obj) {
if (typeof obj === 'function') {
if (!obj.name || obj === Object) return []
return [obj].concat(getClassChain(Object.getPrototypeOf(obj)))
}
if (isObject(obj)) {
return getClassChain(obj.constructor)
}
return []
}
/**
* Returns the names of an object’s class and its ancestors.
* @param {object|function} obj The object or the first class in the chain.
* @return {array.<string>} The names of the classes in the chain.
*/
getClassChain.names = function (obj) {
return getClassChain(obj).map(cls => cls.name)
}
module.exports = getClassChain