Repository URL to install this package:
|
Version:
3.0.0 ▾
|
import { isNil, isObj, isArray, isString } from 'exotic'
// else if (component.identifier) {
// // @todo or @@identifier
// return component.identifier
// }
function toReactInstanceName(component) {
// react instance
let type = component
let name = toComponentName(component.type)
// console.warn('______________')
// console.log({ component, name })
// console.warn('______________')
// ' ^ ' + type.displayName
// while (type._owner) {
// type = type._owner
// recursion yay!
name += ' ^ ' + toComponentName(type._owner)
// }
// console.debug('______________')
// console.log({ component, name })
// return toComponentName(component.type)
return name
}
// eslint-disable-next-line
function toComponentName(
component: Object | Function,
fallback: String = 'no-name'
): String {
if (isNil(component)) {
// @@nil
return ''
} else if (!isObj(component)) {
return component
} else if (!Object.isExtensible(component) && isObj(component.type)) {
return toReactInstanceName(component)
} else if (isArray(component)) {
return component.map(toComponentName).join('-')
} else if (isString(component.displayName)) {
return component.displayName
} else if (
component.constructor.name === 'Function' ||
component.constructor.name === 'Object'
) {
return component.name || fallback
} else {
return component.constructor.name || fallback
}
}
export {
toComponentName,
toComponentName as getDisplayName,
toComponentName as getClassName,
toComponentName as getName,
}
export default toComponentName