-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
46 lines (35 loc) · 761 Bytes
/
data.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
let store = {};
let ScratchTypes = {
text: true,
image: true,
}
const ScratchPad = {
__validatePresence: (id) => {
if (!(id in store)) {
throw Error('unauthorized')
}
},
__validateScratchType: (content) => {
if (!content) throw Error('empty_content')
if (!content.type) throw Error('invalid_data')
if (!(content.type in ScratchTypes)) {
throw Error('invalid_type')
}
return true;
},
create: function(id, data) {
this.__validateScratchType(data)
if (!store[id]) {
store[id] = []
}
store[id].push(data)
return this
},
all: () => Object.keys(store).flatMap(id => store[id] || []),
find: (id) => {
this.__validatePresence(id);
return store[id]
}
}
module.export = ScratchTypes
module.exports = ScratchPad;