-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheitherPractices_15.js
128 lines (101 loc) · 3.21 KB
/
eitherPractices_15.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Definitions
// ====================
const Right = x =>
({
chain: f => f(x),
map: f => Right(f(x)),
fold: (f, g) => g(x),
toString: () => `Right(${x})`
})
const Left = x =>
({
chain: f => Left(x),
map: f => Left(x),
fold: (f, g) => f(x),
toString: () => `Left(${x})`
})
const fromNullable = x =>
x != null ? Right(x) : Left(null)
const tryCatch = f => {
try {
return Right(f())
} catch (e) {
return Left(e)
}
}
const logIt = x => {
console.log(x)
return x
}
const DB_REGEX = /postgres:\/\/([^:]+):([^@]+)@.*?\/(.+)$/i
// Exercise: Either
// Goal: Refactor each example using Either
// Bonus: no curlies
// =========================
// Ex1: Refactor streetName to use Either instead of nested if's
// =========================
const street = user =>
fromNullable(user.address) // right /left(address)
.map(address => address.street)
.fold(() => 'no street', x => x)
QUnit.test("Ex1: street", assert => {
const user = { address: { street: { name: "Willow" } } }
assert.deepEqual(street(user), { name: "Willow" })
assert.equal(street({}), "no street")
})
// Ex1: Refactor streetName to use Either instead of nested if's
// =========================
const streetName = user => {
return fromNullable(user)
.chain(user => fromNullable(user.address))
.chain(address => fromNullable(address.street))
.map(street => street.name)
.fold(() => 'no street', x => x)
}
QUnit.test("Ex1: streetName", assert => {
const user = { address: { street: { name: "Willow" } } }
assert.equal(streetName(user), "Willow")
assert.equal(streetName({}), "no street")
assert.equal(streetName({ address: { street: null } }), "no street")
})
// Ex2: Refactor parseDbUrl to return an Either instead of try/catch
// =========================
const parseDbUrl_ = cfg => {
try {
const c = JSON.parse(cfg) // throws if it can't parse
return c.url.match(DB_REGEX)
} catch (e) {
return null
}
}
const parseDbUrl = cfg =>
fromNullable(cfg)
.chain(cfg => fromNullable(JSON.parse(cfg)))
.map(c => c.url.match(DB_REGEX))
.fold(e => null, x => x)
QUnit.test("Ex1: parseDbUrl", assert => {
const config = '{"url": "postgres://sally:muppets@localhost:5432/mydb"}'
assert.equal(parseDbUrl(config)[1], "sally")
assert.equal(parseDbUrl(), null)
})
// Ex3: Using Either and the functions above, refactor startApp
// =========================
const startApp_ = cfg => {
const parsed = parseDbUrl(cfg)
if (parsed) {
const [_, user, password, db] = parsed
return `starting ${db}, ${user}, ${password}`
} else {
return "can't get config"
}
}
const startApp = cfg =>
fromNullable(parseDbUrl(cfg))
.map(([_, user, password, db]) => `starting ${db}, ${user}, ${password}`)
.fold(() => "can't get config", x => x)
QUnit.test("Ex3: startApp", assert => {
const config = '{"url": "postgres://sally:muppets@localhost:5432/mydb"}'
assert.equal(String(startApp(config)), "starting mydb, sally, muppets")
assert.equal(String(startApp()), "can't get config")
})
// go here https://codepen.io/Tidalu/pen/RwqaLpV?editors=0010