mirror of
https://github.com/zebrajr/react.git
synced 2025-12-06 12:20:20 +01:00
add isNode to react-is
This commit is contained in:
parent
3297102de6
commit
efe8e670af
17
packages/react-is/src/ReactIs.js
vendored
17
packages/react-is/src/ReactIs.js
vendored
|
|
@ -90,6 +90,23 @@ export function isForwardRef(object: any) {
|
|||
export function isFragment(object: any) {
|
||||
return typeOf(object) === REACT_FRAGMENT_TYPE;
|
||||
}
|
||||
export function isNode(object: any) {
|
||||
switch (typeof object) {
|
||||
case 'number':
|
||||
case 'string':
|
||||
case 'undefined':
|
||||
return true;
|
||||
case 'boolean':
|
||||
return !object;
|
||||
case 'object':
|
||||
if (Array.isArray(object)) {
|
||||
return object.every(isNode);
|
||||
}
|
||||
return object === null || isElement(object);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
export function isProfiler(object: any) {
|
||||
return typeOf(object) === REACT_PROFILER_TYPE;
|
||||
}
|
||||
|
|
|
|||
20
packages/react-is/src/__tests__/ReactIs-test.js
vendored
20
packages/react-is/src/__tests__/ReactIs-test.js
vendored
|
|
@ -85,6 +85,26 @@ describe('ReactIs', () => {
|
|||
expect(ReactIs.isContextConsumer(<div />)).toBe(false);
|
||||
});
|
||||
|
||||
it('should identify nodes', () => {
|
||||
expect(ReactIs.isNode(<div />)).toBe(true);
|
||||
expect(ReactIs.isNode('div')).toBe(true);
|
||||
expect(ReactIs.isNode(false)).toBe(true);
|
||||
expect(ReactIs.isNode(true)).toBe(false);
|
||||
expect(ReactIs.isNode(123)).toBe(true);
|
||||
expect(ReactIs.isNode(null)).toBe(true);
|
||||
expect(ReactIs.isNode(undefined)).toBe(true);
|
||||
expect(ReactIs.isNode([1, 'string', <div />])).toBe(true);
|
||||
expect(ReactIs.isNode({})).toBe(false);
|
||||
|
||||
// It should also identify more specific types as nodes
|
||||
const Context = React.createContext(false);
|
||||
expect(ReactIs.isNode(<Context.Provider />)).toBe(true);
|
||||
expect(ReactIs.isNode(<Context.Consumer />)).toBe(true);
|
||||
expect(ReactIs.isNode(<React.Fragment />)).toBe(true);
|
||||
expect(ReactIs.isNode(<React.unstable_AsyncMode />)).toBe(true);
|
||||
expect(ReactIs.isNode(<React.StrictMode />)).toBe(true);
|
||||
});
|
||||
|
||||
it('should identify context providers', () => {
|
||||
const Context = React.createContext(false);
|
||||
expect(ReactIs.typeOf(<Context.Provider />)).toBe(ReactIs.ContextProvider);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user