-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertor.js
86 lines (73 loc) · 2.12 KB
/
convertor.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
const fs = require("fs");
class Convertor {
constructor() {
this.output = "";
}
checkType(value, name) {
if (typeof value === "number") {
this.numberConvertor(value, name);
} else if (typeof value === "string") {
this.stringConvertor(value, name);
} else if (typeof value === "boolean") {
this.booleanConvertor(value, name);
} else if (value === null) {
this.nullConvertor(null, name);
} else if (Array.isArray(value)) {
this.arrayConvertor(value, name);
} else if (typeof value === "object") {
this.objectConvertor(value, name);
console.log(value);
}
}
JSONToXML(inputPath, outputPath) {
fs.readFile(inputPath, "utf8", (err, data) => {
this.output = "";
if (!err) {
const res = JSON.parse(data);
console.log(typeof res);
this.checkType(res);
}
console.log(this.output);
fs.writeFile(outputPath, this.output, (err) => {
if (!err) {
console.log("Data has been saved successfully");
}
});
});
}
numberConvertor = (value, name) => {
this.output += `<number${
name ? ' name="' + name + '"' : ""
}>${value}</number>`;
};
booleanConvertor = (value, name) => {
this.output += `<boolean${
name ? ' name="' + name + '"' : ""
}>${value}</boolean>`;
};
stringConvertor(value, name) {
this.output += `<string${
name ? ' name="' + name + '"' : ""
}>${value}</string>`;
}
nullConvertor(value, name) {
this.output += `<null${name ? ' name="' + name + '"' : ""}/>`;
}
objectConvertor(res, name) {
this.output += `<object${name ? ' name="' + name + '"' : ""}>`;
for (const [key, value] of Object.entries(res)) {
this.checkType(value, key);
}
this.output += `</object>`;
}
arrayConvertor(value, name) {
this.output += `<array${name ? ' name="' + name + '"' : ""}>`;
for (let i = 0; i < value.length; i++) {
this.checkType(value[i]);
}
this.output += `</array>`;
}
}
const ConvertorInstance = new Convertor();
// Object.freeze(ConvertorInstance);
module.exports = ConvertorInstance;