forked from CloudCannon/11feed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
category-store.js
43 lines (36 loc) · 889 Bytes
/
category-store.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
class CategoryStore {
constructor() {
this.store = {};
}
// Method to add items to a category.
// If the category already exists, merge the items with the existing ones.
add(key, items) {
if (!Array.isArray(items)) {
items = [items];
}
// Check if the category already exists.
if (this.store.hasOwnProperty(key)) {
// Merge the new items with the existing ones if the category already exists.
this.store[key] = [...new Set([...this.store[key], ...items])];
} else {
// Otherwise, just add the new category with its items.
this.store[key] = items;
}
}
list() {
return this.store;
}
listFlat() {
let flat = {};
for (let category in this.store) {
for (let item of this.store[category]) {
if (! (item in flat)) {
flat[item] = [];
}
flat[item].push(category);
}
}
return flat;
}
}
module.exports = CategoryStore;