-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy path07.js
165 lines (143 loc) · 4.3 KB
/
07.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import shallowEqualArrays from 'shallow-equal/arrays'
import CID from 'cids'
const validate = async (result, ipfs) => {
if (!result) {
return { fail: 'No result was returned. Did you forget to return a result from your `traversePosts` function? Or perhaps you accidentally edited the `run` function?' }
}
if (result instanceof Error && result.message === `Cannot read property 'prev' of undefined`) {
return {
fail: 'Cannot read property `prev` of undefined. Did you try to access the value of `ipfs.dag.get` before the function completed?',
overrideError: true
}
}
if (result instanceof Error && result.message === `Cannot read property 'value' of undefined`) {
return {
fail: 'Cannot read property `value` of undefined. Did you try to access the value of `ipfs.dag.get` before the function completed?',
overrideError: true
}
}
if (!Array.isArray(result)) {
return { fail: 'The return value of your traversePosts function needs to be an array.' }
}
const dogPostCid = 'bafyreifvq4aykfnxjgqqmjelphadwhzvc4mt6h3mwytj54oa3qakuis3ie'
const computerPostCid = 'bafyreiflecr42lhn6bpy7friobhurxagp6ml34s5uzazqmwxxpqnouhfne'
const treePostCid = 'bafyreic5ndfk2yj4vr7pdhk4n435hxr522faalcse2ls4ukzddr7d5qxhi'
try {
if (result.length !== 3 || result === undefined) {
return { fail: 'Your traversePosts function needs to return 3 CIDs' }
}
const isCids = result.every(CID.isCID)
if (!isCids) {
return { fail: 'Your traversePosts function needs to return CIDs.' }
}
const expectedCids = [treePostCid, computerPostCid, dogPostCid]
const returnedCids = result.map(item => item.toString())
if (!shallowEqualArrays(returnedCids.sort(), expectedCids.sort())) {
return {
fail: 'The CIDs returned by the traversePosts function did not match the expected CIDs.',
log: {
returnedCids: returnedCids,
expectedCids: expectedCids
}
}
}
} catch (err) {
return { fail: `Your function threw an error: ${err}.` }
}
return { success: 'Great job! You\'ve completed this series of lessons!' }
}
const code = `/* globals ipfs */
const traversePosts = async (cid) => {
// Your code goes here
}
const run = async () => {
const natCid = await ipfs.dag.put({ author: "Nat" })
const samCid = await ipfs.dag.put({ author: "Sam" })
const treePostCid = await ipfs.dag.put({
content: "trees",
author: samCid,
tags: ["outdoor", "hobby"]
})
const computerPostCid = await ipfs.dag.put({
content: "computers",
author: natCid,
tags: ["hobby"],
prev: treePostCid
})
const dogPostCid = await ipfs.dag.put({
content: "dogs",
author: samCid,
tags: ["funny", "hobby"],
prev: computerPostCid
})
const outdoorTagCid = await ipfs.dag.put({
tag: "outdoor",
posts: [treePostCid]
})
const hobbyTagCid = await ipfs.dag.put({
tag: "hobby",
posts: [treePostCid, computerPostCid, dogPostCid]
})
const funnyTagCid = await ipfs.dag.put({
tag: "funny",
posts: [dogPostCid]
})
return await traversePosts(dogPostCid)
}
return run
`
const solution = `/* globals ipfs */
const traversePosts = async (cid) => {
const result = []
while (cid) {
result.push(cid)
const current = await ipfs.dag.get(cid)
cid = current.value.prev
}
return result
}
const run = async () => {
const natCid = await ipfs.dag.put({ author: "Nat" })
const samCid = await ipfs.dag.put({ author: "Sam" })
const treePostCid = await ipfs.dag.put({
content: "trees",
author: samCid,
tags: ["outdoor", "hobby"]
})
const computerPostCid = await ipfs.dag.put({
content: "computers",
author: natCid,
tags: ["hobby"],
prev: treePostCid
})
const dogPostCid = await ipfs.dag.put({
content: "dogs",
author: samCid,
tags: ["funny", "hobby"],
prev: computerPostCid
})
const outdoorTagCid = await ipfs.dag.put({
tag: "outdoor",
posts: [treePostCid]
})
const hobbyTagCid = await ipfs.dag.put({
tag: "hobby",
posts: [treePostCid, computerPostCid, dogPostCid]
})
const funnyTagCid = await ipfs.dag.put({
tag: "funny",
posts: [dogPostCid]
})
return await traversePosts(dogPostCid)
}
return run
`
const options = {
overrideErrors: true
}
export default {
validate,
code,
solution,
options
}