-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskPractises_18.js
81 lines (55 loc) · 2.13 KB
/
TaskPractises_18.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
// SETUP
// =========================
const posts = { 1: { title: "First" }, 2: { title: "Second" } }
const comments = { First: [{ id: 1, body: "Brilliant!" }], Second: [{ id: 2, body: "Unforgivable" }] }
const getPost = id =>
new Task((rej, res) =>
setTimeout(() => posts[id] ? res(posts[id]) : rej('not found'), 200))
const getComments = post =>
new Task((rej, res) =>
setTimeout(() => res(comments[post.title]), 200))
// Exercise: Task
// Goal: Refactor each example using Task
// Bonus points: no curly braces
// Ex1: Use the result of getPost() and upperCase the title. Posts and comments are defined above and look like {title: String} and {id: Int, body: String} respectively.
// =========================
const postTitle = id => // uppercase the title of the result of getPost()
getPost(id).map(post => post.title.toUpperCase())
QUnit.test("Ex1: postTitle", assert => {
const done = assert.async();
postTitle(1)
.fork(console.error, t => {
assert.deepEqual(t, 'FIRST')
done()
})
})
// Ex2: pass in the post to getComments(), defined above, then assign the returned comments to the post
// =========================
const commentsForPost = id =>
getPost(id)
.chain(post =>
getComments(post)
.map(comments => Object.assign({ comments }, post))
)
QUnit.test("Ex2: commentsForPost", assert => {
const done = assert.async();
commentsForPost(2)
.fork(console.error, t => {
assert.deepEqual(t.title, "Second")
assert.deepEqual(t.comments, comments["Second"])
done()
})
})
// Ex3: Wrap location.href in a Task to make it "pure"
// =========================
const getHref =
new Task((rej, res) => res(location.href)) // wrap me in Task
QUnit.test("Ex3: getHref", assert => {
const done = assert.async();
getHref
.fork(console.error, t => {
assert.equal(true, !!t.match("cdpn.io"))
done()
})
})
// i have a problem with importing a package so go this link to run: https://codepen.io/Tidalu/pen/WNYGOLq?editors=0010