-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataFile.js
36 lines (32 loc) · 851 Bytes
/
DataFile.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
"use strict";
var
fs = require('fs');
class DataFile {
constructor(path, init) {
this.path = path;
this.init = init ? init : {};
try {
fs.accessSync(this.path, fs.R_OK);
this.collection = JSON.parse(fs.readFileSync(this.path));
} catch(error) {
this.collection = this.init;
this.write();
}
}
write() {
fs.writeFile(this.path, JSON.stringify(this.collection));
}
getAll() { return this.collection; }
get(key) { return this.collection[key]; }
has(key) {
let hasKey = true;
if(this.collection[key] === undefined) {
hasKey = false;
}
return hasKey;
}
set(key,value) { this.collection[key] = value; this.write(); }
delete(key) { delete this.collection[key]; this.write(); }
size() { return Object.keys(this.collection).length; }
}
module.exports = DataFile;