-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (63 loc) · 2.42 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
const fs = require('fs')
function create(key, value, file) {
if(!key) return new Error("Invalid Key! Use: create('key', 'value', './data.json')")
if(!value) return new Error("Invalid Value! Use: create('key', 'value', './data.json')")
if(!file) return new Error("enter the file name! Use: create('key', 'value', './data.json')")
if(!fs.existsSync(file)) {
fs.writeFileSync(file, "{}")
}
const json = JSON.parse(fs.readFileSync(file).toString())
json[key] = value;
fs.writeFileSync(file, JSON.stringify(json))
const yes = 'yes'
return yes
}
function set(key, value, file) {
if(!key) return new Error("Invalid Key! Use: create('key', 'value', './data.json')")
if(!value) return new Error("Invalid Value! Use: create('key', 'value', './data.json')")
if(!file) return new Error("enter the file name! Use: create('key', 'value', './data.json')")
if(!fs.existsSync(file)) {
fs.writeFileSync(file, "{}")
}
const json = JSON.parse(fs.readFileSync(file).toString())
json[key] = value;
fs.writeFileSync(file, JSON.stringify(json))
const yes = 'yes'
return yes
}
function get(key, file) {
if(!key) return new Error("enter key string! Use: get('key', './data.json')")
if(!file) return new Error( "enter file path! Use: get('key', './data.json')")
if(!fs.existsSync(file)) return new Error(" file not found ")
const jason = JSON.parse(fs.readFileSync(file).toString())
return jason[key]
}
function del(name, file) {
if(!name) return new Error("enter key name! Use: delete('key', 'data.json')")
if(!file) return new Error("enter file path! Use: delete('key', './data.json')")
if(!fs.existsSync(file)) new Error(" file not found ")
let jason = JSON.parse(fs.readFileSync(file, "utf8"));
delete jason[name];
fs.writeFileSync(file, JSON.stringify(jason));
return "";
}
function all(file) {
if(!file) return new Error("enter file path! Use: all('./data.json')")
if(!fs.existsSync(file)) return new Error(" file not found ")
return JSON.parse(fs.readFileSync(file, "utf8"));
}
function has(name, file) {
if(!name) return new Error("enter key name! Use: has('key', './data')")
if(!file) return new Error("enter file path! Use: has('key', './data.json')")
if(!fs.existsSync(file)) return new Error(" file not found ")
let content = JSON.parse(fs.readFileSync(file, "utf8"));
return content[name] ? true : false;
}
module.exports = {
create,
set,
get,
del,
all,
has
}