-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
89 lines (86 loc) · 1.99 KB
/
main.ts
File metadata and controls
89 lines (86 loc) · 1.99 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { chain } from './case.js'
export type { Condition, Options, Switch } from './types.js'
/**
* Functional switch statement. This must be chained with
* `.case()` statements and end with `.default()`.
*
* @example <caption>Basic usage</caption>
* ```js
* import switchFunctional from 'switch-functional'
*
* const getUserType = (user) =>
* switchFunctional(user.type)
* .case('dev', 'developer')
* .case(['admin', 'owner'], 'administrator')
* .default('unknown')
* ```
*
* This is equivalent to:
*
* ```js
* const getUserType = (user) => {
* switch (user.type) {
* case 'dev': {
* return 'developer'
* }
*
* case 'admin':
*
* case 'owner': {
* return 'administrator'
* }
*
* default: {
* return 'unknown'
* }
* }
* }
* ```
*
* @example <caption>Testing input</caption>
* ```js
* const getUserType = (user) =>
* switchFunctional(user)
* .case(isDeveloper, 'developer')
* .case([isAdmin, isOwner], 'admin')
* .default('unknown')
* ```
*
* This is equivalent to:
*
* ```js
* const getUserType = (user) => {
* if (isDeveloper(user)) {
* return 'developer'
* }
*
* if (isAdmin(user) || isOwner(user)) {
* return 'admin'
* }
*
* return 'unknown'
* }
* ```
*
* @example <caption>Testing properties</caption>
* ```js
* const getUserType = (user) =>
* switchFunctional(user)
* // Checks `user.hasDevProjects === true`
* .case({ hasDevProjects: true }, 'developer')
* // Checks for deep properties
* .case({ devProjectsCount: 0, permissions: { admin: true } }, 'admin')
* .default('unknown')
* ```
*
* @example <caption>Returning dynamic values</caption>
* ```js
* const getUserType = (user) =>
* switchFunctional(user)
* .case(isDeveloper, (user) => user.developerType)
* .case(isAdmin, (user) => user.adminType)
* .default((user) => user.genericType)
* ```
*/
const switchFunctional = chain(false)
export default switchFunctional