File tree Expand file tree Collapse file tree 1 file changed +35
-1
lines changed
Expand file tree Collapse file tree 1 file changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,40 @@ Functional switch statement. Strictly typed.
1515``` js
1616import switchFunctional from ' switch-functional'
1717
18+ const getUserType = (user ) =>
19+ switchFunctional (user .type )
20+ .case (' dev' , ' developer' )
21+ .case ([' admin' , ' owner' ], ' administrator' )
22+ .default (' unknown' )
23+ ```
24+
25+ This is equivalent to:
26+
27+ <!-- eslint-disable no-restricted-syntax, no-fallthrough -->
28+
29+ ``` js
30+ const getUserType = (user ) => {
31+ switch (user .type ) {
32+ case ' dev' : {
33+ return ' developer'
34+ }
35+
36+ case ' admin' :
37+
38+ case ' owner' : {
39+ return ' administrator'
40+ }
41+
42+ default : {
43+ return ' unknown'
44+ }
45+ }
46+ }
47+ ```
48+
49+ ## Testing input
50+
51+ ``` js
1852const getUserType = (user ) =>
1953 switchFunctional (user)
2054 .case (isDeveloper, ' developer' )
@@ -50,7 +84,7 @@ const getUserType = (user) =>
5084 .default (' unknown' )
5185```
5286
53- ## Dynamic return value
87+ ## Returning dynamic values
5488
5589<!-- eslint-disable no-shadow -->
5690
You can’t perform that action at this time.
0 commit comments