-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonMan.js
79 lines (66 loc) · 1.55 KB
/
jsonMan.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
const fs = require('fs');
const test = require('./testingMethods.js');
module.exports = {
//Reads json file and returns data in object
read: file => {
try{
let data = fs.readFileSync(file,'utf8');
return JSON.parse(data);
}catch(err){
console.log(err);
}
},
//Writes object to json file
write: (file, obj) => {
if(obj == undefined){return test.errorLog(`Unable to write ${obj} to ${file}`);}
try{
json = JSON.stringify(obj);
fs.writeFileSync(file, json, 'utf8');
}catch(err){
console.log(err);
}
},
stringConverter: arr => {
let str = "";
for(let i = 0; i < arr.length; i++){
str += arr[i];
if(i + 1 != arr.length){
str += " ";
}
}
return str;
},
attributeConverter: arr => {
let str = "";
for(let i = 0; i < arr.length; i++){
if(i != arr.length - 1){
str += `${arr[i]}, `;
}else{
str += `${arr[i]}`;
}
}
return str;
},
valueConverter: arr => {
let str = "";
for(let i = 0; i < arr.length; i++){
if(i != arr.length - 1){
if(typeof arr[i] == "string"){
str += `'${arr[i]}', `;
}else{
str += `${JSON.stringify(arr[i])}, `;
}
}else{
if(typeof arr[i] == "string"){
str += `'${arr[i]}'`;
}else{
str += `${JSON.stringify(arr[i])}`;
}
}
}
return str;
},
filter: str =>{
return str.replace(`'`, `/'`);
}
}