-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
51 lines (40 loc) · 1.01 KB
/
index.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
// Copyright 2023 Sleepless Software Inc. All rights reserved.
const fs = require("fs")
function load( f ) {
const self = this;
f = f || self.file;
self.__proto__.file = f;
try {
const ds = JSON.parse( fs.readFileSync( f ) );
for( let key in ds )
self[ key ] = ds[ key ];
} catch( e ) {
self.clear();
}
}
// this may throw exception, but it's up to caller to deal with it.
function save( f ) {
const self = this;
f = f || self.file;
self.__proto__.file = f;
fs.writeFileSync( f, JSON.stringify( self, null, 4 ) );
}
function clear() {
const self = this;
for( let key in self )
delete self[ key ];
}
const ldsv = { load:load, save:save, clear:clear }
function F( file, opts ) {
var self = this;
self.file = file;
self.opts = opts || {};
}
F.prototype = ldsv;
function DS( f, opts ) {
const self = this;
self.__proto__ = new F( "ds.json", opts );
self.load( f );
}
DS.prototype = new F();
module.exports = DS;