Skip to content

Commit 88d1dd4

Browse files
committed
typeOf returns symbols; symbols exported for switch/case
1 parent cbffbd1 commit 88d1dd4

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

packages/react-is/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {isAsyncMode, typeOf} from 'react-is';
1111

1212
const AsyncMode = React.unstable_AsyncMode;
1313

14-
typeOf(<AsyncMode />); // "ReactAsyncMode"
14+
typeOf(<AsyncMode />); // ReactIs.AsyncMode
1515

1616
isAsyncMode(<AsyncMode />); // true
1717
```
@@ -23,8 +23,8 @@ import {isContextConsumer, isContextProvider, typeOf} from 'react-is';
2323

2424
const ThemeContext = React.createContext('blue');
2525

26-
typeOf(<ThemeContext.Provider />); // "ReactContextProvider"
27-
typeOf(<ThemeContext.Consumer />); // "ReactContextConsumer"
26+
typeOf(<ThemeContext.Provider />); // ReactIs.ContextProvider
27+
typeOf(<ThemeContext.Consumer />); // ReactIs.ContextConsumer
2828

2929
isContextConsumer(<ThemeContext.Consumer />); // true
3030
isContextProvider(<ThemeContext.Provider />); // true
@@ -35,7 +35,7 @@ isContextProvider(<ThemeContext.Provider />); // true
3535
import React from 'react';
3636
import {isElement, typeOf} from 'react-is';
3737

38-
typeOf(<div />); // "ReactElement"
38+
typeOf(<div />); // ReactIs.Element
3939

4040
isElement(<div />); // true
4141
```
@@ -45,7 +45,7 @@ isElement(<div />); // true
4545
import React from 'react';
4646
import {isFragment, typeOf} from 'react-is';
4747

48-
typeOf(<></>); // "ReactFragment"
48+
typeOf(<></>); // ReactIs.Fragment
4949

5050
isFragment(<></>); // true
5151
```
@@ -59,7 +59,7 @@ import {isPortal, typeOf} from 'react-is';
5959
const div = document.createElement('div');
6060
const portal = createPortal(<div />, div);
6161

62-
typeOf(portal); // "ReactPortal"
62+
typeOf(portal); // ReactIs.Portal
6363

6464
isPortal(portal); // true
6565
```
@@ -69,7 +69,7 @@ isPortal(portal); // true
6969
import React from 'react';
7070
import {isStrictMode, typeOf} from 'react-is';
7171

72-
typeOf(<React.StrictMode />); // "ReactStrictMode"
72+
typeOf(<React.StrictMode />); // ReactIs.StrictMode
7373

