-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstorage.js
More file actions
27 lines (24 loc) · 807 Bytes
/
storage.js
File metadata and controls
27 lines (24 loc) · 807 Bytes
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
export default class Storage {
constructor() {
this.limit = 10;
this.storedName = 'gaSearch.history';
}
setLimit(limit) {
this.limit = limit;
}
addEntry(entry) {
const history = this.getHistory();
// eliminate duplicated entry
const duplicatedEntryIdx = history.findIndex(item => item._key === entry._key);
if (duplicatedEntryIdx > -1) {
history.splice(duplicatedEntryIdx, 1);
} else if (history.length > this.limit - 1) {
history.pop();
}
history.unshift(entry);
localStorage.setItem(this.storedName, JSON.stringify(history));
}
getHistory() {
return localStorage.getItem(this.storedName) ? JSON.parse(localStorage.getItem(this.storedName)) : [];
}
}