-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection.js
56 lines (48 loc) · 1.28 KB
/
section.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
const _ = require('lodash');
module.exports = class Section {
constructor(sections) {
this.sections = Array.isArray(sections) ? sections : [];
this.SN = {};
this.ID = {};
this.triggerMap();
}
load(sections) {
if (Array.isArray(sections)) {
this.sections = sections;
this.triggerMap();
}
}
triggerMap() {
this.SN = _.keyBy(this.sections, 'system_name');
this.ID = _.keyBy(this.sections, 'id');
}
getById(id) {
return this.ID[id] ? this.ID[id] : null;
}
addOrUpdateSection(section) {
if (this.ID[section.id]) {
const index = this.sections.findIndex(item => item.id === section.id);
this.sections[index] = section;
} else {
this.sections.push(section);
}
this.ID[section.id] = section;
this.SN[section.system_name] = section;
}
//Key should be id or sn
deleteByKey(key) {
const item = this.ID[key] || this.SN[key];
if (item) {
delete this.ID[item.id];
delete this.SN[item.system_name];
const index = this.sections.findIndex(item => item.id === section.id);
delete this.sections[index];
}
}
getBySystemName(name) {
return this.SN[name] ? this.SN[name] : null;
}
getSystemName(id) {
return this.ID[id] ? this.ID[id].system_name : null;
}
};