-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
51 lines (40 loc) · 1.29 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var React = require('react');
const FUNCTION_REGEX = /react(\d+)?./i;
function classComponent(component) {
return (
typeof component === 'function' && component.prototype && !!component.prototype.isReactComponent
);
}
// Ensure compatability with transformed code
function functionComponent(component) {
return (
typeof component === 'function' &&
String(component).includes('return') &&
!!String(component).match(FUNCTION_REGEX) &&
String(component).includes('.createElement')
);
}
function component(component) {
return classComponent(component) || functionComponent(component);
}
function element(typeElement) {
return React.isValidElement(typeElement);
}
function DOMTypeElement(typeElement) {
return element(typeElement) && typeof typeElement.type === 'string';
}
function compositeTypeElement(typeElement) {
return element(typeElement) && typeof typeElement.type === 'function';
}
function compatible(item) {
return element(item) || component(item);
}
var isReact = {};
isReact.classComponent = classComponent;
isReact.functionComponent = functionComponent;
isReact.component = component;
isReact.element = element;
isReact.DOMTypeElement = DOMTypeElement;
isReact.compositeTypeElement = compositeTypeElement;
isReact.compatible = compatible;
module.exports = isReact;