-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
story_store.js
88 lines (71 loc) · 1.61 KB
/
story_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
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
let cnt = 0;
function getId() {
cnt += 1;
return cnt;
}
export default class StoryStore {
constructor() {
this._data = {};
}
addStory(kind, name, fn) {
if (!this._data[kind]) {
this._data[kind] = {
kind,
index: getId(),
stories: {},
};
}
this._data[kind].stories[name] = {
name,
index: getId(),
fn,
};
}
getStoryKinds() {
return Object.keys(this._data)
.map(key => this._data[key])
.filter(kind => Object.keys(kind.stories).length > 0)
.sort((info1, info2) => (info1.index - info2.index))
.map(info => info.kind);
}
getStories(kind) {
if (!this._data[kind]) {
return [];
}
return Object.keys(this._data[kind].stories)
.map(name => this._data[kind].stories[name])
.sort((info1, info2) => (info1.index - info2.index))
.map(info => info.name);
}
getStory(kind, name) {
const storiesKind = this._data[kind];
if (!storiesKind) {
return null;
}
const storyInfo = storiesKind.stories[name];
if (!storyInfo) {
return null;
}
return storyInfo.fn;
}
removeStoryKind(kind) {
this._data[kind].stories = {};
}
hasStoryKind(kind) {
return Boolean(this._data[kind]);
}
hasStory(kind, name) {
return Boolean(this.getStory(kind, name));
}
dumpStoryBook() {
const data = this.getStoryKinds()
.map(kind => ({ kind, stories: this.getStories(kind) }));
return data;
}
size() {
return Object.keys(this._data).length;
}
clean() {
this.getStoryKinds().forEach(kind => delete this._data[kind]);
}
}