7474
isStrictMode(<React.StrictMode />); // true
7575
```

packages/react-is/src/ReactIs.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,37 @@ function getTypeTypeOf(object: any) {
4141

4242
const ReactIs = {
4343
typeOf(object: any) {
44-
switch (getType(object)) {
44+
const type = getType(object);
45+
switch (type) {
4546
case REACT_ASYNC_MODE_TYPE:
46-
return 'ReactAsyncMode';
4747
case REACT_FRAGMENT_TYPE:
48-
return 'ReactFragment';
4948
case REACT_STRICT_MODE_TYPE:
50-
return 'ReactStrictMode';
49+
return type;
5150
}
5251

53-
switch (getTypeTypeOf(object)) {
52+
const typeTypeOf = getTypeTypeOf(object);
53+
switch (typeTypeOf) {
5454
case REACT_CONTEXT_TYPE:
55-
return 'ReactContextConsumer';
5655
case REACT_PROVIDER_TYPE:
57-
return 'ReactContextProvider';
56+
return typeTypeOf;
5857
}
5958

60-
switch (getTypeOf(object)) {
59+
const typeOf = getTypeOf(object);
60+
switch (typeOf) {
6161
case REACT_ELEMENT_TYPE:
62-
return 'ReactElement';
6362
case REACT_PORTAL_TYPE:
64-
return 'ReactPortal';
63+
return typeOf;
6564
}
6665
},
66+
67+
AsyncMode: REACT_ASYNC_MODE_TYPE,
68+
ContextConsumer: REACT_CONTEXT_TYPE,
69+
ContextProvider: REACT_PROVIDER_TYPE,
70+
Element: REACT_ELEMENT_TYPE,
71+
Fragment: REACT_FRAGMENT_TYPE,
72+
Portal: REACT_PORTAL_TYPE,
73+
StrictMode: REACT_STRICT_MODE_TYPE,
74+
6775
isAsyncMode(object: any) {
6876
return getType(object) === REACT_ASYNC_MODE_TYPE;
6977
},

packages/react-is/src/__tests__/ReactIs-test.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,40 @@ describe('ReactIs', () => {
2222
});
2323

2424
it('should identify async mode', () => {
25-
expect(ReactIs.typeOf(<React.unstable_AsyncMode />)).toBe('ReactAsyncMode');
25+
expect(ReactIs.typeOf(<React.unstable_AsyncMode />)).toBe(
26+
ReactIs.AsyncMode,
27+
);
2628
expect(ReactIs.isAsyncMode(<React.unstable_AsyncMode />)).toBe(true);
2729
expect(ReactIs.isAsyncMode(<React.StrictMode />)).toBe(false);
2830
expect(ReactIs.isAsyncMode(<div />)).toBe(false);
2931
});
3032

3133
it('should identify context consumers', () => {
3234
const Context = React.createContext(false);
33-
expect(ReactIs.typeOf(<Context.Consumer />)).toBe('ReactContextConsumer');
35+
expect(ReactIs.typeOf(<Context.Consumer />)).toBe(ReactIs.ContextConsumer);
3436
expect(ReactIs.isContextConsumer(<Context.Consumer />)).toBe(true);
3537
expect(ReactIs.isContextConsumer(<Context.Provider />)).toBe(false);
3638
expect(ReactIs.isContextConsumer(<div />)).toBe(false);
3739
});
3840

3941
it('should identify context providers', () => {
4042
const Context = React.createContext(false);
41-
expect(ReactIs.typeOf(<Context.Provider />)).toBe('ReactContextProvider');
43+
expect(ReactIs.typeOf(<Context.Provider />)).toBe(ReactIs.ContextProvider);
4244
expect(ReactIs.isContextProvider(<Context.Provider />)).toBe(true);
4345
expect(ReactIs.isContextProvider(<Context.Consumer />)).toBe(false);
4446
expect(ReactIs.isContextProvider(<div />)).toBe(false);
4547
});
4648

4749
it('should identify elements', () => {
48-
expect(ReactIs.typeOf(<div />)).toBe('ReactElement');
50+
expect(ReactIs.typeOf(<div />)).toBe(ReactIs.Element);
4951
expect(ReactIs.isElement(<div />)).toBe(true);
5052
expect(ReactIs.isElement('div')).toBe(false);
5153
expect(ReactIs.isElement(true)).toBe(false);
5254
expect(ReactIs.isElement(123)).toBe(false);
5355
});
5456

5557
it('should identify fragments', () => {
56-
expect(ReactIs.typeOf(<React.Fragment />)).toBe('ReactFragment');
58+
expect(ReactIs.typeOf(<React.Fragment />)).toBe(ReactIs.Fragment);
5759
expect(ReactIs.isFragment(<React.Fragment />)).toBe(true);
5860
expect(ReactIs.isFragment('React.Fragment')).toBe(false);
5961
expect(ReactIs.isFragment(<div />)).toBe(false);
@@ -63,13 +65,13 @@ describe('ReactIs', () => {
6365
it('should identify portals', () => {
6466
const div = document.createElement('div');
6567
const portal = ReactDOM.createPortal(<div />, div);
66-
expect(ReactIs.typeOf(portal)).toBe('ReactPortal');
68+
expect(ReactIs.typeOf(portal)).toBe(ReactIs.Portal);
6769
expect(ReactIs.isPortal(portal)).toBe(true);
6870
expect(ReactIs.isPortal(div)).toBe(false);
6971
});
7072

7173
it('should identify strict mode', () => {
72-
expect(ReactIs.typeOf(<React.StrictMode />)).toBe('ReactStrictMode');
74+
expect(ReactIs.typeOf(<React.StrictMode />)).toBe(ReactIs.StrictMode);
7375
expect(ReactIs.isStrictMode(<React.StrictMode />)).toBe(true);
7476
expect(ReactIs.isStrictMode(<React.unstable_AsyncMode />)).toBe(false);
7577
expect(ReactIs.isStrictMode(<div />)).toBe(false);

0 commit comments

Comments
 (0)