-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
196 lines (193 loc) · 6.84 KB
/
Copy pathindex.js
File metadata and controls
196 lines (193 loc) · 6.84 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const fs = require('fs')
module.exports = class db {
constructor(pathtofile, opts) {
if (pathtofile.startsWith("./")) {
this.path1 = '../../' + pathtofile.slice(2)
this.path2 = pathtofile
} else {
this.path1 = '../../' + pathtofile
this.path2 = './' + pathtofile
}
if (opts) {
if (opts.init) {
fs.writeFileSync(this.path2, "{}", function (err) {
if (err) return console.log(err);
});
this.db = {}
}
}
else {
this.getall(1)
}
}
getall(num) {
if (num == 1) {
this.db = require(this.path1)
}
else {
this.db = require(this.path2)
}
return this.db
}
write() {
let str = JSON.stringify(this.db)
fs.writeFileSync(this.path2, str, function (err) {
if (err) return console.log(err);
});
this.getall(1)
}
init(path) {
this.getall(1)
let paths = path.split("/")
let str = ""
for (let i = 0; i < paths.length; i++) {
if (paths[i] != '') {
str += "[`" + paths[i] + "`]"
eval("if(!this.db" + str + "){this.db" + str + " = {}}")
}
}
return str
}
set(path, data) {
if (!data) throw "data must be specified"
let str = this.init(path)
if (Array.isArray(data)) {
let str = this.init(path)
eval("this.db" + str + " = [" + data + "]")
}
else if (typeof data == "object") {
Object.keys(data).forEach((el, i) => {
let str = this.init(path)
str += "[" + el + "]"
if (typeof data[el] == "object") {
let data2 = Object.values(data)[i]
let str = this.init(path)
str += "[`" + el + "`]"
eval("this.db" + str + " = "+da)
Object.keys(data2).forEach((el2, f) => {
let str = this.init(path)
str+="[`"+el+"`]"
if (Array.isArray(Object.values(data2)[f])) {
let str2 = Object.values(data2)[f].join(",")
str += "[`" + el2 + "`]"
if(Object.values(data2)[f].length == 0) {
eval("this.db" + str + " = []")
return
}
eval("this.db" + str + " = [" + str2 + "]")
}
else if (typeof Object.values(data2)[f] == "string") {
str += "[`" + el2 + "`]"
eval("this.db" + str + " = " + '`' + Object.values(data2)[f] + '`')
}
else if (typeof Object.values(data2)[f] == "boolean") {
str += "[`" + el2 + "`]"
eval("this.db" + str + " = " + Object.values(data2)[f])
}
else {
str += "[`" + el2 + "`]"
eval("this.db" + str + " = " + Object.values(data2)[f])
}
})
}
else if (Array.isArray(Object.values(data)[i])) {
let str = this.init(path)
let str2 = Object.values(data)[i].join(",")
if(Object.values(data)[i].length == 0) {
eval("this.db" + str + " = []")
return
}
eval("this.db" + str + " = [" + str2 + "]")
}
else if (typeof Object.values(data)[i] == "string") {
let str = this.init(path)
str += "[`" + el + "`]"
eval("this.db" + str + " = " + '`' + Object.values(data)[i] + '`')
}
else if (typeof Object.values(data)[i] == "boolean") {
let str = this.init(path)
str += "[`" + el + "`]"
eval("this.db" + str + " = " + Object.values(data)[i])
}
else {
let str = this.init(path)
str += "[`" + el + "`]"
eval("this.db" + str + " = " + Object.values(data)[i])
}
})
}
else if (typeof data == "string") {
eval("this.db" + str + " = `" + data + "`")
}
this.write()
return data
}
get(path) {
this.getall(1)
let paths = path.split("/")
let str = ""
for (let i = 0; i < paths.length; i++) {
if (paths[i] != '') {
str += "[`" + paths[i] + "`]"
}
}
let result
eval("result = this.db" + str)
return result
}
push(path, data) {
this.getall(1)
let paths = path.split("/")
let str = ""
let dataa = data
if (typeof data == "string") {
dataa = "`" + data + "`"
}
for (let i = 0; i < paths.length; i++) {
str += "[`" + paths[i] + "`]"
eval("if(!this.db" + str + "){this.db" + str + " = {}}")
}
eval("if(!this.db" + str + ")throw `the path doesnt exist`")
eval("this.db" + str + ".push(" + dataa + ")")
this.write()
}
delete(path) {
let str = this.init(path)
eval("if(!this.db" + str + ")throw `the path doesnt exist`")
eval("delete this.db" + str)
this.write()
return true
}
add(path, number) {
if (typeof number != "number") return false
let str = this.init(path)
let paths = path.split("/")
let key = path[paths.length - 1]
if (paths.length > 0) {
paths.pop()
key = path
}
paths = paths.join("/")
eval("if(typeof this.db" + str + "== 'object' ){this.set(`" + paths + "`, { " + key + " : " + number + "} )}else{ this.db" + str + "+= " + number + "}")
this.write()
return true
}
has(path) {
this.getall(1)
let paths = path.split("/")
let str = ""
let result = true
for (let i = 0; i < paths.length; i++) {
str += "[`" + paths[i] + "`]"
eval('if(!this.db' + str + '){result = false}')
}
return result
}
substract(path, number) {
this.add(path, -number)
}
all() {
this.getall(1)
return this.db
}
}