-
Notifications
You must be signed in to change notification settings - Fork 0
/
localStorage.js
40 lines (33 loc) · 940 Bytes
/
localStorage.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
const { writeFile, readFileSync, existsSync, unlink } = require('fs');
class LocalStorage {
constructor() {
if(existsSync('localStorage.json')) {
console.log("Loading Local Storage...");
const txt = readFileSync('localStorage.json');
this.items = JSON.parse(txt);
} else {
this.items = {};
}
}
get length() {
return Object.keys(this.items).length;
}
getItem(key) {
return this.items[key];
}
setItem(key, value) {
this.items[key] = value;
writeFile('localStorage.json', JSON.stringify(this.items), error => {
if (error) {
console.error(error);
}
})
}
clear() {
this.items = {};
unlink('localStorage.json', () => {
console.log("Removed Local Storage json file.");
})
}
}
module.exports = new LocalStorage();