|
| 1 | +const hasSymbol = typeof Symbol === 'function' && Symbol.for |
| 2 | + |
| 3 | +export const REACT_ELEMENT_TYPE = hasSymbol ? Symbol.for('react.element') : 0xeac7 |
| 4 | +export const REACT_PORTAL_TYPE = hasSymbol ? Symbol.for('react.portal') : 0xeaca |
| 5 | +export const REACT_FRAGMENT_TYPE = hasSymbol ? Symbol.for('react.fragment') : 0xeacb |
| 6 | +export const REACT_STRICT_MODE_TYPE = hasSymbol ? Symbol.for('react.strict_mode') : 0xeacc |
| 7 | +export const REACT_PROFILER_TYPE = hasSymbol ? Symbol.for('react.profiler') : 0xead2 |
| 8 | +export const REACT_PROVIDER_TYPE = hasSymbol ? Symbol.for('react.provider') : 0xeacd |
| 9 | +export const REACT_CONTEXT_TYPE = hasSymbol ? Symbol.for('react.context') : 0xeace |
| 10 | +export const REACT_FORWARD_REF_TYPE = hasSymbol ? Symbol.for('react.forward_ref') : 0xead0 |
| 11 | +export const REACT_SUSPENSE_TYPE = hasSymbol ? Symbol.for('react.suspense') : 0xead1 |
| 12 | +export const REACT_SUSPENSE_LIST_TYPE = hasSymbol ? Symbol.for('react.suspense_list') : 0xead8 |
| 13 | +export const REACT_MEMO_TYPE = hasSymbol ? Symbol.for('react.memo') : 0xead3 |
| 14 | +export const REACT_LAZY_TYPE = hasSymbol ? Symbol.for('react.lazy') : 0xead4 |
| 15 | +export const REACT_SCOPE_TYPE = hasSymbol ? Symbol.for('react.scope') : 0xead7 |
| 16 | + |
| 17 | +export function typeOf(object: any) { |
| 18 | + if (typeof object === 'object' && object !== null) { |
| 19 | + const { $$typeof } = object |
| 20 | + switch ($$typeof) { |
| 21 | + case REACT_ELEMENT_TYPE: { |
| 22 | + const { type } = object |
| 23 | + switch (type) { |
| 24 | + case REACT_FRAGMENT_TYPE: |
| 25 | + case REACT_PROFILER_TYPE: |
| 26 | + case REACT_STRICT_MODE_TYPE: |
| 27 | + case REACT_SUSPENSE_TYPE: |
| 28 | + return type |
| 29 | + default: { |
| 30 | + const typeOfType = type && type.$$typeof |
| 31 | + switch (typeOfType) { |
| 32 | + case REACT_CONTEXT_TYPE: |
| 33 | + case REACT_FORWARD_REF_TYPE: |
| 34 | + case REACT_PROVIDER_TYPE: |
| 35 | + case REACT_MEMO_TYPE: |
| 36 | + case REACT_LAZY_TYPE: |
| 37 | + return typeOfType |
| 38 | + default: |
| 39 | + return $$typeof |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + case REACT_PORTAL_TYPE: |
| 45 | + return $$typeof |
| 46 | + default: |
| 47 | + return $$typeof |
| 48 | + } |
| 49 | + } |
| 50 | + return undefined |
| 51 | +} |
| 52 | + |
| 53 | +export const AsyncMode = 0xead5 |
| 54 | +export const ConcurrentMode = 0xead5 |
| 55 | +export const ContextConsumer = REACT_CONTEXT_TYPE |
| 56 | +export const ContextProvider = REACT_PROVIDER_TYPE |
| 57 | +export const Element = REACT_ELEMENT_TYPE |
| 58 | +export const ForwardRef = REACT_FORWARD_REF_TYPE |
| 59 | +export const Fragment = REACT_FRAGMENT_TYPE |
| 60 | +export const Lazy = REACT_LAZY_TYPE |
| 61 | +export const Memo = REACT_MEMO_TYPE |
| 62 | +export const Portal = REACT_PORTAL_TYPE |
| 63 | +export const Profiler = REACT_PROFILER_TYPE |
| 64 | +export const StrictMode = REACT_STRICT_MODE_TYPE |
| 65 | +export const Suspense = REACT_SUSPENSE_TYPE |
| 66 | + |
| 67 | +export function isValidElementType(type: any) { |
| 68 | + return ( |
| 69 | + typeof type === 'string' || |
| 70 | + typeof type === 'function' || |
| 71 | + type === REACT_FRAGMENT_TYPE || |
| 72 | + type === REACT_PROFILER_TYPE || |
| 73 | + type === REACT_STRICT_MODE_TYPE || |
| 74 | + type === REACT_SUSPENSE_TYPE || |
| 75 | + type === REACT_SUSPENSE_LIST_TYPE || |
| 76 | + (typeof type === 'object' && |
| 77 | + type !== null && |
| 78 | + (type.$$typeof === REACT_LAZY_TYPE || |
| 79 | + type.$$typeof === REACT_MEMO_TYPE || |
| 80 | + type.$$typeof === REACT_PROVIDER_TYPE || |
| 81 | + type.$$typeof === REACT_CONTEXT_TYPE || |
| 82 | + type.$$typeof === REACT_FORWARD_REF_TYPE || |
| 83 | + type.$$typeof === REACT_SCOPE_TYPE)) |
| 84 | + ) |
| 85 | +} |
| 86 | + |
| 87 | +export function isElement(object: any) { |
| 88 | + return typeof object === 'object' && object !== null && object.$$typeof === REACT_ELEMENT_TYPE |
| 89 | +} |
| 90 | + |
| 91 | +export function isFragment(object: any) { |
| 92 | + return typeOf(object) === REACT_FRAGMENT_TYPE |
| 93 | +} |
| 94 | + |
| 95 | +export function isMemo(object: any) { |
| 96 | + return typeOf(object) === REACT_MEMO_TYPE |
| 97 | +} |
| 98 | + |
| 99 | +export function isLazy(object: any) { |
| 100 | + return typeOf(object) === REACT_LAZY_TYPE |
| 101 | +} |
| 102 | + |
| 103 | +export function isForwardRef(object: any) { |
| 104 | + return typeOf(object) === REACT_FORWARD_REF_TYPE |
| 105 | +} |
| 106 | + |
| 107 | +export function isContextConsumer(object: any) { |
| 108 | + return typeOf(object) === REACT_CONTEXT_TYPE |
| 109 | +} |
| 110 | + |
| 111 | +export function isContextProvider(object: any) { |
| 112 | + return typeOf(object) === REACT_PROVIDER_TYPE |
| 113 | +} |
| 114 | + |
| 115 | +export function isPortal(object: any) { |
| 116 | + return typeOf(object) === REACT_PORTAL_TYPE |
| 117 | +} |
| 118 | + |
| 119 | +export function isProfiler(object: any) { |
| 120 | + return typeOf(object) === REACT_PROFILER_TYPE |
| 121 | +} |
| 122 | + |
| 123 | +export function isStrictMode(object: any) { |
| 124 | + return typeOf(object) === REACT_STRICT_MODE_TYPE |
| 125 | +} |
| 126 | + |
| 127 | +export function isSuspense(object: any) { |
| 128 | + return typeOf(object) === REACT_SUSPENSE_TYPE |
| 129 | +} |
0 commit comments