This repository was archived by the owner on Aug 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (144 loc) · 3.65 KB
/
index.js
File metadata and controls
157 lines (144 loc) · 3.65 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
const fs = require("fs");
/**
* Translation system class
* @class Translate
*/
module.exports = class Translate {
/**
* Initialize the class
*
* @param {string} files - Files name
* @param {string} Directory - File path
* @throws Will throw an error if file does not exist
* @constructor
*/
constructor(files, Directory = "./locales/") {
this.error = true;
this.location = Directory;
this.loc = `${this.location + files}.json`;
try {
this.data = JSON.parse(fs.readFileSync(this.loc));
this.error = false;
} catch (err) {
throw new Error("[ERROR] : The translation of the '" + files + "' file is not found !");
}
}
/**
* Select the line defined by the key
*
* @param {string} Lines - Key name
* @param {null} replace - Replace string in value
* @throws Will throw an error if key does not exist
* @returns {string}
* @constructor
*/
GetLine(Lines, replace = null) {
if(!this.error && this.resolve(Lines)) {
if (replace !== null) {
return this.resolve(Lines).replace("%s", replace);
} else {
return this.resolve(Lines);
}
}
else {
throw new Error("[ERROR] : This key doesn't exist");
}
}
/**
* Select block defined by the key
*
* @param {string} Lines - Key name
* @param {null} replaces - Replace string in value
* @throws Will throw an error if block does not exist
* @returns {string}
* @constructor
*/
GetBlock(Lines, replaces = null) {
if (!this.error && this.resolve(Lines)) {
if (replaces) {
return this.resolve(Lines).replace("%s", replaces);
}
return this.resolve(Lines);
}
else {
throw new Error("[ERROR] : This block doesn't exist");
}
}
/**
* Add a key to the json file
*
* @param {string} key - Set key
* @param {string} value - Set value
* @throws Will throw an error if key already exists
* @returns {boolean}
* @constructor
*/
SetLine(key, value) {
if (!this.error && this.resolve(key) !== false) {
this.data[key] = value;
fs.writeFileSync(this.loc, JSON.stringify(this.data, null, 2));
return true;
}
else {
throw new Error("[ERROR] : This key already exists");
}
}
/**
* Update a key in the json file
*
* @param {string} key - Key name
* @param {string} value - New value
* @throws Will throw an error if key does not exist
* @returns {boolean}
* @constructor
* @todos Redesign of the function to allow to change the key
*/
Update(key, value) {
if (!this.error && this.resolve(key)) {
let res = this.resolve(key);
res = value;
fs.writeFileSync(this.loc, JSON.stringify(res, null, 2));
return true;
}
else {
throw new Error("[ERROR] : This key does not exist");
}
}
/**
* Deletes a key in the json file
*
* @param {string} key - Key name
* @throws Will throw an error if key does not exist
* @returns {boolean}
* @constructor
*/
Del(key) {
if (!this.error && this.resolve(key)) {
delete this.resolve(key);
fs.writeFileSync(this.loc, JSON.stringify(this.data, null, 2));
return true;
} else {
throw new Error("[ERROR] : This key does not exist");
}
}
/**
* finds a key in the json file
* @param {string} key - Key name
* @returns {(string|boolean)} return bool if the key does not exist
* @constructor
*/
resolve(key) {
if (key.indexOf(".") > -1) {
return key.split(".").reduce(function (prev, curr) {
return prev ? prev[curr] : false;
}, this.data);
}
else {
if (typeof this.data[key] !== "undefined") {
return this.data[key];
} else {
return false;
}
}
}
};