-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (140 loc) · 3.78 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
const fs = require('fs'),
path = require('path');
class Database {
constructor(location = 'database.json') {
if (typeof location !== 'string')
throw new TypeError('Location must be a string');
if (location.endsWith('/'))
location += 'database.json';
location = path.resolve(location);
let dir = location.split('/');
dir.pop();
dir = dir.join('/');
if (dir && !fs.existsSync(dir))
fs.mkdirSync(dir, {
recursive: true
});
if (!fs.existsSync(location))
fs.writeFileSync(location, '{}');
else if (fs.statSync(location).isDirectory()) {
location += '/database.json';
if (!fs.existsSync(location))
fs.writeFileSync(location, '{}');
}
this.#filePath = location;
}
#filePath;
toString() {
return fs.readFileSync(this.#filePath, 'utf8');
}
get(path = '') {
if (typeof path !== 'string')
throw new TypeError('Path must be a string');
if (path === '')
return this.read();
return _get(path, this.read());
}
set(path, value) {
if (typeof path !== 'string')
throw new TypeError('Path must be a string');
const valStr = JSON.stringify(value);
if (path === '') {
if (this.toString() !== valStr)
fs.writeFileSync(this.#filePath, valStr || '{}');
return this;
}
const data = JSON.stringify(_set(path, value, this.read()));
if (this.toString() !== data)
fs.writeFileSync(this.#filePath, data);
return this;
}
delete(path) {
if (typeof path !== 'string')
throw new TypeError('Path must be a string');
if (path === '')
return this.set('', {});
if (!this.has(path))
return this;
const data = _delete(path, this.read());
fs.writeFileSync(this.#filePath, JSON.stringify(data));
return this;
}
find(path, fn) {
const obj = this.get(path);
if (typeof obj !== 'object')
throw new TypeError('Path must lead to an object');
if (typeof fn !== 'function')
throw new TypeError('fn must be a function');
for (const [k, v] of Object.entries(obj))
if (fn(v, k))
return v;
}
findAll(path, fn) {
const obj = this.get(path);
if (typeof obj !== 'object')
throw new TypeError('Path must lead to an object');
if (typeof fn !== 'function')
throw new TypeError('fn must be a function');
const arr = [];
for (const [k, v] of Object.entries(obj))
if (fn(v, k))
arr[arr.length] = v;
if (arr.length !== 0)
return arr;
}
has(path) {
return this.get(path) !== undefined;
}
read() {
return JSON.parse(this.toString());
}
clone() {
return new Database(this.#filePath);
}
}
const { bind, call } = Function.prototype,
uncurryThis = bind.bind(call),
hasOwnProperty = uncurryThis(Object.prototype.hasOwnProperty);
function _delete(path, obj) {
let locations = path.split('.'),
key = locations.pop(),
ref = obj;
for (const loc of locations) {
if (!hasOwnProperty(ref, loc) ||
typeof ref[loc] !== 'object')
return obj;
ref = ref[loc];
}
if (hasOwnProperty(ref, key))
delete ref[key];
return obj;
}
function _set(path, value, obj) {
let locations = path.split('.'),
key = locations.pop(),
ref = obj;
for (const loc of locations) {
if (!hasOwnProperty(ref, loc) ||
typeof ref[loc] !== 'object')
ref = ref[loc] = {};
else ref = ref[loc];
}
ref[key] = value;
return obj;
}
function _get(path, obj) {
const locations = path.split('.'),
key = locations.pop();
for (const loc of locations) {
if (!hasOwnProperty(obj, loc) ||
typeof obj[loc] !== 'object')
return;
obj = obj[loc];
}
if (typeof obj !== 'object' ||
!hasOwnProperty(obj, key))
return;
return obj[key];
}
module.exports = Database;